How can I use a C # dll in a Win32 C ++ project?

I am working on a solution, most of its main engine is developed as Win32 C ++ (and is platform independent and also used in OS X), some time ago we needed to call the C ++ kernel dll from C # and I managed to load basic DLL solution in C # (using some threads here on SO). but now we have certain things implemented in managed C # dll, and we need to use it in a Win32 C ++ project? (and only function and dll definitions are provided)

+4
source share
3 answers

You can create a managed DLL for interacting with C ++ to act as a wrapper around the C # library.

Most guided C ++ manuals, unfortunately, only explain how to wrap unmanaged C ++ for use in C #. But it can work in another way.

Define the abstract interface class in your own C ++ code, then create a specific subclass inside the managed C ++ DLL. Call your C # objects in the method implementation.

Finally, export the factory function, which will instantiate the implementation class and return a pointer to the base class that your own code can use.

Here is an example:

First, define the class interface in your native DLL.

interopclassbase.h

class InteropClassBase { public: virtual void doStuff() = 0; virtual int getValue() = 0; virtual void getString(CString* outStr) = 0; }; 

Now you need to create a C ++ / CLI DLL that will allow you to mix your own and managed code in one assembly. Add a new C ++ project, and in the project configuration, set "Common Language Runtime Support" for the Mixed (/ clr) parameter.

Once you have added a link to your C # library (which we will call ManagedLibrary), we can implement the interop class:

interopclass.cpp

 #include "interopclassbase.h" #include <vcclr.h> public class InteropClass : public InteropClassBase { protected: gcroot<ManagedLibrary::ManagedObject^> m_managedObject; public: InteropClass() { m_managedObject = gcnew ManagedLibrary::ManagedObject(); } virtual void doStuff() { m_managedObject->doStuff(); } virtual int getValue() { return m_managedObject->getValue(); } virtual void getString(CString* pOutStr) { System::String^ managedString = m_managedObject->getString(); CString nativeString(managedString); // overloaded CString constructor if (pOutStr) *pOutStr = nativeString; } }; __declspec(dllexport) InteropClassBase* getImplementationPointer() { return new InteropClass(); } 

Now you just need to load the DLL interaction from your own project and call the exported function.

+8
source

One solution is COM Interop. Perhaps the only solution. This is a big topic. There's a big fat blue book at work that I use. Hundreds of pages are nothing but COM Interop.

In the short version, you mark some classes and interfaces on the managed side and create interop assemblies that look like COM DLLs, but really are proxies for managed assemblies. Interop collections are registered as COM dlls to and from you.

MSDN has a lot of information about this.

This is probably a good starting point. "Providing .NET Framework Components for COM" http://msdn.microsoft.com/en-us/library/aa720072%28VS.71%29.aspx

It may be surprisingly easy, but try to keep it simple.

+3
source

To create a managed object and call its methods, you need the CLR to work in your C ++ process.

In windows, you can place the CLR by referencing mscoree.dll and placing the CLR in the 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 in your C ++ application.

http://www.mono-project.com/Embedding_Mono

0
source

Source: https://habr.com/ru/post/1304131/


All Articles