Files
IronRDP/ffi/dotnet
Marc-André Moreau b9d1400033 feat(cliprdr): transfer files through the Windows clipboard (#2179)
Adds clipboard file transfer, in both directions, to the Windows
clipboard backend (`WinClipboard`), and stops it advertising formats it
can't deliver. ActiveX and `ironrdp-client` get the feature as is; the
.NET FFI gets it through new bindings, opt-in.

## Backend (`feat(cliprdr)`)

**Remote to local.** When the remote format list has
`FileGroupDescriptorW`, an OLE data object goes on the clipboard
(`OleSetClipboard`). It offers `FileGroupDescriptorW` and one
`FileContents` `IStream` per file (`lindex`), plus the other remote
formats.
- Streams read through `FileContents` RANGE requests carrying the
`clipDataId` of the lock `Cliprdr` took for that list, so a paste keeps
working after the remote clipboard changes, until the lock is released.
Files over 4 GiB work.
- OLE needs an STA: this is only enabled when `WinClipboard` is created
on one. COM is never initialized on a thread that has none.
- The objects are not agile and run on the clipboard thread. A stream
read blocks it like `WM_RENDERFORMAT` handling already does (30 s
timeout, chunks up to 1 MiB with read-ahead). Backend events arriving
during a wait are now queued instead of dropped.

**Local to remote.** `CF_HDROP` paths (directories walked into relative
paths), or another application's virtual files (`FileGroupDescriptorW` +
`FileContents`), are offered with `SendInitiateFileCopy` once the
channel is ready. The remote's SIZE/RANGE requests are served, from the
lock snapshot when a `clipDataId` is given.

**Capabilities.** The backend now negotiates `STREAM_FILECLIP_ENABLED`,
`FILECLIP_NO_FILE_PATHS`, `CAN_LOCK_CLIPDATA` and
`HUGE_FILE_SUPPORT_ENABLED`.

**Format filtering.** Handle-based formats (`CF_BITMAP`,
`CF_ENHMETAFILE`, `CF_METAFILEPICT`, `CF_PALETTE`), private ranges,
local paths and shell items (`CF_HDROP`, `FileNameW`, `Shell IDList
Array`) and OLE storage formats are no longer advertised in either
direction. Images still go through as `CF_DIB`, which Windows
synthesizes.

**Request validation.** `ironrdp-client` treats a rejected clipboard
request as session-fatal, so the backend checks what it sends against
the rules `Cliprdr` enforces: file list indexes, range bounds, huge-file
positions, and released locks.

**Also fixed.** The window subclass state leaked on drop, and
`WM_DESTROY` freed it while `WM_NCDESTROY` could still use it.

## Viewer (`fix(viewer)`)

Without a backend factory, `ironrdp-client` created `WinClipboard` on
the RDP thread, which runs a tokio runtime and no Windows message loop,
so clipboard redirection never worked in the viewer (no clipboard
events, no backend wake-ups, no STA for OLE). The viewer now initializes
OLE and creates the clipboard, with file transfer, on its winit thread
and hands the factory to `RdpClient`.

## .NET FFI (`feat(ffi)`)

- `ActiveStage.SubmitClipboardMessage` hands any backend message to the
clipboard channel, file transfer included, and returns the frame to
send.
- `ActiveStage.DriveClipboardTimeouts` expires clipboard locks and file
transfers.
- `WinCliprdr.NewWithFileTransfer(bool)` opts in. `WinCliprdr.New()`
keeps file transfer off, so existing consumers that only route the three
original message types don't receive new ones.
- `WinCliprdr.NextClipboardMessageTimeout(ms)` lets a loop wake up to
drive the timeouts.
- The Avalonia example opts in, submits every message through
`SubmitClipboardMessage`, drives the timeouts, and logs a failed
clipboard operation instead of stopping clipboard handling.

Only the four affected generated files are included.

## Testing

- `cargo xtask check fmt`, `check lints` and `check locks` pass.
- 12 unit tests in `cliprdr_native::windows`: format filtering, file
list building, file contents serving, range sizing, descriptor
round-trips.
- 6 tests in `cliprdr_native::windows_live` run a `CliprdrClient` over
`WinClipboard` against an in-memory `CliprdrServer` on the real Windows
clipboard. They are opt-in with `IRONRDP_WIN_CLIPBOARD_LIVE=1`, because
they replace the clipboard and briefly open an Explorer window. All pass
locally:
- Explorer pastes remote folders and files, contents checked byte for
byte.
  - A 5 GiB remote file is read through OLE past 4 GiB.
  - Remote handle and path formats are not offered locally.
- Local folders reach the remote, both at connect time and when copied
mid-session.
  - Local image formats are filtered.
  - Virtual files from another session reach the remote.
- .NET: the solution builds and the smoke tests pass (9/9, one new).

- End to end with `ironrdp-viewer` against a Windows 11 server
(rdpclip): capabilities negotiated as `STREAM_FILECLIP_ENABLED |
FILECLIP_NO_FILE_PATHS | CAN_LOCK_CLIPDATA | HUGE_FILE_SUPPORT_ENABLED`.
A local folder (nested directories, an empty directory, an empty file, a
5 MiB random file) copied in Explorer was pasted into Explorer in the
session, served under rdpclip's lock. Copying it back in the session and
pasting it into a local Explorer window gave byte-identical files
(SHA-256).

Not verified by hand: ActiveX and the Avalonia example against a real
server.

## Notes for review

- This conflicts with #2175 in `MainWindow.axaml.cs` and the generated
`ActiveStage` files. The clipboard loop body here is a single
`SubmitClipboardMessage` call, so the merge only needs #2175's
`WithActiveStage(...)` wrapper around it; the generated files can be
regenerated.
- Running the extra test suite locally may need `CMAKE` pointed at
Visual Studio 2026's bundled CMake: `libopus_sys` fails with older CMake
versions that lack the "Visual Studio 18 2026" generator.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 08:47:03 -04:00
..