将C ++代码导出为DLL并使用C#导入? [英] Exporting C++ code as a DLL and import using C#?

查看:189
本文介绍了将C ++代码导出为DLL并使用C#导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的程序,主要功能是调用一堆其他的C ++类/函数。



我是C ++的新手(已经是一个python程序员),所以我想知道 - 我需要遵循的步骤才能将其导出为DLL这可以从C#程序导入?任何建议?

解决方案

如果您希望C ++类在C#应用程序中可用,您将需要使用COM或目标您的C ++程序中的CLR(即使用C ++ / CLI)。



如果您只需要C ++ DLL中的某些功能,您可以从C#中调用POD类型参数然后将每个函数声明为 externC以避免名称变形,并使用 DLLImport 属性导入该函数。 PInvoke.net 是一个很棒的资源。


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?

解决方案

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.

这篇关于将C ++代码导出为DLL并使用C#导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆