1

I have windows form application written in VC ++ . I want to change the button click in this app (WInform app in VC++) to hit a function written in C#. What are the possible ways to do this.

6
  • Is this a CLI project or straight Win32/MFC? Commented Oct 3, 2011 at 21:03
  • This is a straight Win32/MFC project Commented Oct 3, 2011 at 21:20
  • If this really is necessary, can you just compile the C# as an executable and call that? C++ to C# is a lot more difficult than the other way around. Commented Oct 3, 2011 at 21:32
  • codeproject.com/KB/mcpp/ijw_unmanaged.aspx Commented Oct 3, 2011 at 21:35
  • Is the C# function part of an existing library or something you are writing ? Commented Oct 4, 2011 at 2:23

1 Answer 1

1

Calling a C# assembly without compiling with C++/CLI is tricky. some of the ideas I've had:

a) Create a mixed-assembly wrapper dll (mixed assemblies contain both native code and managed code). Add a reference to your C# assembly, then create functions in your dll that call the C# code via C++/CLI, then define dll entrypoints ( __declspec(dllexport) ) for those functions. You can now link against the dll as it will provide a native interface that C++ can understand, while internally that dll can call your C# code. ( Afterthought: You can probably compile this to a .lib, that would be easier than a dll.)

Walkthrough: Creating and Using a Static Library

b) Use Mono to call the C# assembly. Mono has a C API which you can use to load and use managed assemblies without using c++/CLI. This is what I would do if C++/CLI is not an option.

Embedding Mono

c) If your function has a relatively simple input/output signature (no large objects, etc) you might consider compiling your C# function into an executable and use standard input/output to call the function. You'll have to carry input arguments and the output as text, though. Then make C++ start the executable and pass some input to it, and read the output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.