问题
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