Delphi PChar and C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jure Bogataj

    Delphi PChar and C#

    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






  • Rudy Velthuis

    #2
    Re: Delphi PChar and C#

    Jure Bogataj wrote:
    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;
    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("name OfDLL", 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

    Comment

    Working...