使用纯虚方法包装类

时间:2013-02-05 12:10:38

标签: c# c++ c++-cli wrapper wrapping

我有一个非托管dll,它包含一个只有纯虚方法的类(回调类型):

class PAClient
{
public:
    __declspec(dllexport) virtual void SetCalculationStarted() = 0;     
    __declspec(dllexport) virtual void SetCalculationStopped() = 0; 
}

现在我必须将这些函数调用发送到托管C#代码,并决定使用接口。这就是我所做的:

public interface class IPAClientWrapper
{
    void SetCalculationStarted();       
    void SetCalculationStopped();   
};

private class PAClientWrapper : public PAClient
{
private:
    gcroot<IPAClientWrapper^> callBack;

public:

    PAClientWrapper(IPAClientWrapper^ c)
    {
        callBack = c;
    }

    void SetCalculationStarted()
    {
        callBack->SetCalculationStarted();
    }

    void SetCalculationStopped()
    {
        callBack->SetCalculationStopped();
    }
}

但每次调用非托管 SetCalculationStarted()时,非托管代码都会抛出异常:

An unhandled exception of type 'System.AccessViolationException' occurred in PAnalysisLib.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

修改

public partial class Form1 : Form, IPAClientWrapper
{
    public Form1()
    {
        InitializeComponent();
    }

    public void SetCalculationStarted()
    {
        Console.WriteLine("started");
    }

    public void SetCalculationStopped()
    {
        Console.WriteLine("stopped");
    }

}

我错过了什么吗?

0 个答案:

没有答案