如何从 Win32 进程调用 .NET DLL?
在 Win32 进程中使用 .NET DLL 时有哪些选项? 我基本上需要使用 Win32 进程中的 C# DLL。
我现在有一个可能的解决方案,需要将 C# DLL 添加到 GAC(使用 RegAsm.exe),然后通过 COM 包装调用来调用 C# DLL。 然而,这个解决方案相当沉重。它要求将 .NET DLL 添加到所有应该运行此 Win32 进程的计算机。
在使用 C# DLL 之前,是否可以在不调用 RegAsm 的情况下完成此操作?
What are the options when it comes to using a .NET DLL from a Win32 process?
I need to basically use a C# DLL from a Win32 process.
I have a possible solution right now that requires adding the C# DLL to the GAC (using RegAsm.exe), then calling the C# DLL via COM wrapped calls.
However that solution is pretty heavy. It requires that the .NET DLL be added to the GAC on all machines that are supposed to run this Win32 process.
Would it be possible to do this without having to call RegAsm
prior to being able to use the C# DLL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将免注册 COM 与 .NET COM 组件结合使用 - 请参阅 在这里。
另一种选择是使用 C++/CLI 作为桥梁。人们最熟悉的是使用它来包装非托管 API 以公开给托管代码,但它实际上是双向的 - 可以使用
/clr
进行编译,但仍会生成.dll< /code> 具有普通非托管导出的程序集,可以像往常一样从非托管代码调用。这是一个非常简单的示例,以这种方式公开
System::String::ToUpper
:这将生成
wrapper.dll
- 混合托管/非托管程序集 - 和一个导出库wrapper.lib
。后者可以在纯本机应用程序中使用,如下所示:实际上,它将 CLR 运行时加载到调用进程中(除非它已经加载到那里),并透明地从本机代码分派到托管代码 - 所有魔法都是由 C++/CLI 完成的编译器。
You can use registration-free COM with .NET COM components - see here.
Another option is to use C++/CLI as a bridge. People are mostly familiar with using it to wrap unmanaged APIs to expose to managed code, but it actually works both ways - it is possible to compile with
/clr
, and yet produce a.dll
assembly with plain unmanaged exports, which can be called from unmanaged code as usual. Here's a very simple example that exposesSystem::String::ToUpper
that way:This will produce
wrapper.dll
- hybrid managed/unmanaged assembly - and an export librarywrapper.lib
. The latter can be used in a pure native application as follows:In practice it will load CLR runtime into the calling process (unless it's already loaded there) and dispatch from native code to managed code transparently - all the magic is done by C++/CLI compiler.
有两种选择。
首先,您可以使用免注册COM Interop。
其次,您可以使用 CLR 托管 API 直接托管 CLR ,并加载程序集。这无需 COM 即可工作。
There are two options.
First, you can use Registration Free COM Interop.
Second, you could use the CLR Hosting APIs to directly host the CLR, and load the assembly. This works without COM.