Read and write Windows clipboard text from Rust with WinAPI

The Windows clipboard is a system-wide store that every application reads and writes through a handful of Win32 calls. This post wraps those calls in a small Rust module that copies text to the clipboard and reads it back.
The rules the code has to follow:
- Open the clipboard with
OpenClipboardbefore using it, and close it withCloseClipboardafterwards. Only one window can have it open at a time. - Data goes on the clipboard as a movable global memory handle from
GlobalAlloc(GMEM_MOVEABLE, ...). After a successfulSetClipboardData, the system owns that memory and you must not free it. GetClipboardDatareturns a handle, not a pointer. You callGlobalLockto get a pointer to the data, thenGlobalUnlockwhen done.- Use
CF_UNICODETEXT(null-terminated UTF-16) rather thanCF_TEXT(the ANSI code page), so non-ASCII text survives. Windows synthesizesCF_UNICODETEXTwhen another app only putCF_TEXTon the clipboard.
Project setup
Section titled “Project setup”cargo new winapi_clipboardcd winapi_clipboardAdd winapi with the winuser (clipboard functions) and winbase (global memory functions) features:
[dependencies]winapi = { version = "0.3.9", features = ["winuser", "winbase"] }The clipboard module
Section titled “The clipboard module”The module opens the clipboard in a constructor and closes it in Drop, so it is closed on every path, including early returns with ?. Put this in src/main.rs:
mod clipboard { use std::ptr::{copy_nonoverlapping, null_mut}; use std::slice;
use winapi::um::winbase::{GlobalAlloc, GlobalFree, GlobalLock, GlobalSize, GlobalUnlock, GMEM_MOVEABLE}; use winapi::um::winuser::{ CloseClipboard, EmptyClipboard, GetClipboardData, IsClipboardFormatAvailable, OpenClipboard, SetClipboardData, CF_UNICODETEXT, };
/// The clipboard, held open until this value is dropped. pub struct Clipboard(());
impl Clipboard { pub fn open() -> Result<Self, &'static str> { // A null owner is fine for reading. A GUI app should pass its window handle; // Microsoft's docs warn SetClipboardData can fail when the owner is NULL. if unsafe { OpenClipboard(null_mut()) } == 0 { return Err("OpenClipboard failed (another window may have it open)"); } Ok(Clipboard(())) }
pub fn get_text(&self) -> Result<String, &'static str> { unsafe { if IsClipboardFormatAvailable(CF_UNICODETEXT) == 0 { return Err("the clipboard does not contain text"); } let handle = GetClipboardData(CF_UNICODETEXT); if handle.is_null() { return Err("GetClipboardData failed"); } let ptr = GlobalLock(handle) as *const u16; if ptr.is_null() { return Err("GlobalLock failed"); } // Never read past the allocation, even if the terminator is missing. let max_len = GlobalSize(handle) / 2; let units = slice::from_raw_parts(ptr, max_len); let len = units.iter().position(|&c| c == 0).unwrap_or(max_len); let text = String::from_utf16_lossy(&units[..len]); GlobalUnlock(handle); Ok(text) } }
pub fn set_text(&self, text: &str) -> Result<(), &'static str> { let wide: Vec<u16> = text.encode_utf16().chain(std::iter::once(0)).collect(); let size_in_bytes = wide.len() * std::mem::size_of::<u16>();
unsafe { let h_mem = GlobalAlloc(GMEM_MOVEABLE, size_in_bytes); if h_mem.is_null() { return Err("GlobalAlloc failed"); } let dst = GlobalLock(h_mem) as *mut u16; if dst.is_null() { GlobalFree(h_mem); return Err("GlobalLock failed"); } copy_nonoverlapping(wide.as_ptr(), dst, wide.len()); GlobalUnlock(h_mem);
if EmptyClipboard() == 0 { GlobalFree(h_mem); return Err("EmptyClipboard failed"); } if SetClipboardData(CF_UNICODETEXT, h_mem).is_null() { // Ownership was not transferred, so the memory is still ours to free. GlobalFree(h_mem); return Err("SetClipboardData failed"); } // Success: the system now owns h_mem. Do not free it. } Ok(()) } }
impl Drop for Clipboard { fn drop(&mut self) { unsafe { CloseClipboard(); } } }}A few details worth calling out:
EmptyClipboardmust be called beforeSetClipboardData. It clears the previous contents and makes the window that opened the clipboard its owner.- The original version of this post cast the result of
GetClipboardDatastraight to a byte pointer and read it as ASCII. That is wrong twice over: the handle must be locked withGlobalLockto get a pointer, and byte-to-charconversion garbles anything outside ASCII. GlobalSizebounds the read, so a malformed clipboard entry without a terminator can’t make the loop run off the end of the buffer.
Example: write, then read back
Section titled “Example: write, then read back”Add a main below the module:
fn main() -> Result<(), &'static str> { let example_text = "Hello, clipboard! Grüße";
println!("Writing to clipboard: {example_text}"); { let cb = clipboard::Clipboard::open()?; cb.set_text(example_text)?; } // dropped here, which closes the clipboard
let cb = clipboard::Clipboard::open()?; let text = cb.get_text()?; println!("Clipboard content: {text}");
Ok(())}Run it:
cargo run --releaseOutput:
Writing to clipboard: Hello, clipboard! GrüßeClipboard content: Hello, clipboard! GrüßePaste into any other app to confirm the text really is on the system clipboard.
Where to go next
Section titled “Where to go next”- Other formats (
CF_HDROPfor file lists,CF_DIBfor bitmaps, or formats registered withRegisterClipboardFormatW) follow the same open, lock and set pattern. - To react when the clipboard changes, register a window with
AddClipboardFormatListenerand handleWM_CLIPBOARDUPDATE. - Microsoft’s Using the Clipboard page has the C versions of these operations, which map almost line for line onto the Rust code above.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)