I've got a project where I need to create a C# project and a Delphi DLL, in
which the DLL has a callback function to send statuses back to the C#
application. It seems pretty straightforward in a MSDN example in which it
explains the use of delegates
(http://msdn2.microsoft.com/en-us/library/843s5s5x.aspx), but for some
reason I get an AccessViolationException when the callback function is
called. Anyone have any ideas? Here's the Delphi DLL:
library Project1;
uses
SysUtils,
Classes;
{$R *.res}
type
TTestProc = procedure(TestInt: integer) of object;
function TestCallBack(CallBack: TTestProc): boolean; stdcall;
begin
CallBack(12345);
Result := True;
end;
exports
TestCallBack;
end.
Then the C# project:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private delegate void CallBack(int testParam);
[DllImport("N:\\Temp\\Project1.dll")]
private static extern bool TestCallBack(CallBack x);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CallBack myCallBack = new CallBack(Form1.Report);
TestCallBack(myCallBack); //<-- AccessViolationException here
}
private static void Report(int testParam)
{
MessageBox.Show(testParam.ToString());
}
}
}
--
Thanks,
Jon E. Scott
Blue Orb Software
http://www.blueorbsoft.com