how to call this function in a delphi dll from C# -
i have function defined in delphi code:
procedure testflashwnew( name: array of string; id: array of integer; var d1:double ); stdcall;
how can define , call c#?
this bit of messy p/invoke because can't (to best of admittedly limited knowledge) use of built-in easy marshalling techniques. instead need use marshal.structuretoptr
this:
c#
[structlayout(layoutkind.sequential)] public struct myitem { [marshalas(unmanagedtype.lpwstr)] public string name; public int id; } [dllimport(@"mydll.dll")] private static extern void testflashwnewwrapper(intptr items, int count, ref double d1); static void main(string[] args) { myitem[] items = new myitem[3]; items[0].name = "jfk"; items[0].id = 35; items[1].name = "lbj"; items[1].id = 36; items[2].name = "tricky dicky"; items[2].id = 37; intptr itemsptr = marshal.allochglobal(marshal.sizeof(typeof(myitem))*items.length); try { int32 addr = itemsptr.toint32(); (int i=0; i<items.length; i++) { marshal.structuretoptr(items[i], new intptr(addr), false); addr += marshal.sizeof(typeof(myitem)); } double d1 = 666.0; testflashwnewwrapper(itemsptr, items.length, ref d1); console.writeline(d1); } { marshal.freehglobal(itemsptr); } }
delphi
titem = record name: pchar; id: integer; end; pitem = ^titem; procedure testflashwnewwrapper(items: pitem; count: integer; var d1: double); stdcall; var i: integer; name: array of string; id: array of integer; begin setlength(name, count); setlength(id, count); := 0 count-1 begin name[i] := items.name; id[i] := items.id inc(items); end; testflashwnew(name, id, d1); end;
i've implemented wrapper function calls testflashwnew
function you'll no doubt want re-work it.
i've assumed using delphi unicode strings. if not change [marshalas(unmanagedtype.lpwstr)]
[marshalas(unmanagedtype.lpstr)]
.
Comments
Post a Comment