user1398822
user1398822

Reputation: 21

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

I have a C++ class in my 3rd party dll.

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

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

How do I then call one of its member functions?

Upvotes: 2

Views: 926

Answers (2)

Indy9000
Indy9000

Reputation: 8881

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

for example:

//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;
    }
};

Upvotes: 2

Tilak
Tilak

Reputation: 30728

Assembly.LoadFrom is used to load managed assembly.

For unmanaged assemblies P/Invoke is required.

How to Marshal c++ class

Upvotes: 1

Related Questions