能否使用 C++/CLI 从本机 C++ 调用 .NET 代码? 应用程序?
我已经使用 C++/CLI 完成了另一种方法(从 .NET 调用纯 C++ 代码),并且它有效(大部分)。
本机到 C++/CLI 方向是如何完成的?
我真的不想使用 COM 互操作...
I've done the other way around (calling pure C++ code from .NET) with C++/CLI, and it worked (for the most part).
How is the native-to-C++/CLI direction done?
I really don't want to use COM interop...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您有一个现有的本机 C++ 应用程序,并且希望避免过多的 CLR 内容“污染”它,您可以仅为一个特定文件打开
/clr
标志,并使用标准 C++ 标头来提供它的一个接口。 我已经用一段旧代码完成了此操作。 在标头中我有:所以程序的其余部分有一个简单的 API,它可以向其传递 HICON 和目标文件路径。
然后我有一个单独的源文件,它是唯一打开
/clr
的文件:这样我就可以将 HICON 从我的旧 C++ 程序中转换为 PNG 文件,但我已经隔离了.NET 框架与其余代码的区别 - 因此,如果我以后需要可移植,我可以轻松地交换不同的实现。
您可以更进一步,将依赖于 CLR 的代码放入单独的 DLL 中,尽管除非您希望能够单独修补它,否则几乎没有什么附加值。
If you have an existing native C++ app and want to avoid "polluting" it with too much CLR stuff, you can switch on the
/clr
flag for just one specific file and use a standard C++ header to provide an interface to it. I've done this in an old bit of code. In the header I have:So the rest of the program has a simple API to which it can pass an HICON and a destination filepath.
Then I have a separate source file which is the only one that has
/clr
switched on:That way I can convert HICONs into PNG files from my hairy old C++ program, but I've isolated the use of the .NET framework from the rest of the code - so if I need to be portable later, I can easily swap in a different implementation.
You could take this a stage further and put the CLR-dependent code in a separate DLL, although there would be little added value in that unless you wanted to be able to patch it separately.
您始终可以在本机应用中托管 CLR。
You can always host the CLR in your native app.
C++/CLI in Action 一书有一章名为“混合托管代码和本机代码”在本章中,在使用互操作机制标题下,它讨论了从本机代码访问托管库和从托管代码访问本机库。 当我曾经读过它时,它确实帮助我理解了概念。
The book C++/CLI in Action has a chapter named Mixing Managed and Native Code and inside the chapter, under Working With Interop Mechanisms heading, it talks about both accessing a managed library from native code and accessing a native library from managed code. It did help me get the concepts when I read it once upon a time.
您应该查看非托管导出,您可以将其作为NuGet 包。 这是作者的描述:
You should take a look at Unmanaged Exports, which you can get as a NuGet package. This is the description according to the author:
从 C++/CLI 调用 .NET 代码非常简单。 它与常规 C++ 非常相似。 确保您的项目设置为 C++/CLI 项目,通过转到“公共属性”下的项目属性添加对 .NET 程序集的引用,然后将 .NET 对象与如下代码一起使用:
Calling .NET code from C++/CLI is very straightforward. Its very similar to regular C++. Make sure your project is setup as a C++/CLI project, add a reference to your .NET assembly by going to the project properties under "Common Properties", then use your .NET objects with some code like this: