c# - PInvoke & Delphi -
how can use dll function in c#? tried following error. "external component has thrown exception."
first time doing pinvoke stuff c# , delphi.
function httpget(location:string):string; stdcall; var http:tidhttp; begin http := tidhttp.create(nil); try result := http.get(location); freeandnil(http); end; end; exports httpget; begin end. namespace test { class program { [dllimport("project1.dll")] public static extern string httpget(string location); static void main(string[] args) { console.writeline(httpget("http://www.reuters.com/")); } } }
you cannot call function c#. that's because cannot use delphi string
interop. can use pansichar
strings passed managed unmanaged, in other direction it's more complex. you'd need allocate memory @ caller, or use shared heap. prefer latter approach easiest done com bstr
. widestring
in delphi.
as has been discussed before, cannot use widestring
return value interop, since delphi uses different abi ms tools return values.
the delphi code needs this:
procedure httpget(url: pansichar; out result: widestring); stdcall;
on c# side write this:
[dllimport("project1.dll")] public static extern void httpget( string url, [marshalas(unmanagedtype.bstr)] out string result );
if want unicode url use pwidechar
, charset.unicode
.
procedure httpget(url: pwidechar; out result: widestring); stdcall; .... [dllimport("project1.dll", charset=charset.unicode)] public static extern void httpget( string url, [marshalas(unmanagedtype.bstr)] out string result );
Comments
Post a Comment