开发者

Accessing C# library from VC++ code

I have windows form application written in VC ++ . I wan开发者_Go百科t 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜