我如何在 Win32 C++ 中使用 C# dll项目?
我正在研究一个解决方案,它的大部分核心引擎都是作为 Win32 C++ 开发的(并且与平台无关,也在 OS X 上使用),前段时间我们需要从 C# 调用核心引擎的 C++ dll,我能够在 C# 中加载主解决方案的 DLL(在 SO 上的一些线程的帮助下)。但现在我们在托管 C# dll 中实现了某些功能,并且需要在 Win32 C++ 项目中使用它? (仅提供函数定义和dll)
I am working on a solution, most of its core engine is developed as Win32 C++ (and is Platform independent and is also used on OS X), some time ago we needed to call C++ dll's of core engine from C# and I was able to Load main solution's DLL in C# (by the help of some threads here on SO). but now we have certain things implemented in Managed C# dll and need to use it in Win32 C++ project? (and just function definitions and dll are provided)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建托管 C++ 互操作 DLL 来充当 C# 库的包装器。
遗憾的是,大多数有关托管 C++ 的教程仅解释如何包装非托管 C++ 以在 C# 中使用。但它也可以以其他方式发挥作用。
在本机 C++ 代码中定义一个抽象接口类,然后在托管 C++ DLL 中创建一个具体子类。在方法实现中调用 C# 对象。
最后,导出一个工厂函数,该函数将实例化实现类并返回本机代码可以使用的基类指针。
下面是一个简单的示例:
首先,在本机 DLL 中定义类接口。
interopclassbase.h
现在您需要创建一个 C++/CLI DLL,它允许您在单个程序集中混合本机代码和托管代码。将新的 C++ 项目添加到您的解决方案中,并在项目配置中设置 “公共语言运行时支持” 选项到混合 (/clr)。
添加对 C# 库(我们将其称为 ManagedLibrary)的引用后,我们就可以实现互操作类:
interopclass.cpp
现在,您只需从本机项目加载互操作 DLL 并调用导出的函数。
You can create a managed C++ interop DLL to act as a wrapper around the C# library.
Most tutorials on managed C++ unfortunately only explain how to wrap unmanaged C++ for use in C#. But it can work the other way also.
Define an abstract interface class in your native C++ code, then create a concrete subclass inside the managed C++ DLL. Call into your C# objects in the method implementations.
Finally, export a factory function that will instantiate the implementation class and return a base-class pointer that your native code can use.
Here's a quick example:
First, define the class interface in your native DLL.
interopclassbase.h
Now you need to create a C++/CLI DLL that will allow you to mix native and managed code in a single assembly. Add a new C++ project to your solution, and in the project configuration set the "Common Language Runtime Support" option to Mixed (/clr).
Once you've added a reference to your C# library (which we'll call ManagedLibrary) we can implement the interop class:
interopclass.cpp
Now you just need to load the interop DLL from your native project and call the exported function.
一种解决方案是 COM Interop。可能是唯一的解决方案。这是一个很大的话题。我在工作时使用一本厚厚的蓝皮书。数百页除了 COM 互操作之外什么都没有。
简而言之,您在托管端标记一些类和接口,并创建看起来像 COM dll 的互操作程序集,但实际上是托管程序集的代理。互操作程序集像 COM dll 一样注册,然后就可以使用了。
MSDN 有很多相关信息。
这可能是一个很好的起点。
“向 COM 公开 .NET Framework 组件”
http://msdn.microsoft.com/en- us/library/aa720072%28VS.71%29.aspx
它可以非常简单,但尽量保持简单。
One solution is COM Interop. Possibly the only solution. It a big topic. There's a big fat blue book at work I use. Hundreds of pages of nothing but COM Interop.
The short version is that you label some classes and interfaces in the managed side, and have interop assemblies created which look like COM dlls, but are really proxies to the managed assemblies. The interop assemblies are registered like COM dlls and off you go.
MSDN has a lot of information about it.
This is probably a good starting spot.
"Exposing .NET Framework Components to COM"
http://msdn.microsoft.com/en-us/library/aa720072%28VS.71%29.aspx
It can be surprisingly easy, but try to keep it simple.
要创建托管对象并调用其方法,您需要在 C++ 进程中运行 CLR。
在 Windows 下,您可以通过引用 mscoree.dll 并在进程中托管 CLR 来托管 CLR。
http://msdn.microsoft.com/en-us/magazine/cc163567。 .aspx
http://msdn.microsoft.com/en-us/library/ms230997。 aspx
对于 Mono,您可以将 Mono 运行时嵌入到您的 C++ 应用程序中。
http://www.mono-project.com/Embedding_Mono
To create a managed object and call methods on it you need to have the CLR running in your C++ process.
Under windows you can host the CLR by referencing mscoree.dll and hosting the CLR in process.
http://msdn.microsoft.com/en-us/magazine/cc163567.aspx
http://msdn.microsoft.com/en-us/library/ms230997.aspx
For Mono, you can embed the Mono runtime within your C++ application.
http://www.mono-project.com/Embedding_Mono