Dragging a file between PCs froze my desktop
Moving the cursor between two Windows PCs was the easy part. Getting a dragged file to drop into the folder under the cursor on the other machine meant learning which thread Windows lets hold the mouse, after one version froze my desktop.
By Ojas KekreCyber security, Monash University
The first version of file drag in nvx_kinetix froze my desktop until I ended the process from Task Manager. nvx_kinetix is the KVM I'm building because I kept bouncing off the ones that exist: one keyboard and mouse across several Windows machines, meant to hold up next to ShareMouse, Synergy and Deskflow.
Moving the cursor across a screen edge is the part every tool already does, several of them for free. File drag took me longer than everything else in the project put together, and most of that time is below.
- 01Failed
Version one: inject a press, run
DoDragDropon a worker threadChrome selected text instead of dragging. The release did nothing. When it wedged, the desktop stayed frozen until Task Manager ended the process.
- 02Shelved
Version two: skip OLE and copy into whatever folder is under the cursor
Worked for Explorer folders and the Desktop. Can never drop into a browser or an app.
- 03Shipped
Version three: take the press on a window I own
An OLE drop into whatever sits under the cursor, with that app's own highlight.
An empty function called `fakeDraggingFiles`
Synergy, Barrier and Deskflow come from the same codebase, and all three contain a function named fakeDraggingFiles. It is where the receiving machine would perform the drop.
| Synergy, Barrier and Deskflow | What I wanted | |
|---|---|---|
fakeDraggingFiles | On Windows the body is empty, and the comment above it calls this "a possible design flaw". | The receiving machine performs the drop itself. |
| What happens on release | The dragged file's path is read on the source machine, the bytes are sent over, and the file is written into a fixed folder on the target. | The file goes to whatever is under the cursor on the second machine. |
| Where the file lands | Usually the Desktop, through CSIDL_DESKTOP. You let go over a project folder, the file turns up on the Desktop, and you pick it up again and drag it where you meant. | An Explorer folder, the Desktop, a browser or any other app that accepts files, on whichever monitor the cursor is on. That app draws its own drop highlight. |
ShareMouse drops files properly, and you pay for it. I wanted that behaviour in an open-core tool.
How Windows decides where a drop goes
| Interface | Belongs to | What it does |
|---|---|---|
IDataObject | The payload | For files, the CF_HDROP format, a list of paths. |
IDropSource | Whoever started the drag | QueryContinueDrag is asked on every tick whether to keep going, drop or cancel. GiveFeedback sets the cursor. |
IDropTarget | Any window that accepts drops | Registered privately with RegisterDragDrop. The source never sees it. |
- 01
Hidden tracker window
Created by
DoDragDrop - 02
SetCaptureTakes the mouse and holds it for as long as the loop runs
- 03
GetMessageloopIts own message loop
- 04
Each mouse move
WindowFromPoint, up to the nearest registered target, thenDragEnterorDragOver - 05
Button-up
Dropon whatever is under the cursor, then it returns
DoDragDrop ties them together, and it does not return until the drag is over.Two details in that loop matter for everything below. It holds the mouse capture for as long as it runs, and the drop goes to the window under the cursor, never to the code that called it.
Faking the press
- 01
Cursor crosses over
Onto the second machine, carrying a file
- 02
Inject a left button-down
With
SendInput - 03
DoDragDropOn a background thread, with my own data object
- 04
Forward the mouse moves
Into the drag
- 05
Inject a button-up
When the button comes up on the source machine, to finish the drop
On hardware, dragging over a browser selected text, and the release did not drop anything. Then the drag wedged and the whole desktop stopped responding.
A cancel flag the loop never read
- 01Input stops reaching the modal loop. It waits indefinitely, still holding the mouse. DoDragDrop loop: waiting for input; Mouse capture: held by the loop.
- 02wd to fl: Set. I had a safety net: a cancel flag checked inside
QueryContinueDrag. My watchdog set it thousands of times a second. Cancel flag: set, thousands/s. - 03
QueryContinueDragonly runs when input reaches the loop, which was the thing not happening. The loop never once looked at the flag. DoDragDrop loop: never reads the flag. - 04On Windows a loop that holds the mouse and never returns looks exactly like a hung machine. Mouse capture: desktop frozen.
- 05wd to lp: Mouse move or Escape. More input does get through. An injected mouse move reaches the captured tracker window no matter which window has focus, and so does an injected Escape. Either one ends the loop. Cancel flag: cleared; DoDragDrop loop: loop ends; Mouse capture: cleared.
That explained the freeze. The text selection was still unexplained, and it was the more useful clue.
The version that cannot freeze
- 01
Send the file
- 02
Draw a thumbnail
It follows the cursor
- 03
On release
Find the folder under the cursor
- 04
Copy into it
With the shell. No modal loop, no mouse capture, so nothing left that could hang
| Shell copy | OLE drop | |
|---|---|---|
| Explorer folder under the cursor | ||
| The Desktop | ||
| A browser | ||
| Any other app that accepts files | ||
| The target draws its own highlight | ||
| Risk of hanging the desktop | nothing to hang | holds the mouse while it runs |
The browser and app rows were the ones I could not live with. Only an OLE drop can land there, so shipping the copy version meant shipping Synergy's workaround with a nicer thumbnail. It went in a drawer, and I went back to the text selection.
Who got the click
Windows only lets certain threads take the mouse capture. A thread becomes eligible when a mouse button goes down over one of its own windows, and it keeps an implicit capture until that button comes back up. The tracker window in DoDragDrop can only take the capture on an eligible thread.
- 01
SendInputbutton-down arrives asWM_LBUTTONDOWN - 02Takes the implicit mouse capture
- 03
DoDragDrop(dataObject, dropSource, ...) - 04
SetCapturerefused: another process has the mouse - 05Forwarded mouse moves
- 06A press, then movement: an ordinary click-drag, so it selects text
- 07
GetMessagewaits for input that never comes
- 01nvx_kinetixChrome
SendInputbutton-down arrives asWM_LBUTTONDOWN - 02ChromeTakes the implicit mouse capture
- 03WorkerOLE tracker
DoDragDrop(dataObject, dropSource, ...) - 04OLE tracker
SetCapturerefused: another process has the mouse - 05nvx_kinetixChromeForwarded mouse moves
- 06ChromeA press, then movement: an ordinary click-drag, so it selects text
- 07OLE tracker
GetMessagewaits for input that never comes
My injected press went to whatever was under the cursor, which was Chrome. Every input event went to Chrome, and the drag's loop started on a thread with no claim to the mouse.
A window at alpha 1
So the press has to land on a window I own. When the cursor arrives on the target machine with a file pending, nvx_kinetix puts a tiny top-most window underneath it, layered at alpha 1.
At alpha 1 nobody can see it, but it is still fully hit-testable. A transparent click-through window would be skipped by the hit test, and being hit is its only job.
The injected button-down lands on it, as the top-most thing under the cursor, so my thread now holds the implicit capture. The rest happens synchronously, inside that message handler.
1case WM_LBUTTONDOWN:
2{
3 SetWindowPos(hwnd, nullptr, -32000, -32000, 0, 0,
4 SWP_NOSIZE | SWP_NOZORDER);
5
6 DWORD effect = DROPEFFECT_NONE;
7 DoDragDrop(dataObject, dropSource, DROPEFFECT_COPY, &effect);
8 return 0;
9}- 1L3
DoDragDrophit-tests withWindowFromPointon every move. Left under the cursor, this window is the only thing it would ever find, and the window underneath would never getDragEnter. - 2L4Moved, not hidden. Hiding a window that holds the mouse capture releases the capture.
- 3L7Same thread that received the press, so
SetCaptureinside the tracker succeeds. Blocks until the drop.
- 01
SendInputbutton-down arrives asWM_LBUTTONDOWN - 02The implicit capture belongs to my thread
- 03Moves itself to -32000, -32000
- 04
DoDragDropon the same thread - 05
SetCapturesucceeds - 06Forwarded mouse moves
- 07
DragEnter, thenDragOver, and the target draws its own highlight - 08Forwarded release
- 09
Drop
- 01nvx_kinetixAlpha-1 window
SendInputbutton-down arrives asWM_LBUTTONDOWN - 02Alpha-1 windowThe implicit capture belongs to my thread
- 03Alpha-1 windowMoves itself to -32000, -32000
- 04Alpha-1 windowOLE tracker
DoDragDropon the same thread - 05OLE tracker
SetCapturesucceeds - 06nvx_kinetixOLE trackerForwarded mouse moves
- 07OLE trackerDrop target
DragEnter, thenDragOver, and the target draws its own highlight - 08nvx_kinetixOLE trackerForwarded release
- 09OLE trackerDrop target
Drop
After that first press the little window has nothing left to do. Forwarded moves drive DragOver on the window under the cursor, and the forwarded release drops the file into the folder, the Desktop or the browser beneath it.
- 01
Edge strip
Thin, invisible, registered as an
IDropTarget - 02
Cursor carries a drag across it
- 03
Strip reads the
CF_HDROP - 04
File list sent
To the other machine
Escape, not a button-up
One more bug kept eating drops. After the edge strip reads the file list, the source machine's own drag has to be cancelled so the file does not also drop there. The user is still physically holding the button at that moment.
Ends DoDragDrop without dropping and leaves the button down, so the physical release survives long enough to reach the other machine.
Scaled displays and shutdown
| Case | What goes wrong | What the code does |
|---|---|---|
| Scaled display | DoDragDrop tracks the cursor in virtualized coordinates and the drop lands on the wrong monitor. | The drag-source thread declares PER_MONITOR_AWARE_V2. Every thread that deals in physical pixels has to agree: capture, injection, the edge strip, the drag source and the thumbnail overlay. |
| Shutdown with a stuck drag | A stuck drag must not stop the app from quitting. | nvx_kinetix requests a cancel, then detaches the drag thread instead of joining it. Process teardown kills the thread and releases any capture it held. |
Dragging from one PC into Chrome on another
Drag a file off the edge of one machine, let go over a Chrome window on the other, and it drops into Chrome. The same goes for an Explorer folder, the Desktop, or any app that accepts files, on any monitor, with the target's own highlight, driven by the physical mouse the whole way.
I have not found another open-core tool in this category that does this on Windows.
The file bytes travel over QUIC with device-key pinning, which needs a write-up of its own (part two).
Admin windows
One case still does not work. Windows UIPI blocks synthetic input from a medium-integrity process into a higher-integrity window, so the injected press and release never reach an app running as administrator. Running nvx_kinetix with administrator rights gets around it, and every tool in this space runs into the same wall.
Next on the list: baking a drop-description badge into the drag image, and letting drops target folders in Explorer's navigation pane.