// The following code copies the "Hello World" string to the clipboard. After the code is executed, paste it at another address and you will see "Hello World"
# Include <windows. h>
Void copystringclipboard (hwnd, char * Str) // Save the string to the clipboard:
{
Hglobal hclip;
// Define an hglobal handle variable to point to the allocated memory block
If (openclipboard (hwnd ))
{
Emptyclipboard (); // clear the clipboard content
Hclip = globalalloc (gmem_moveable, strlen (STR) + 1 );
// Allocate removable memory blocks on the heap. The program returns a memory handle.
Char * Buff; // defines the pointer variable pointing to the struct type
Buff = (char *) globallock (hclip );
// Lock the allocated memory block, convert the memory block handle into a pointer, and Add 1
Strcpy_s (buff, strlen (STR) + 1, STR );
// Wcscpy (buff, STR );
// Copy the user input data to the pointer variable, which is actually copied to the allocated memory block.
Globalunlock (hclip );
// After the data is written, unlock the data and reduce the number of the referenced counter by 1.
Setclipboarddata (cf_text, hclip );
// Put the memory block containing data into the resource management of the clipboard
Closeclipboard ();
// Close the clipboard and release the occupied resources of the clipboard.
// MessageBox (L "data has been saved to the Clipboard ");
}
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Copystringclipboard (null, "Hello World ");
Return 0;
}