How do I copy the contents of a String to the clipboard in C?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In .NET, copying text to the clipboard is simple once you are using the correct Windows clipboard API and the calling thread is in STA mode. Most confusion comes from mixing plain C with C#, or from trying to call clipboard APIs from a console app without the required desktop thread model. The reliable answer for a Windows C# application is usually Clipboard.SetText.
The Usual C# Answer
For a Windows Forms application, use System.Windows.Forms.Clipboard.SetText.
Two pieces matter here:
- '
Clipboard.SetTextwrites the string to the Windows clipboard.' - '
[STAThread]makes the main thread compatible with COM-based clipboard operations.'
Without STA, the clipboard call may throw a threading-related exception.
Why STA Matters
The Windows clipboard is tied to desktop APIs that expect a single-threaded apartment. Graphical application templates usually give you that automatically, but console utilities often do not.
That is why code that works in a WinForms app can fail in a console project until you add [STAThread] and reference the right desktop assembly.
If the project targets modern .NET, you may also need Windows desktop support enabled in the project file.
WPF Uses a Similar API
In WPF, the Clipboard class lives under System.Windows, but the idea is the same.
If your application is already WPF-based, use the WPF clipboard API rather than adding a Windows Forms dependency only for clipboard access.
Console Applications Need Extra Care
A console tool can still copy text to the clipboard, but it needs to be a Windows-only utility because the built-in clipboard APIs are desktop APIs.
This is a good fit for internal developer tools, but it is not a cross-platform clipboard solution.
Handle Empty Input and Busy Clipboard States
Clipboard access is not guaranteed to succeed on the first try. Another process may temporarily hold the clipboard. Validate the input and retry if the operation is transiently blocked.
That is usually enough for desktop utilities. If repeated failures matter, log the exception instead of swallowing it completely.
If You Truly Mean the C Language
The title says "in C," but the tags point to C#. That distinction matters. There is no standard C clipboard library in the language itself. In plain C, clipboard access is platform-specific and usually means calling native APIs such as Win32 on Windows.
So the short version is:
- in C#, use
Clipboard.SetText - in plain C, use the operating system's native clipboard APIs
Security and UX Considerations
Copying to the clipboard can overwrite data the user was intentionally keeping there. If the text may be sensitive, do not silently copy it during background work. Trigger the action explicitly from a button or command so the user knows what happened.
Common Pitfalls
- Forgetting
[STAThread], which causes clipboard calls to fail in console and desktop entry points. - Mixing up C# and plain C, even though the solution is platform-specific in C and framework-specific in C#.
- Referencing the wrong clipboard namespace for the project type.
- Assuming clipboard access is always immediate even though another process may temporarily lock it.
- Using Windows-only clipboard APIs in code that is supposed to run cross-platform.
Summary
- In Windows C#, the normal solution is
Clipboard.SetText. - The calling thread should be STA, which is why
[STAThread]is important. - WinForms and WPF expose similar clipboard APIs from different namespaces.
- Console apps can use the clipboard too, but only in a Windows desktop context.
- If you truly mean plain C rather than C#, you need operating-system-specific clipboard APIs instead.

