Connecting C ++ and C # code with C ++ / cli bridge

I have a client application in my own C ++ code that uses native C ++ dlls. I am exploring the possibility of connecting this code with C # dll, as it would be much easier to write them. I decided to write a C ++ / cli bridge dll that can be loaded using LoadLibrary and which will pass C # dll calls.

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

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

I know that I can use COM instead of wrapping a C # object with gcroot, but I would prefer not to. Is there any way around this?

+1
source share
1 answer

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

I recommend thinking of the C ++ / CLI project as a wrapper alone - do not define any new interfaces. Just take what is in the current C ++ code and wrap it in β€œref classes” so you can build and call them from C #.

+2
source

All Articles