Exporting C++ code as a DLL and import using C#?

孤者浪人 提交于 2020-01-06 13:29:11

问题


I have a program written in C++ with a main function that calls a bunch of other C++ classes/functions.

I am new to C++ (been a python programmer), so I'm wondering - What are the steps I need to follow to be able to export this as a DLL that is importable from a C# program? Any suggestions?


回答1:


If you want your C++ classes to be usable in a C# application you will need to use COM or target the CLR in your C++ program (i.e., use C++/CLI).

If you simply have some functions in the C++ DLL that you want to call from C# that take POD type arguments then declare each function as extern "C" to avoid name mangling and use the DLLImport attribute to import the function. PInvoke.net is a great resource here.




回答2:


If you are only exporting plain functions, not classes, you can p/invoke them. if ypu really need the classes, you have to write your project as a C++/CLI and reference it in your c# project like any other .NET assembly.



来源:https://stackoverflow.com/questions/8333159/exporting-c-code-as-a-dll-and-import-using-c

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