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.
-
Is this a CLI project or straight Win32/MFC?Ed Swangren– Ed Swangren2011-10-03 21:03:32 +00:00Commented Oct 3, 2011 at 21:03
-
This is a straight Win32/MFC projectparanjai– paranjai2011-10-03 21:20:50 +00:00Commented 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.Ed Swangren– Ed Swangren2011-10-03 21:32:30 +00:00Commented Oct 3, 2011 at 21:32
-
codeproject.com/KB/mcpp/ijw_unmanaged.aspxfardjad– fardjad2011-10-03 21:35:19 +00:00Commented Oct 3, 2011 at 21:35
-
Is the C# function part of an existing library or something you are writing ?user957902– user9579022011-10-04 02:23:03 +00:00Commented Oct 4, 2011 at 2:23
1 Answer
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.
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.