Mixing c# and vc++

喜你入骨 提交于 2019-12-24 02:40:57

问题


I've got a code snippet of c# that I've been trying to translate into vc++, but I'm having some difficulties. So, I'm wondering if it is possible or advisable to mix c# and vc++; that is to say, can I call my c# function from vc++ and vice-versa. If so, are there tricks to it? If, not, why?


回答1:


Calling C++ code from C# is easy enough and you should be able to find plenty of articles on Pinvoke and other types of interop.

For calling your C# code from C++ - if your C++ app is managed then you should be able to directly reference the C# assembly from your C++ app and use it as normal.

If ypu C++ code is not managed then you will need to use some sort of interop to allow your C++ assembly to call C# code, the only way of doing this that springs to mind is COM.

Try some of these links for more information on COM interop with C#:

  • http://www.codeproject.com/KB/COM/COM_DOTNET_INTEROP.aspx
  • http://www.codeproject.com/KB/COM/nettocom.aspx?df=100&forumid=14076&exp=0&select=864859



回答2:


Is the VC++ app managed? If so, you could create a C# DLL, use it from your c++ app, and if you so desire, ILMerge them into a single EXE.




回答3:


If you want to mix the two, I'd start with C# as the base project and then register an unmanaged (C++) DLL in your C# project. That way you can combine the reliability of the managed C# parent application with the speed benefits of C++ in any processor-intensive methods you might have.



来源:https://stackoverflow.com/questions/1462963/mixing-c-sharp-and-vc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!