public static unsafe void ProcessDropFiles(ref WindowMessage msg, MainWindow window) { fixed(WindowMessage *ptr = &msg) { var packet = new DropFilesPacket(ptr); window.OnDropFiles(ref packet); } }
public void OnDropFiles(ref DropFilesPacket packet) { IntPtr hDrop = packet.HDrop; try { uint count = Shell32.DragQueryFileW(hDrop, 0xFFFFFFFF, IntPtr.Zero, 0); var paths = new string[(int)count]; for (uint i = 0; i < count; i++) { uint size = Shell32.DragQueryFileW(hDrop, i, IntPtr.Zero, 0); char[] chars = ArrayPool <char> .Shared.Rent((int)(size + 1)); try { GCHandle handle = GCHandle.Alloc(chars, GCHandleType.Pinned); try { Shell32.DragQueryFileW(hDrop, i, handle.AddrOfPinnedObject(), size + 1); paths[i] = new string(chars.AsSpan(0, (int)size)); } finally { if (handle.IsAllocated) { handle.Free(); } } } finally { ArrayPool <char> .Shared.Return(chars); } } OnFilesDropped(paths); } finally { Shell32.DragFinish(hDrop); } }