nvx/apertureAll write-ups
No. 01ENGINEERING · 8 min

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 Cyber 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.

The drop, so far
  1. 01

    Version one: inject a press, run DoDragDrop on a worker thread

    Chrome selected text instead of dragging. The release did nothing. When it wedged, the desktop stayed frozen until Task Manager ended the process.

    Failed
  2. 02

    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.

    Shelved
  3. 03

    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.

    Shipped

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 DeskflowWhat I wanted
fakeDraggingFilesOn 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 releaseThe 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 landsUsually 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

InterfaceBelongs toWhat it does
IDataObjectThe payloadFor files, the CF_HDROP format, a list of paths.
IDropSourceWhoever started the dragQueryContinueDrag is asked on every tick whether to keep going, drop or cancel. GiveFeedback sets the cursor.
IDropTargetAny window that accepts dropsRegistered privately with RegisterDragDrop. The source never sees it.
What DoDragDrop does5 stages
  1. 01

    Hidden tracker window

    Created by DoDragDrop

  2. 02

    SetCapture

    Takes the mouse and holds it for as long as the loop runs

  3. 03

    GetMessage loop

    Its own message loop

  4. 04

    Each mouse move

    WindowFromPoint, up to the nearest registered target, then DragEnter or DragOver

  5. 05

    Button-up

    Drop on whatever is under the cursor, then it returns

Drag and drop on Windows is OLE, built on the three COM interfaces above. 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

Version one: inject a press, then drag5 stages
  1. 01

    Cursor crosses over

    Onto the second machine, carrying a file

  2. 02

    Inject a left button-down

    With SendInput

  3. 03

    DoDragDrop

    On a background thread, with my own data object

  4. 04

    Forward the mouse moves

    Into the drag

  5. 05

    Inject a button-up

    When the button comes up on the source machine, to finish the drop

This looked like the obvious way to start a drag on the second machine.

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

Why a stuck drag looks like a hung machine5 steps
  1. 01Input stops reaching the modal loop. It waits indefinitely, still holding the mouse. DoDragDrop loop: waiting for input; Mouse capture: held by the loop.
  2. 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.
  3. 03QueryContinueDrag only 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.
  4. 04On Windows a loop that holds the mouse and never returns looks exactly like a hung machine. Mouse capture: desktop frozen.
  5. 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

Version two: skip OLE and copy4 stages
  1. 01

    Send the file

  2. 02

    Draw a thumbnail

    It follows the cursor

  3. 03

    On release

    Find the folder under the cursor

  4. 04

    Copy into it

    With the shell. No modal loop, no mouse capture, so nothing left that could hang

By this point giving up on OLE looked like the sensible move. I built it, and it worked for Explorer folders and the Desktop.
Shell copy against an OLE drop
Shell copyOLE 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 desktopnothing to hangholds 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.

SequenceVersion one: the press lands on Chrome
nvx_kinetixInjects input
ChromeUI thread
WorkerMy thread
OLE trackerInside DoDragDrop
  1. 01SendInput button-down arrives as WM_LBUTTONDOWN
  2. 02Takes the implicit mouse capture
  3. 03DoDragDrop(dataObject, dropSource, ...)
  4. 04SetCapture refused: another process has the mouse
  5. 05Forwarded mouse moves
  6. 06A press, then movement: an ordinary click-drag, so it selects text
  7. 07GetMessage waits for input that never comes
  1. 01nvx_kinetixChromeSendInput button-down arrives as WM_LBUTTONDOWN
  2. 02ChromeTakes the implicit mouse capture
  3. 03WorkerOLE trackerDoDragDrop(dataObject, dropSource, ...)
  4. 04OLE trackerSetCapture refused: another process has the mouse
  5. 05nvx_kinetixChromeForwarded mouse moves
  6. 06ChromeA press, then movement: an ordinary click-drag, so it selects text
  7. 07OLE trackerGetMessage waits 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.

press handler, simplified
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}
  1. 1L3DoDragDrop hit-tests with WindowFromPoint on every move. Left under the cursor, this window is the only thing it would ever find, and the window underneath would never get DragEnter.
  2. 2L4Moved, not hidden. Hiding a window that holds the mouse capture releases the capture.
  3. 3L7Same thread that received the press, so SetCapture inside the tracker succeeds. Blocks until the drop.
SequenceVersion three: the press lands on my own window
nvx_kinetixInjects input
Alpha-1 windowMy thread
OLE trackerSame thread
Drop targetFolder, Desktop or app
  1. 01SendInput button-down arrives as WM_LBUTTONDOWN
  2. 02The implicit capture belongs to my thread
  3. 03Moves itself to -32000, -32000
  4. 04DoDragDrop on the same thread
  5. 05SetCapture succeeds
  6. 06Forwarded mouse moves
  7. 07DragEnter, then DragOver, and the target draws its own highlight
  8. 08Forwarded release
  9. 09Drop
  1. 01nvx_kinetixAlpha-1 windowSendInput button-down arrives as WM_LBUTTONDOWN
  2. 02Alpha-1 windowThe implicit capture belongs to my thread
  3. 03Alpha-1 windowMoves itself to -32000, -32000
  4. 04Alpha-1 windowOLE trackerDoDragDrop on the same thread
  5. 05OLE trackerSetCapture succeeds
  6. 06nvx_kinetixOLE trackerForwarded mouse moves
  7. 07OLE trackerDrop targetDragEnter, then DragOver, and the target draws its own highlight
  8. 08nvx_kinetixOLE trackerForwarded release
  9. 09OLE trackerDrop targetDrop

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.

The source machine, in reverse4 stages
  1. 01

    Edge strip

    Thin, invisible, registered as an IDropTarget

  2. 02

    Cursor carries a drag across it

  3. 03

    Strip reads the CF_HDROP

  4. 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.

Cancelling the source machine's drag

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

CaseWhat goes wrongWhat the code does
Scaled displayDoDragDrop 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 dragA 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.

No comments yet

Comments are read before they appear.