Skip to content

Instantly share code, notes, and snippets.

@t-mat
Created January 11, 2012 12:19
Show Gist options
  • Select an option

  • Save t-mat/1594419 to your computer and use it in GitHub Desktop.

Select an option

Save t-mat/1594419 to your computer and use it in GitHub Desktop.
Windows SDK : クリップボードにTCHARテキストをコピーする
#include <windows.h>
#include <tchar.h>
void setStringToClipboard(const TCHAR* str) {
if(OpenClipboard(0)) {
EmptyClipboard();
size_t l = _tcslen(str);
if(l) {
HGLOBAL h = GlobalAlloc(GMEM_MOVEABLE, (l+1) * sizeof(*str));
if(h) {
_tcscpy_s((TCHAR*)GlobalLock(h), l+1, str);
GlobalUnlock(h);
switch(sizeof(*str)) {
default:
case 1:
SetClipboardData(CF_TEXT, h);
break;
case 2:
SetClipboardData(CF_UNICODETEXT, h);
break;
}
}
}
CloseClipboard();
}
}
#if defined(SELF_TEST)
void main() {
setStringToClipboard(_T("TEST STRING"));
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment