如何使用从非托管code管理code?如何使用、code

2023-09-03 02:09:37 作者:热情喂冷风

我如何打电话从本机C ++(非托管code)一种.NET code?我要揭露​​.NET code到我的非托管(C ++)应用程序,然后使用它们。更具体地讲,我想打电话给C#从本地C ++ :)。我知道有很多方法,但你能告诉我每一个优点和缺点?顺便说一句,我不希望使用COM那么什么是选择现在?

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 ++ / CLI的C#code,然后从C ++调用它呢?如果是这样,我怎么做呢?我该如何包装在C ++ / CLI的C#,然后从C调用它++?

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 ++工作也。

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

.NET的德尔福组件2009年

即使不知道很多关于C ++,我还是知道++(的情况下,你需要传递的对象,而不仅仅是结构)IUnknown和COM兼容接口引用应该是可用的从C就好了。

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).

您可以使用微软的Visual C ++ / CLI参考你的C#code,并将其导出为普通的DLL函数。 您也可以下载 MSBuild任务我写的,这可以让你直接导出功能从C#。类似于如何DllImportAttribute被使用。 You can use Microsoft's C++/CLI to reference your C# code and export it as ordinary DLL functions. You can also download an MSBuild task I wrote, that allows you to export function directly from C#. Analogous to how DllImportAttribute is used.

本C#code将导出功能YourExportName,可用于就像任何C兼容的功能将被使用。

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

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