从非托管 C++ 调用 C# dll没有 COM 的应用程序
有没有办法在不使用 COM 的情况下从 C++ 非托管应用程序调用 C# DLL?
Is there a way to call c# dll from c++ unmanaged application without COM usage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
Reverse P/Invoke
来完成此操作 - 示例和讨论这里。You can do this using
Reverse P/Invoke
- example and discussion here.实际上可以反汇编、修改 IL,并使用导出函数重新组装它。几年前我搞砸了这个问题,创建了一个应用程序来反汇编 dll,提供可能导出的函数列表 - 允许用户选择它们,然后重新编写 IL 并重新组装所有内容。然后,我可以从非托管代码直接调用 dll...或者从托管代码调用 dll(不太实用,但仍然很有趣)。
当然,.net 语言本身不支持这一点是有原因的(即使它在 MSIL 中受支持)。我不会在生产中使用它:
死链接:
http://www.csharphelp.com/ 2007/03/将托管代码导出为非托管/
回溯机:
https://web.archive.org/web/20140213030149/http://www.csharphelp.com/2007/03/exporting-management-code-as-unmanagement/
It is actually possible to disassemble, modify the IL, and reassemble it with exported functions. I messed with this a few years ago, and created an application that would disassemble a dll, provide a list of functions that could potentially be exported - allowing the user to select them, then re-write the IL and reassemble everything. Then, I could call directly into the dll from unmanaged code...or p-invoke into the dll from managed code (not really practical, but interesting nonetheless).
Surely there is a reason that this isn't supported in the .net languages themselves (even tho it is supported in MSIL). I wouldn't use this in production:
Dead link:
http://www.csharphelp.com/2007/03/exporting-managed-code-as-unmanaged/
Wayback Machine:
https://web.archive.org/web/20140213030149/http://www.csharphelp.com/2007/03/exporting-managed-code-as-unmanaged/
我可能有点晚了,但请查看此。
使用这个小小的 msbuild 任务,您可以创建一个可以像本机 DLL 一样调用的 C# 库。 (例如,为需要本机 dll 的应用程序编写插件)
哦,不要忘记使用 项目模板,它将为您设置一切。
I might be a bit late, but check this out.
Using this little msbuild task, you can create a C# library that can be called as if it were a native DLL. (e.g. write plugins for apps that require them to be native dlls)
Oh and don't forget to use the project template, which will setup everything for you.
您唯一的选择实际上是使用 C++.net 或为其创建一个 C++.net 包装器来导出您需要的内容。
从 C++ 调用 C# 代码
Your only option really is to either use C++.net or create a C++.net wrapper for it that exports what you need.
Calling C# code from C++