如何从非托管代码中使用托管代码?
如何从本机 C++(非托管代码)调用 .NET 代码?我想将 .NET 代码公开给我的非托管 (C++) 应用程序,然后使用它们。更具体地说,我想从本机 C++ 调用 C# :)。我知道有很多方法,但你能告诉我每种方法的优缺点吗?顺便问一下,我不想使用 COM,那么现在有什么选择呢?
我是否可以将 C# 代码包装在 C++/CLI 中,然后从 C++ 调用它?如果是这样,我该怎么做?如何将 C# 包装在 C++/CLI 中,然后从 C++ 调用它?
How do I call a .NET code from native C++ (unmanaged code)? I want to expose .NET code to my unmanaged (C++) application and then use them. More specifically, I want to call C# from native C++ :). I know there are many ways but can you tell me the pros and cons of each? By the way, I don't want to use COM so what are the options now?
Is it possible that I wrap the C# code in C++/CLI and then call it from C++? If so, how do I do that? How do I wrap the C# in C++/CLI and then call it from C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最近才写过相关文章。它是关于 Delphi 的,但这并不意味着它不能与 C++ 一起使用。
DELPHI 2009 中的.NET 组件
即使对 C++ 不太了解,我仍然知道 IUnknown 和 COM 兼容的接口引用应该可以在 C++ 中很好地使用(如果您需要传递对象,而不仅仅是结构)。
此 C# 代码将导出一个函数“YourExportName”,可以像使用任何 C 兼容函数一样使用该函数。
I've written about it just recently. It was about Delphi, but that doesn't mean it won't work with C++ as well.
.NET component in DELPHI 2009
Even without knowing much about C++, I still know that IUnknown and COM-compatible interface references should be usable just fine from C++ (in the case you need to pass objects, not just structures).
This C# code would export a function "YourExportName" that can be used just like any c-compatible function would be used.
您始终可以嵌入 Mono。到目前为止,我个人已经在两个项目中完成了它,而且很简单。请关注嵌入 Mono 了解更多信息。
You can always embed Mono. I have personally done it in two projects so far and it's easy enough. Follow Embedding Mono for more information.
您可以将托管组件公开为COM对象。
You could expose the managed component as COM object.
检查如何从 Visual Studio.NET 或 Visual Studio 2005 中的本机 Visual C++ 代码调用托管 DLL
Check How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005