如何从Windows中未管理的C ++代码调用托管.NET代码,反之亦然?

时间:2022-09-01 14:23:02

I have a pure C++ application developed using VC 6.0. I would like this application to make use of a library developed in C#. How do I call the methods in the C# library from my native executable? I do not want to convert my un-managed C++ native application to managed code. Similarly, how do I do the reverse? Is PInvoke the only option? I would appreciate any references or pointers for the same.

我有一个使用VC 6.0开发的纯C ++应用程序。我希望这个应用程序能够使用在C#中开发的库。如何从本机可执行文件中调用C#库中的方法?我不想将我的未托管C ++本机应用程序转换为托管代码。同样,我该怎么做呢? PInvoke是唯一的选择吗?我将不胜感激任何参考或指针。

2 个解决方案

#1


To call into managed code from unmanaged C++, use ClrCreateManagedInstance, or export your types in your managed assembly as COM visible, and use COM. To call into unmanaged code from managed, use COM or P/Invoke.

要从非托管C ++调用托管代码,请使用ClrCreateManagedInstance,或者将托管程序集中的类型导出为COM可见,并使用COM。要从托管中调用非托管代码,请使用COM或P / Invoke。

#2


Microsoft's main line on this is use COM interop. There is however another option, sometimes referred to as "Reverse P/Invoke" there is a interesting blog post here and some more here

微软的主要内容是使用COM互操作。然而,有另一种选择,有时被称为“反向P /调用”,这里有一篇有趣的博客文章,还有一些在这里

Also, if you have Delphi.NET (now defunct) this language allows you to export static methods like you would any dll function then you can call into the Delphi.NET assembly just like a normal native Dll.

此外,如果您有Delphi.NET(现已不存在),这种语言允许您像任何dll函数一样导出静态方法,那么您可以像普通的本机Dll一样调用Delphi.NET程序集。

#1


To call into managed code from unmanaged C++, use ClrCreateManagedInstance, or export your types in your managed assembly as COM visible, and use COM. To call into unmanaged code from managed, use COM or P/Invoke.

要从非托管C ++调用托管代码,请使用ClrCreateManagedInstance,或者将托管程序集中的类型导出为COM可见,并使用COM。要从托管中调用非托管代码,请使用COM或P / Invoke。

#2


Microsoft's main line on this is use COM interop. There is however another option, sometimes referred to as "Reverse P/Invoke" there is a interesting blog post here and some more here

微软的主要内容是使用COM互操作。然而,有另一种选择,有时被称为“反向P /调用”,这里有一篇有趣的博客文章,还有一些在这里

Also, if you have Delphi.NET (now defunct) this language allows you to export static methods like you would any dll function then you can call into the Delphi.NET assembly just like a normal native Dll.

此外,如果您有Delphi.NET(现已不存在),这种语言允许您像任何dll函数一样导出静态方法,那么您可以像普通的本机Dll一样调用Delphi.NET程序集。