I've had some problems passing string as PChar to Delphi built DLL, and resolved it thanks to Jens Mühlenhoff.
Now I have another issue -
I've made successful callback of c# method when passed to DLL if the Delphi declaration is a regular type procedure, but if Delphi declaration is a method type procedure I get "Attempted to read or write protected memory" error.
I tried searching...
Here is Delphi declaration
TCallBack = procedure ( s : String) of object;stdcall;
Here is C# code
[DllImport(
"DLLTest.dll",
CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Ansi,
EntryPoint = "DLL_Test"
)]
public static extern void DLL_Test(IntPtr p, [MarshalAs(UnmanagedType.LPStr)] string Location, int AIntValue);
public delegate void MethodCallBackEvent(string s);
public event MethodCallBackEvent Info;
public void GetInfo(string s)
{
MessageBox.Show("Info: " + s);
}
used as
Info = GetInfo; //or Info = new MethodCallBackEvent(GetInfo);
IntPtr p = Marshal.GetFunctionPointerForDelegate(Info);
DLL_Test(p, "location message", 10);
Here is a working example. DllTest1 is using a normal function callback. DllTest2 expects the callback as a direct C# function pointer (requires a small hack on the Delphi side), and DllTest3 expects a Delphi method callback pointer (requires a small hack on the C# side).