从 .net DLL 导出本机 C 函数?
我有一个用 C# 编写的 .net 程序集,我希望从该程序集中导出本机 C 函数。
我有一个应用程序,它将对“插件”文件夹中找到的任何 DLL 执行“LoadLibrary()”。加载 DLL 后,应用程序执行“GetProcAddress”来查找名为“Register”的函数。应用程序期望此函数遵循 C 调用约定。
如何从 .net 程序集中导出名为“Register”的函数,以便我可以成功连接该应用程序的插件系统?
谢谢, 安德鲁
I have a .net assembly written in C#, and I'm looking to export a native C function from the assembly.
I have an application which will perform a 'LoadLibrary()' on any DLL's found in a 'plugin' folder. After loading the DLL, the application performs a 'GetProcAddress' looking for a function named 'Register'. The application expects this function to follow the C calling convention.
How can I export a function named 'Register' from my .net assembly, so I can successfully hookup with the plugin system for this application?
Thanks,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请查看非托管导出。
Have a look at Unmanaged Exports.
用托管 C++ 编写一个 .Net 库,您可以在其中导出“本机”方法/函数。
Write a .Net library in Managed C++ and there you can export a "Native" method/function.
您想要的是反向 P/Invoke。您实际上无法将 C 函数嵌入到 C# dll 中,如果您的意思是实际用 C 实现的函数,但按照链接页面上给出的教程,您可以创建可由非托管 C/C++ 代码调用的 DLL 导出。
What you want is a Reverse P/Invoke. You can't actually embed a C function in a C# dll, if by that you mean a function actually implemented in C, but by following the tutorials given on the linked page you can create a DLL export that's callable by unmanaged C/C++ code.
遗憾的是,Microsoft 不支持此功能,您必须在构建后更改 msil 才能公开这些方法。
这是可能的,因为一个人已经展示了一个合理的解决方案
在 codeproject 或 此处,
但它需要一个构建后步骤,之后你就得靠自己了。
我不知道这个 hack 是否适用于 .net 4.0 或更高版本。
希望 Microsoft 能够听取我们的意见并支持 C# 中的这个简单功能,因为 CLR 支持已经存在。
Sadly Microsoft does not support this feature, and you have to change the msil after the build to expose those methods.
It is possible as one guy has shown a reasonable solution
on codeproject or here,
but it requires a post build step and you are on your own after that.
I don't know if this hack will work on .net 4.0 or later though.
Hopefully Microsoft will listen to us and support this simple feature in C# since the CLR support is already there.