简体   繁体   English

如何从标准的非托管非.NET应用程序调用C ++ / CLI(.NET)DLL?

[英]How do I call C++/CLI (.NET) DLLs from standard, unmanaged non-.NET applications?

In the unmanaged world, I was able to write a __declspec(dllexport) or, alternatively, use a .DEF file to expose a function to be able to call a DLL. 在非托管的世界中,我能够编写__declspec(dllexport),或者使用.DEF文件来公开一个能够调用DLL的函数。 (Because of name mangling in C++ for the __stdcall, I put aliases into the .DEF file so certain applications could re-use certain exported DLL functions.) Now, I am interested in being able to expose a single entry-point function from a .NET assembly, in unmanaged-fashion, but have it enter into .NET-style functions within the DLL. (由于在C ++中使用__stdcall进行名称修改,我将别名放入.DEF文件中,以便某些应用程序可以重用某些导出的DLL函数。)现在,我感兴趣的是能够从一个单独的入口点函数公开.NET程序集,以非托管方式,但让它在DLL中进入.NET样式的函数。 Is this possible, in a simple and straight-forward fashion? 这可能是一种简单而直接的方式吗?

What I have is a third-party program that I have extended through DLLs (plugins) that implement some complex mathematics. 我所拥有的是第三方程序,我通过DLL(插件)扩展,实现了一些复杂的数学。 However, the third-party program has no means for me to visualize the calculations. 但是,第三方程序无法让我可视化计算。 I want to somehow take these pre-written math functions, compile them into a separate DLL (but using C++/CLI in .NET), but then add hooks to the functions so I can render what's going on under the hood in a .NET user control. 我想以某种方式采用这些预先编写的数学函数,将它们编译成一个单独的DLL(但在.NET中使用C ++ / CLI),然后在函数中添加钩子,这样我就可以在.NET中渲染一些内容。用户控制。 I'm not sure how to blend the .NET stuff with the unmanaged stuff, or what to Google to accomplish this task. 我不确定如何将.NET内容与非托管内容混合,或者谷歌如何完成此任务。

Specific suggestions with regard to the managed/unmanaged bridge, or alternative methods to accomplish the rendering in the manner I have described would be helpful. 关于托管/非托管网桥的具体建议,或以我所描述的方式完成渲染的替代方法将会有所帮助。 Thank you. 谢谢。

Do you use C++/CLI because you want to, or because you think you have to to export functions? 您是否因为想要使用C ++ / CLI,或者因为您认为必须导出函数?

In case of the latter, check out my unmanaged exports , which allows you to declare unmanaged exports in C# equivalent to how DllImport works. 如果是后者,请查看我的非托管导出 ,它允许您在C#中声明非托管导出,等同于DllImport的工作方式。

internal class Sample
{
  [DllExport("_export_test", CallingConvention.Cdecl)]
  static int Test(int a)
  {
     return a + 1;
  }
}

Well, the C++/CLI compiler makes it pretty easy. 好吧,C ++ / CLI编译器使它非常简单。 Just write a static managed function and attribute it with __declspec(dllexport). 只需编写一个静态托管函数,并使用__declspec(dllexport)对其进行归属。 The compiler injects a stub that automatically loads the CLR to execute the managed code. 编译器注入一个存根,该存根自动加载CLR以执行托管代码。

That's a serviceable approach, it isn't very extensible and it won't be very fast. 这是一种可用的方法,它不是很可扩展,而且速度也不会很快。 The next step up is that you write a ref class with the [ComVisible(true)] attribute. 下一步是您使用[ComVisible(true)]属性编写ref类。 After registering it with Regasm.exe, any unmanaged COM aware client can use that server. 使用Regasm.exe注册后,任何非托管的COM感知客户端都可以使用该服务器。 Hosting the CLR yourself (CorBindToRuntimeEx) is usually the last choice, but the most universal one. 自己托管CLR(CorBindToRuntimeEx)通常是最后的选择,但是最普遍的选择。


Example code: 示例代码:

ref class ManagedClass {
public:
  static void StaticFunc() {}
};

extern "C" __declspec(dllexport)
void __stdcall UnmanagedFunc() {
  ManagedClass::StaticFunc();
}

This CodeProject article explains the process pretty well. CodeProject的这篇文章很好地解释了这个过程。

Using managed code in an unmanaged application 在非托管应用程序中使用托管代码
http://www.codeproject.com/KB/mcpp/ijw_unmanaged.aspx http://www.codeproject.com/KB/mcpp/ijw_unmanaged.aspx

See also here and here . 另见这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM