J
Jure Bogataj
Hello!
Does anybody knows how to handle this issue:
I have an Delphi DLL with following two function declaration:
function DeallocateString(lpszString : PChar) : DWORD; stdcall;
function MyFunc1(lpszInput : 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). DeallocateString 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("mylib.dll", EntryPoint = "DeallocateStringA", CharSet =
CharSet.Ansi, ExactSpelling = true, CallingConvention =
CallingConvention.Winapi)]
public static extern uint DeallocateString(string lpszString);
[DllImport("mylib.dll", EntryPoint = "MyFunc1A", CharSet = CharSet.Ansi,
ExactSpelling = true, CallingConvention = CallingConvention.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.MyFunc1("mystring", ref myvalue);
MessageBox.Show(myvalue); // This is OK
DLLWrapper.DeallocateString(myvalue); // 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
Does anybody knows how to handle this issue:
I have an Delphi DLL with following two function declaration:
function DeallocateString(lpszString : PChar) : DWORD; stdcall;
function MyFunc1(lpszInput : 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). DeallocateString 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("mylib.dll", EntryPoint = "DeallocateStringA", CharSet =
CharSet.Ansi, ExactSpelling = true, CallingConvention =
CallingConvention.Winapi)]
public static extern uint DeallocateString(string lpszString);
[DllImport("mylib.dll", EntryPoint = "MyFunc1A", CharSet = CharSet.Ansi,
ExactSpelling = true, CallingConvention = CallingConvention.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.MyFunc1("mystring", ref myvalue);
MessageBox.Show(myvalue); // This is OK
DLLWrapper.DeallocateString(myvalue); // 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