Hello!
Does anybody knows how to handle this issue:
I have an Delphi DLL with following two function declaration:
function DeallocateStrin g(lpszString : PChar) : DWORD; stdcall;
function MyFunc1(lpszInp ut : PChar; var lpszOutput : PChar) : DWORD;
stdcall;
This function (MyFunc1) processes lpszInput and stores the result in
lpszOutput using AllocMem function in Delphi to allocate memory (delphi's
own memory function; must be released by calling FreeMem, also using Delphi
code). DeallocateStrin g function's solely purpose is to destroy PChar
(string) create by delphi's AllocMem function (it only calls FreeMem and
that's it!).
I have made a function prototypes in C#:
[DllImport("myli b.dll", EntryPoint = "DeallocateStri ngA", CharSet =
CharSet.Ansi, ExactSpelling = true, CallingConventi on =
CallingConventi on.Winapi)]
public static extern uint DeallocateStrin g(string lpszString);
[DllImport("myli b.dll", EntryPoint = "MyFunc1A", CharSet = CharSet.Ansi,
ExactSpelling = true, CallingConventi on = CallingConventi on.Winapi)]
public static extern uint MyFunc1(string lpszInputText, ref string
lpszOutputValue );
MyFunc1 is working as expected (it returns the right string into
lpszOutputValue parameter). Here's the test code:
string myvalue = "";
uint res = DLLWrapper.MyFu nc1("mystring", ref myvalue);
MessageBox.Show (myvalue); // This is OK
DLLWrapper.Deal locateString(my value); // Here I get an exception: "External
component has thrown an exception."
Am I doing this right? How does C# pass string parameters, whereas Delphi
expects it to be PChar. Delphi then tries to free this string, but obviously
it cannot free it for some reason. Must I use something else or declare
function differently. I'm wondering in the dark here, so any help would be
greatly appreciated. Since C# does not support pointer type, is this code at
all possible?
Thanks in advance!
Best regards,
Jure