3

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?

4

2 回答 2

2

使用暴露 C++ 函数的 C++/CLI 创建包装 DLL

例如:

//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;
    }
};
于 2012-05-16T16:09:25.630 回答
1

Assembly.LoadFrom 用于加载托管程序集。

对于非托管程序集,需要P/Invoke

如何编组 c++ 类

于 2012-05-16T14:56:18.350 回答