2

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?

1

2 Answers 2

2

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;
    }
};
Sign up to request clarification or add additional context in comments.

Comments

1

Assembly.LoadFrom is used to load managed assembly.

For unmanaged assemblies P/Invoke is required.

How to Marshal c++ class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.