简体   繁体   English

将C ++代码导出为DLL并使用C#导入?

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

I have a program written in C++ with a main function that calls a bunch of other C++ classes/functions. 我有一个用C ++编写的程序,它的主要功能调用了许多其他C ++类/函数。

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? 我是C ++的新手(成为python程序员),所以我想知道-为了将其导出为可从C#程序导入的DLL,我需要遵循哪些步骤? Any suggestions? 有什么建议么?

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 (ie, use C++/CLI). 如果希望C ++类在C#应用程序中可用,则需要使用COM或在C ++程序中定位CLR(即,使用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. 如果您只想在C ++ DLL中有一些要从C#调用的带有POD类型参数的函数,则将每个函数声明为extern "C"以避免名称修改,并使用DLLImport属性导入该函数。 PInvoke.net is a great resource here. PInvoke.net是一个很好的资源。

If you are only exporting plain functions, not classes, you can p/invoke them. 如果仅导出普通函数而不是类,则可以p /调用它们。 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. 如果ypu确实需要这些类,则必须将您的项目编写为C ++ / CLI,并像其他任何.NET程序集一样在c#项目中引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM