467,877 Members | 1,263 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,877 developers. It's quick & easy.

Delphi PChar and C#

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


Jun 27 '08 #1
  • viewed: 9225
Share:
1 Reply
Jure Bogataj wrote:
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.

--
Rudy Velthuis http://rvelthuis.de

"A narcissist is someone better looking than you are."
- Gore Vidal
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Tim Conner | last post: by
4 posts views Thread by erdosa | last post: by
1 post views Thread by denis | last post: by
reply views Thread by Ivan | last post: by
1 post views Thread by =?Utf-8?B?U2hhdW5P?= | last post: by
5 posts views Thread by kelvin.koogan | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.