Delphi PChar and C#

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
 
R

Rudy Velthuis

Jure said:
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;

Problem is that the string buffer is probably also allocated by the
DLL. This means you'll have to keep the pointer around as an IntPtr,
since marshalling will create new strings, and you don't want the DLL
to deallocate a buffer allocated by the marshaller. This means you
can't easily access the string as such, only pass it around to other
functions of the DLL.

If you have control over the DLL, it is better not to let the DLL
allocate or deallocate strings at all, just to let them fill buffers
allocated by the client (or the marhsller, in this time).

The second function is probably best declared as:

[DllImport("nameOfDLL", CharSet = CharSet.Ansi,
EntryPoint = "MyFunc1")]
public static extern uint MyFunc1(string lpszInput,
ref StringBuilder lpszOutput);

Now the marshaller also doesn't know the size of the second buffer, but
I guess you'll have to handle that, by passing a StringBuilder of a
suitable size.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top