We recommend using Visual Studio 2017

How to: Store Text in the Clipboard (C++/CLI)


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at How to: Store Text in the Clipboard (C++/CLI).

The following code example uses the Clipboard object defined in the System.Windows.Forms namespace to store a string. This object provides two member functions: SetDataObject and GetDataObject. Data is stored in the Clipboard by sending any object derived from Object to SetDataObject.

// store_clipboard.cpp  
// compile with: /clr  
#using <System.dll>  
#using <System.Drawing.dll>  
#using <System.Windows.Forms.dll>  
using namespace System;  
using namespace System::Windows::Forms;  
[STAThread] int main()  
   String^ str = "This text is copied into the Clipboard.";  
   // Use 'true' as the second argument if  
   // the data is to remain in the clipboard  
   // after the program terminates.  
   Clipboard::SetDataObject(str, true);  
   Console::WriteLine("Added text to the Clipboard.");  
   return 0;  

How to: Retrieve Text from the Clipboard (C++/CLI)
Windows Operations (C++/CLI)
.NET Programming with C++/CLI (Visual C++)