{"id":4313,"date":"2013-05-20T07:00:00","date_gmt":"2013-05-20T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/05\/20\/copying-a-file-to-the-clipboard-so-you-can-paste-it-into-explorer-or-an-email-message-or-whatever\/"},"modified":"2013-05-20T07:00:00","modified_gmt":"2013-05-20T07:00:00","slug":"copying-a-file-to-the-clipboard-so-you-can-paste-it-into-explorer-or-an-email-message-or-whatever","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130520-00\/?p=4313","title":{"rendered":"Copying a file to the clipboard so you can paste it into Explorer or an email message or whatever"},"content":{"rendered":"<p><P>\nToday&#8217;s Little Program takes a fully-qualified file name\nfrom the command line and puts that file onto the clipboard.\nOnce there, you can paste it into an Explorer window,\nor into an email message,\nor a word processing document,\nor anybody else who understands shell data objects.\n<\/P>\n<PRE>\n#include &lt;windows.h&gt;\n#include &lt;shlobj.h&gt;\n#include &lt;atlbase.h&gt;\n#include &lt;shlobj.h&gt;<\/p>\n<p>class COleInitialize {\npublic:\n COleInitialize() : m_hr(OleInitialize(NULL)) { }\n ~COleInitialize() { if (SUCCEEDED(m_hr)) OleUninitialize(); }\n operator HRESULT() const { return m_hr; }\n HRESULT m_hr;\n};<\/p>\n<p>\/\/ <A HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/09\/21\/231739.aspx\">GetUIObjectOfFile<\/A> incorporated by reference<\/p>\n<p>int __cdecl wmain(int argc, PWSTR argv[])\n{\n COleInitialize init;\n CComPtr&lt;IDataObject&gt; spdto;<\/p>\n<p> if (SUCCEEDED(init) &amp;&amp;\n     argc == 2 &amp;&amp;\n     SUCCEEDED(GetUIObjectOfFile(nullptr, argv[1], IID_PPV_ARGS(&amp;spdto))) &amp;&amp;\n     SUCCEEDED(OleSetClipboard(spdto)) &amp;&amp;\n     SUCCEEDED(OleFlushClipboard())) {\n  \/\/ success\n }<\/p>\n<p> return 0;\n}\n<\/PRE>\n<P>\nThe <CODE>COle&shy;Initialize<\/CODE> class is just the OLE\ncounterpart to\n<A HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/05\/20\/135841.aspx\">\nthe <CODE>CCo&shy;Initialize<\/CODE> class we saw some time ago<\/A>.\n<\/P>\n<P>\nAll the program does is take the file name on the command line,\nasks the shell for the corresponding data object,\nthen puts that object onto the clipboard,\n<A HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/05\/10\/10009448.aspx\">\nerasing what was there before<\/A>.\n<\/P>\n<P>\nOnce the data is on the clipboard, our job is done so we exit.\n<\/P>\n<P>\nNo, wait!\nIf you exit while your application has data on the clipboard,\nthat clipboard data may be lost.\nThe documentation for <CODE>Ole&shy;Set&shy;Clipboard<\/CODE>\nnotes:\n<\/P>\n<BLOCKQUOTE CLASS=\"q\">\nIf you need to leave the data on the clipboard\nafter your application is closed,\nyou should call <B>Ole&shy;Flush&shy;Clipboard<\/B>\nrather than calling\n<B>Ole&shy;Set&shy;Clipboard<\/B>\nwith a <B>NULL<\/B> parameter value.\n<\/BLOCKQUOTE>\n<P>\nTherefore, we stick in a call to\n<CODE>Ole&shy;Flush&shy;Clipboard<\/CODE>\nbefore exiting.\nThis forces any delay-rendered content to be rendered immediately,\nbecause we ain&#8217;t gonna be around to delay-render it no more.\n<\/P>\n<P>\nNote that the file on the command line must be fully-qualified,\nbecause we pass it straight to\n<CODE>Get&shy;UI&shy;Object&shy;Of&shy;File<\/CODE>,\nwhich expects a fully-qualified path.\nFixing the program to allow relative paths (and to actually\nprint error messages and stuff) is left as an exercise,\nbecause Little Programs don&#8217;t deal with annoying details like\nerror checking and reporting.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program takes a fully-qualified file name from the command line and puts that file onto the clipboard. Once there, you can paste it into an Explorer window, or into an email message, or a word processing document, or anybody else who understands shell data objects. #include &lt;windows.h&gt; #include &lt;shlobj.h&gt; #include &lt;atlbase.h&gt; #include &lt;shlobj.h&gt; [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-4313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program takes a fully-qualified file name from the command line and puts that file onto the clipboard. Once there, you can paste it into an Explorer window, or into an email message, or a word processing document, or anybody else who understands shell data objects. #include &lt;windows.h&gt; #include &lt;shlobj.h&gt; #include &lt;atlbase.h&gt; #include &lt;shlobj.h&gt; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/4313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=4313"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/4313\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=4313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=4313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=4313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}