简体   繁体   中英

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

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)

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

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

#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 interop DLL from your native project and call the exported function.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM