连接C ++和C#代码用C ++ / CLI桥 [英] Connecting c++ and c# code with a c++/cli bridge

查看:86
本文介绍了连接C ++和C#代码用C ++ / CLI桥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这是使用原生C ++的DLL本地C ++代码的客户端应用。我调查的连接该代码使用C#的dll,因为他们会更容易编写的可能性。我决定写这可以用LoadLibrary和其装载会传递到C#DLL的调用一个C ++ / CLI桥DLL。

I have a client application in native c++ code which is using native c++ dlls. I am investigating the possibility of connecting this code with c# dlls as they would be much easier to write. I decided to write a c++/cli bridge dll which can be loaded with LoadLibrary and which would pass the calls to c# dll.

在客户端和DLL之间的通信是这样,客户端将指针传递到一个接口对象,通过它的DLL然后与客户端通信。我在C ++ / CLI桥接代码这个包裹对象的C#代码使用它。

The communication between the client and the dll is such that the client passes a pointer to an interface object through which the dll then communicates with the client. I wrapped this object in the c++/cli bridge code for the c# code to use it.

桥还应暴露与__declspec(dllexport)的多种功能,并传递到c#DLL那些呼叫,因此它需要有一个指针到交流#接口其所将通过他们。我想用C#对象与gcroot<>包装,但问题是,我得到这两个DLL之间的循环依赖。 C#DLL需要引用桥DLL能够使用包装类和桥的DLL需要引用的C#的DLL使用接口类

The bridge should also expose several functions with __declspec(dllexport) and pass those calls to the c# dll, so it needs to have a pointer to a c# interface to which it would pass them. I wanted to use a c# object with the gcroot<> wrapper, but the problem is that I get circular dependencies between these two dlls. C# dll needs to reference the bridge dll to be able to use the wrapper class and the bridge dll needs to reference the c# dll to use the interface class.

我知道我可以使用COM的替代包装与gcroot C#的对象,但我宁愿不要。有没有解决这个办法吗?

I know I can use COM instead of wrapping the c# object with gcroot, but I'd rather not. Is there any way around this?

推荐答案

只要定义在C ++ / CLI而不是C#的接口。这消除了完全的C#项目的依赖

Just define the interface in C++/CLI instead of C#. This eliminates the dependency on the C# project entirely.

我推荐的C ++ / CLI项目作为只是一个包装的思考 - 不要在那里定义任何新接口。只要看看什么是在当前的C ++代码,并在裁判班把它包起来,以便您可以构建并从C#调用它们。

I recommend thinking of the C++/CLI project as nothing but a wrapper - don't define any new interfaces in there. Just take what is in the current C++ code, and wrap it in "ref classes" so you can construct and call them from C#.

这篇关于连接C ++和C#代码用C ++ / CLI桥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆