Skip to content

Instantly share code, notes, and snippets.

@gyakoo
Created October 19, 2016 17:12
Show Gist options
  • Save gyakoo/04da0231c632898bae5d4c966c0c4d3e to your computer and use it in GitHub Desktop.
Save gyakoo/04da0231c632898bae5d4c966c0c4d3e to your computer and use it in GitHub Desktop.
Simple snippet to recall how you copy/paste *text* to/from clipboard in c++ win32
// ------- COPY
{
char some[256]; // your text here, zero ended
OpenClipboard(GetDesktopWindow());
EmptyClipboard();
HGLOBAL hg=GlobalAlloc(GMEM_MOVEABLE,ARRAYSIZE(some)+1);
if (!hg)
{
CloseClipboard();
return;
}
memcpy(GlobalLock(hg),some, ARRAYSIZE(some)+1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT,hg);
CloseClipboard();
GlobalFree(hg);
#endif
}
// ------ PASTE
{
if ( !IsClipboardFormatAvailable(CF_TEXT))
return;
if ( !OpenClipboard(GetDesktopWindow()) )
return;
HGLOBAL hg=GetClipboardData(CF_TEXT);
if (hg)
{
LPCSTR strData =(LPCSTR)GlobalLock(hg);
if ( strData )
{
// Your TXT in StrData
GlobalUnlock(hg);
}
}
CloseClipboard();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment