{"id":555,"date":"2019-02-08T16:22:03","date_gmt":"2019-02-08T16:22:03","guid":{"rendered":"http:\/\/www.trapzz.com\/?p=555"},"modified":"2021-01-26T18:15:47","modified_gmt":"2021-01-26T18:15:47","slug":"copying-text-to-the-windows-clipboard","status":"publish","type":"post","link":"http:\/\/www.trapzz.com\/?p=555","title":{"rendered":"Copying Text to the Windows Clipboard"},"content":{"rendered":"\n<p>Recently I was having trouble reproducing a bug the perf team was running into, it had to do with a specific camera position in our workload.  I decided the simplest and most time saving approach (for our purposes and for them) was to have them copy the camera data to a clipboard and email it to me &#8211; then I could reproduce the position exactly.<\/p>\n\n\n\n<p>The msdn example code I found for copying data to the clipboard worked fine but it was a little overblown for the simple case I needed.  I&#8217;ve boiled it down to just a few lines and I wanted to post it here in case anyone else wants to add this simple functionality to their application:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ( ! OpenClipboard(hWnd) ) \n    return; \n\nEmptyClipboard(); \n\nchar text&#91;512] = \"Your clipboard text (or data)\";\nsize_t text_len = strlen(text);\n\n\/\/ Allocate a global memory object for the text. \nHGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (text_len + 1) * sizeof(char)); \n\n\/\/ Lock the handle and copy the text to the buffer. \nchar *lptstrCopy = (char *) GlobalLock( hglbCopy ); \n\nmemcpy(lptstrCopy, text, text_len * sizeof(char) );\nlptstrCopy&#91;text_len] = (char) 0;\n\nGlobalUnlock(hglbCopy); \n\n\/\/ Place the handle on the clipboard. \nSetClipboardData(CF_TEXT, hglbCopy); \n\nCloseClipboard( );<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was having trouble reproducing a bug the perf team was running into, it had to do with a specific camera position in our workload. I decided the simplest and most time saving approach (for our purposes and for them) was to have them copy the camera data to a clipboard and email it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-555","post","type-post","status-publish","format-standard","hentry","category-general-code"],"_links":{"self":[{"href":"http:\/\/www.trapzz.com\/index.php?rest_route=\/wp\/v2\/posts\/555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.trapzz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.trapzz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.trapzz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.trapzz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=555"}],"version-history":[{"count":2,"href":"http:\/\/www.trapzz.com\/index.php?rest_route=\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":732,"href":"http:\/\/www.trapzz.com\/index.php?rest_route=\/wp\/v2\/posts\/555\/revisions\/732"}],"wp:attachment":[{"href":"http:\/\/www.trapzz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.trapzz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.trapzz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}