純粋な仮想メソッドのみを持つクラス (コールバックの種類) を含むアンマネージ 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");
}
}
私は何かが恋しいですか?