1

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.

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.

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.

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?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
  • Can't you pass function points/events or delegates from C# to C++. I know that for some advanced timer stuff you can handle C++ events easily from c#. – CodingBarfield Nov 25 '10 at 09:40
  • 1
    I know this is an old post, but see http://stackoverflow.com/questions/13293888/how-to-call-a-c-sharp-library-from-native-c-using-c-cli-and-ijw for more on using C# libraries in non managed code via a c++/CLI Wrapper. – amalgamate Aug 12 '14 at 17:11

1 Answers1

2

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

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#.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • That is exactly what I am doing - wrapping c++ code in ref classes for c# dll to use them. But the bridge must also forward calls from c++ client to c# dll. To do that, the bridge must use some of c# dll's code, thus the dependency. I don't see how it can be eliminated. Am I missing something completely obvious? –  May 19 '09 at 06:31