0

我有一个非托管 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");
    }

}

我错过了什么吗?

4

0 回答 0