{"id":3023,"date":"2013-10-07T07:00:00","date_gmt":"2013-10-07T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/10\/07\/printing-the-contents-of-the-clipboard-as-text-to-stdout\/"},"modified":"2013-10-07T07:00:00","modified_gmt":"2013-10-07T07:00:00","slug":"printing-the-contents-of-the-clipboard-as-text-to-stdout","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131007-00\/?p=3023","title":{"rendered":"Printing the contents of the clipboard as text to stdout"},"content":{"rendered":"<p>\nThe <code>clip.exe<\/code> takes its stdin and puts it on the clipboard.\nBut how do you get it out?\nThat&#8217;s today&#8217;s Little Program.\n(I guess we could call it\n<code>clop.exe<\/code>.)\n<\/p>\n<pre>\n#define UNICODE\n#define _UNICODE\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;tchar.h&gt;\n#include &lt;strsafe.h&gt;\nvoid WriteToStdOut(const void *pvBuf, DWORD cbBuf)\n{\n DWORD cbWritten;\n WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), pvBuf, cbBuf,\n           &amp;cbWritten, nullptr);\n}\nint __cdecl _tmain(int argc, PTSTR *argv)\n{\n if (OpenClipboard(nullptr)) {\n  HANDLE h = GetClipboardData(CF_UNICODETEXT);\n  if (h) {\n   auto pwchText = static_cast&lt;PCWSTR&gt;(GlobalLock(h));\n   if (pwchText) {\n    SIZE_T cbMemory = GlobalSize(h);\n    \/\/ arbitrary limit because I am lazy\n    cbMemory = min(cbMemory, 0x10000000);\n    size_t cbActual;\n    if (SUCCEEDED(StringCbLengthW(pwchText, cbMemory,\n                                  &amp;cbActual))) {\n     if (argc == 2 &amp;&amp; _tcsicmp(argv[1], TEXT(\"\/u\")) == 0) {\n      WriteToStdOut(pwchText, cbActual);\n     } else {\n      UINT cp = (argc == 2 &amp;&amp;\n                _tcsicmp(argv[1], TEXT(\"\/a\")) == 0) ?\n                     CP_ACP : CP_OEMCP;\n      int cch = WideCharToMultiByte(cp, 0, pwchText,\n               cbActual \/ 2, nullptr, 0, nullptr, nullptr);\n      if (cch &gt; 0) {\n       auto psz = new(std::nothrow) char[cch];\n       if (psz) {\n        WideCharToMultiByte(cp, 0, pwchText, cbActual \/ 2,\n                               psz, cch, nullptr, nullptr);\n        WriteToStdOut(psz, cch);\n        delete[] psz;\n       }\n      }\n     }\n    }\n    GlobalUnlock(h);\n   }\n  }\n  CloseClipboard();\n }\n return 0;\n}\n<\/pre>\n<p>\nOkay, what do we have here?\n<\/p>\n<p>\nWe open the clipboard and try to get the Unicode text on it.\nWe then look for the null terminator within the first\n0x10000000 bytes.\nWhy do I stop at 256<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2009\/06\/11\/9725386.aspx\">MB<\/a>?\nBecause I&#8217;m lazy and this lets me avoid worrying about\ninteger overflow.\nThis is a Little Program, remember.\n<\/p>\n<p>\nIf you pass the <code>\/U<\/code> command line switch,\nthen the output is printed to stdout as the Unicode string itself.\n<\/p>\n<p>\nIf you pass the <code>\/A<\/code> command line switch,\nthen the output is converted to\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/05\/31\/144893.aspx\">\nANSI<\/a>.\n<\/p>\n<p>\nOtherwise the output is converted to the\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/08\/29\/457483.aspx\">\nOEM<\/a> code page.\n<\/p>\n<p>\n<b>Bonus chatter<\/b>:\nYou can get most of the same program above (no Unicode output)\nin much less code\nif you&#8217;re willing to use C#:\n<\/p>\n<pre>\nclass Program {\n  [System.STAThread]\n  public static void Main(string[] args)\n  {\n    string text = System.Windows.Forms.Clipboard.GetText();\n    if (args.Length == 1 &amp;&amp; string.Compare(args[0], \"\/a\", true) == 0) {\n        System.Console.OutputEncoding = System.Text.Encoding.Default;\n        System.Console.Write(\"changed encoding\");\n    }\n   System.Console.Write(text);\n  }\n}\n<\/pre>\n<p>\nOr perl (ANSI output only):\n<\/p>\n<pre>\nuse Win32::Clipboard;\nprint Win32::Clipboard()-&gt;GetText();\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The clip.exe takes its stdin and puts it on the clipboard. But how do you get it out? That&#8217;s today&#8217;s Little Program. (I guess we could call it clop.exe.) #define UNICODE #define _UNICODE #define STRICT #include &lt;windows.h&gt; #include &lt;stdio.h&gt; #include &lt;tchar.h&gt; #include &lt;strsafe.h&gt; void WriteToStdOut(const void *pvBuf, DWORD cbBuf) { DWORD cbWritten; WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), pvBuf, cbBuf, [&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-3023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The clip.exe takes its stdin and puts it on the clipboard. But how do you get it out? That&#8217;s today&#8217;s Little Program. (I guess we could call it clop.exe.) #define UNICODE #define _UNICODE #define STRICT #include &lt;windows.h&gt; #include &lt;stdio.h&gt; #include &lt;tchar.h&gt; #include &lt;strsafe.h&gt; void WriteToStdOut(const void *pvBuf, DWORD cbBuf) { DWORD cbWritten; WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), pvBuf, cbBuf, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3023","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=3023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3023\/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=3023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}