/// <summary> /// Parses the HDROP format and returns a list of strings using /// the DragQueryFile function. /// </summary> private string[]? ReadFileListFromHandle(IntPtr hdrop) { uint count = Shell32.DragQueryFileW(hdrop, 0xFFFFFFFF, null); if (count == 0) { return(null); } var sb = new StringBuilder(Kernel32.MAX_PATH); var files = new string[count]; for (uint i = 0; i < count; i++) { uint charlen = Shell32.DragQueryFileW(hdrop, i, sb); if (charlen == 0) { continue; } string s = sb.ToString(0, (int)charlen); files[i] = s; } return(files); }
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); } }