Wrapping class with pure virtual methods

天涯浪子 提交于 2019-12-08 10:25:09

问题


I have an unmanaged dll which contains a class with pure virtual methods only (kind of callbacks):

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

Now I have to send these function calls to the managed C# code and decided to use interface for that. This is what I've done:

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();
    }
}

But every time the unmanaged SetCalculationStarted() is called, the unmanaged code throws an exception:

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.

EDIT:

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

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

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

}

Do I miss something?

来源:https://stackoverflow.com/questions/14707240/wrapping-class-with-pure-virtual-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!