How to access c ++ dll class in c # code

I have a C ++ class in my third party dll.

If I call Assembly.LoadFrom (), VS throws an unhandled exception because there is no manifest in the module.

I can call global functions using DllImport to get an instance of a specific class.

How can I then call one of its member functions?

+3
source share
2 answers

Create a wrapper DLL with C ++ / CLI, exposing C ++ functions

eg:

//class in the 3rd party dll
class NativeClass
{
    public:
    int NativeMethod(int a)
    {
        return 1;
    }   
};

//wrapper for the NativeClass
class ref RefClass
{
    NativeClass * m_pNative;

    public:
    RefClass():m_pNative(NULL)
    {
        m_pNative = new NativeClass();
    }

    int WrapperForNativeMethod(int a)
    {
        return m_pNative->NativeMethod(a);
    }

    ~RefClass()
    {
        this->!RefClass();
    }

    //Finalizer
    !RefClass()
    {
        delete m_pNative;
        m_pNative = NULL;
    }
};
+2
source

Assembly.LoadFrom is used to load a managed assembly.

Unmanaged builds require P / Invoke .

Like a Marshal C ++ class

+1
source

All Articles