如何在C ++中调用C#代码 [英] How to call C# code in C++
问题描述
我有一个小C#类(Gram.cs),它正在与第三方dll一起操作设备。现在,我需要使用C ++在这个C#类中调用一些函数。
我该怎么做?
我正在使用MS Visual Studio 2010专业版。
如果 C#
类很小, code> dll ,它可能通过在 C ++
中更简单地重写该类,而不是集成 .Net
到您的应用程序。另外,集成 .Net
将强制整个 .Net
虚拟机开始处理你的简单类。 p>
否则,
- 您可以使用COM互操作:根据您的
C#
类构建一个程序集,使COM可见。然后你可以使用类作为COM对象。 - 您可以使用管理C ++ 在托管代码和非托管代码之间进行包装。
- 您可以逆向控制流程,因此
C#
代码将调用非托管函数。在这种情况下,您可以从二进制文件导出,并从C#
代码导入DllImport
属性。
I have a small C# class (Gram.cs) and it is working with a 3rd party dll to operate a device. Now, I need to call some functions in this C# class using C++.
How can I do it?
I am using MS Visual Studio 2010 professional edition.
If C#
class is small, and it deals with native dll
, it might by simpler rewrite that class in C++
, rather then integrate .Net
into your application. In addition, integrating .Net
will force whole .Net
virtual machine to start just for processing your simple class.
Otherwise,
- You could use COM interop: build an assembly based on your
C#
class and make it COM visible. Then you could use class as a COM-object. - You can use Managed C++ to make a wrapper between managed and unmanaged code.
- You can reverse control flow, so
C#
code will call unmanaged functions. In this case you can export it from your binary and import fromC#
code withDllImport
attribute.
这篇关于如何在C ++中调用C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!