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

发布于 2024-09-02 08:15:47 字数 473 浏览 2 评论 0原文

在非托管世界中,我能够编写 __declspec(dllexport),或者使用 .DEF 文件公开函数以便能够调用 DLL。 (由于 C++ 中 __stdcall 的名称修改,我将别名放入 .DEF 文件中,以便某些应用程序可以重用某些导出的 DLL 函数。) 现在,我感兴趣的是能够从.NET 程序集,以非托管方式,但让它进入 DLL 内的 .NET 样式函数。以简单直接的方式这可能吗?

我拥有的是一个第三方程序,我通过 DLL(插件)扩展了它,实现了一些复杂的数学运算。然而,第三方程序无法让我可视化计算。我想以某种方式获取这些预先编写的数学函数,将它们编译成一个单独的 DLL(但在 .NET 中使用 C++/CLI),然后向这些函数添加挂钩,以便我可以在 .NET 中渲染幕后发生的情况用户控制。我不确定如何将 .NET 内容与非托管内容混合在一起,也不知道如何通过 Google 来完成此任务。

关于托管/非托管桥的具体建议,或者以我所描述的方式完成渲染的替代方法将会有所帮助。谢谢。

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. (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. 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. 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. I'm not sure how to blend the .NET stuff with the unmanaged stuff, or what to Google to accomplish this task.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘初心 2024-09-09 08:15:47

嗯,C++/CLI 编译器使它变得非常简单。只需编写一个静态托管函数并使用 __declspec(dllexport) 为其赋予属性即可。编译器注入一个存根,自动加载 CLR 来执行托管代码。

这是一种有用的方法,但它的可扩展性不是很好,而且速度也不会很快。下一步是编写一个带有 [ComVisible(true)] 属性的 ref 类。使用 Regasm.exe 注册后,任何非托管 COM 感知客户端都可以使用该服务器。自己托管 CLR (CorBindToRuntimeEx) 通常是最后的选择,但却是最通用的选择。


示例代码:

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

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

Well, the C++/CLI compiler makes it pretty easy. Just write a static managed function and attribute it with __declspec(dllexport). The compiler injects a stub that automatically loads the CLR to execute the managed code.

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. After registering it with Regasm.exe, any unmanaged COM aware client can use that server. Hosting the CLR yourself (CorBindToRuntimeEx) is usually the last choice, but the most universal one.


Example code:

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

extern "C" __declspec(dllexport)
void __stdcall UnmanagedFunc() {
  ManagedClass::StaticFunc();
}
Bonjour°[大白 2024-09-09 08:15:47

您使用 C++/CLI 是因为您想要,还是因为您认为必须导出函数?

如果是后者,请查看我的非托管导出,它允许您在 C# 中声明非托管导出相当于 DllImport 的工作方式。

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

Do you use C++/CLI because you want to, or because you think you have to to export functions?

In case of the latter, check out my unmanaged exports, which allows you to declare unmanaged exports in C# equivalent to how DllImport works.

internal class Sample
{
  [DllExport("_export_test", CallingConvention.Cdecl)]
  static int Test(int a)
  {
     return a + 1;
  }
}
浅黛梨妆こ 2024-09-09 08:15:47

这篇 CodeProject 文章很好地解释了这个过程。

在非托管应用程序中使用托管代码
http://www.codeproject.com/KB/mcpp/ijw_unmanagement.aspx

另请参阅此处此处

This CodeProject article explains the process pretty well.

Using managed code in an unmanaged application
http://www.codeproject.com/KB/mcpp/ijw_unmanaged.aspx

See also here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文