简体   繁体   中英

How to pass c# string to delphi .dll PChar type?

So I learned using IntPtr in this case,

-delphi (delphi 2006 version) code

 function GetRequestResult(out code:integer):PChar; stdcall;
   begin
    LogMessage('GetRequestResult');
    code:=requestCode;
    result:=PChar(RequestResult);
    LogMessage('GetRequestResult done');
   end;

then, for using in c#, I used like,

IntPtr str = GetRequestResult(out code); 
string loginResult = Marshal.PtrToStringAnsi(str); 

and this works well.

Then, how about this case?

-delphi code

 procedure Login(login,password:PChar); stdcall;
...
...

this PChar is inside (). So what is exact way to pass string value to that Login delphi function?

[DllImport ("ServerTool")]

  1. private static extern void Login([MarshalAs(UnmanagedType.LPStr)]string id, [MarshalAs(UnmanagedType.LPStr)]string pass);

  2. private static extern void Login(IntPtr id, IntPtr pass); // in this case, how use this IntPtr in latter part?

  3. private static extern void Login(string id, string pass);

Thanks in advance.

Your option 3 is the correct way to do this. The p/invoke marshaller will map a C# string to a pointer to null terminated character array, ie PChar. The default calling convention is stdcall, and the default character set is Ansi and so you don't need to specify those.

Your option 1 works also but is unnecessarily verbose since you are just re-stating the default marshalling. Option 2 could work also but you'd just be creating extra work for yourself. You'd have the responsibility of freeing the IntPtr values once the native function call returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM