This is a bit dirty P / Invoke, because you cannot (at best from my admittedly limited knowledge) use any of the built-in simple sorting methods. Instead, you need to use Marshal.StructureToPtr as follows:
WITH#
[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(); for (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); } finally { 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); for i := 0 to Count-1 do begin name[i] := Items.Name; ID[i] := Items.ID inc(Items); end; TestFLASHWNew(name, ID, d1); end;
I implemented it with a wrapper function that calls your TestFLASHWNew function, but you will certainly want to re-process it.
I assumed that you are using Delphi with Unicode strings. If not, change [MarshalAs(UnmanagedType.LPWStr)] to [MarshalAs(UnmanagedType.LPStr)] .
source share