如何从非托管代码中使用托管代码?

时间:2022-09-01 11:48:03

How do I call a .NET code from native C++ (unmanaged code)? I want to expose .NET code to my unmanaged (C++) application and then use them. More specifically, I want to call C# from native C++ :). I know there are many ways but can you tell me the pros and cons of each? By the way, I don't want to use COM so what are the options now?

如何从本机c++(非托管代码)调用。net代码?我希望将. net代码暴露给我的非托管(c++)应用程序,然后使用它们。更具体地说,我想从本机c++:)调用c#。我知道有很多方法,但是你能告诉我每种方法的优缺点吗?顺便说一下,我不想使用COM那么现在的选项是什么?

Is it possible that I wrap the C# code in C++/CLI and then call it from C++? If so, how do I do that? How do I wrap the C# in C++/CLI and then call it from C++?

我是否可以将c#代码封装在c++ /CLI中,然后从c++中调用它?如果是的话,我该怎么做呢?如何在c++ /CLI中包装c#,然后从c++调用它?

4 个解决方案

#1


5  

I've written about it just recently. It was about Delphi, but that doesn't mean it won't work with C++ as well.

我最近才写过。它是关于Delphi的,但这并不意味着它在c++中也不适用。

.NET component in DELPHI 2009

。net组件,DELPHI 2009

Even without knowing much about C++, I still know that IUnknown and COM-compatible interface references should be usable just fine from C++ (in the case you need to pass objects, not just structures).

即使不知道很多关于c++的知识,我仍然知道IUnknown和com兼容的接口引用应该可以从c++中使用(在需要传递对象的情况下,不只是结构)。

  • You can use Microsoft's C++/CLI to reference your C# code and export it as ordinary DLL functions.
  • 您可以使用Microsoft的c++ /CLI引用您的c#代码并将其导出为普通的DLL函数。
  • You can also download an MSBuild task I wrote, that allows you to export function directly from C#. Analogous to how DllImportAttribute is used.
  • 您还可以下载我编写的MSBuild任务,它允许您直接从c#导出函数。类似于如何使用DllImportAttribute。

This C# code would export a function "YourExportName" that can be used just like any c-compatible function would be used.

这个c#代码将导出一个函数“YourExportName”,可以像使用任何C兼容函数一样使用它。


class Sample
{
   [DllExport("YourExportName")]
   static int TestFunction(int left, int right)
   {
     return left + right;
   }
}

#2


1  

You can always embed Mono. I have personally done it in two projects so far and it's easy enough. Follow Embedding Mono for more information.

你可以一直嵌入Mono。到目前为止,我个人已经在两个项目中做过,而且非常简单。按照嵌入Mono来获取更多信息。

#3


0  

You could expose the managed component as COM object.

可以将托管组件公开为COM对象。

#4


0  

Check How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005

检查如何从Visual Studio中的本机Visual c++代码调用托管DLL。NET或Visual Studio 2005

#1


5  

I've written about it just recently. It was about Delphi, but that doesn't mean it won't work with C++ as well.

我最近才写过。它是关于Delphi的,但这并不意味着它在c++中也不适用。

.NET component in DELPHI 2009

。net组件,DELPHI 2009

Even without knowing much about C++, I still know that IUnknown and COM-compatible interface references should be usable just fine from C++ (in the case you need to pass objects, not just structures).

即使不知道很多关于c++的知识,我仍然知道IUnknown和com兼容的接口引用应该可以从c++中使用(在需要传递对象的情况下,不只是结构)。

  • You can use Microsoft's C++/CLI to reference your C# code and export it as ordinary DLL functions.
  • 您可以使用Microsoft的c++ /CLI引用您的c#代码并将其导出为普通的DLL函数。
  • You can also download an MSBuild task I wrote, that allows you to export function directly from C#. Analogous to how DllImportAttribute is used.
  • 您还可以下载我编写的MSBuild任务,它允许您直接从c#导出函数。类似于如何使用DllImportAttribute。

This C# code would export a function "YourExportName" that can be used just like any c-compatible function would be used.

这个c#代码将导出一个函数“YourExportName”,可以像使用任何C兼容函数一样使用它。


class Sample
{
   [DllExport("YourExportName")]
   static int TestFunction(int left, int right)
   {
     return left + right;
   }
}

#2


1  

You can always embed Mono. I have personally done it in two projects so far and it's easy enough. Follow Embedding Mono for more information.

你可以一直嵌入Mono。到目前为止,我个人已经在两个项目中做过,而且非常简单。按照嵌入Mono来获取更多信息。

#3


0  

You could expose the managed component as COM object.

可以将托管组件公开为COM对象。

#4


0  

Check How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005

检查如何从Visual Studio中的本机Visual c++代码调用托管DLL。NET或Visual Studio 2005