{"id":3773,"date":"2013-07-18T07:00:00","date_gmt":"2013-07-18T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/07\/18\/if-youre-not-using-the-command-line-interpreter-then-the-command-line-interpreter-metacharacters-mean-nothing\/"},"modified":"2013-07-18T07:00:00","modified_gmt":"2013-07-18T07:00:00","slug":"if-youre-not-using-the-command-line-interpreter-then-the-command-line-interpreter-metacharacters-mean-nothing","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130718-00\/?p=3773","title":{"rendered":"If you&#039;re not using the command line interpreter, then the command line interpreter metacharacters mean nothing"},"content":{"rendered":"<p>\nA customer observed that the parameters passed to\n<code>Create&shy;Process<\/code> were not being interpreted\ncorrectly.\n<\/p>\n<pre>\nchar commandLine[] = \"reg.exe query \"\n                     \"HKLM\\\\SYSTEM\\\\CurrentControlSet\\\\\"\n                     \"Enum\\\\HID\\VID_0461^&amp;PID_4D15\";\nCreateProcess(NULL, commandLine, ...);\n<\/pre>\n<p>\nThe process is created successfully,\nbut it prints the message\n<tt>ERROR:\nThe system was unable to find the specified registry key or value.<\/tt>.\nWhy aren&#8217;t the parameters being parsed correctly\nby <code>Create&shy;Process<\/code>?\nThey work fine if I paste them into a command prompt.\n<\/p>\n<p>\nThis is a variation of the problem we saw a few years ago.\nBack then, we had a string with command line redirection metacharacters,\nbut since we were passing them directly to <code>Create&shy;Process<\/code>,\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2006\/05\/16\/598893.aspx\">\nthe command interpreter never got a chance to interpret them<\/a>.\nHere, we have a string that contains an ampersand,\nwhich has special meaning to the command interpreter,\nso we\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2009\/10\/29\/9914426.aspx\">\nescaped it<\/a>\nso that the command interpreter won&#8217;t try to treat it as command\nseparator.\nBut then we passed it directly to the\n<code>Create&shy;Process<\/code> function\nwithout ever invoking the command processor.\n<\/p>\n<p>\nIt&#8217;s like exchanging your U.S. dollars for Canadian dollars\nbecause you think you&#8217;re going to drive through Canada,\nand then deciding to take the southern route instead,\nand then wondering why the Canadian money doesn&#8217;t work.\nYou went to the extra effort of converting the string into\na form that will survive a journey you never sent it on!\n<\/p>\n<p>\nIf you&#8217;re going to munge the string so that you get the desired\nend result after it travels through the command interpreter,\nthen you need to send it through the command interpreter.\nOr more preferably,\ncut out the middle man and don&#8217;t bother munging\nit in the first place.\n<\/p>\n<p>\nIf you&#8217;re going to prepare a string for a journey,\nyou need to send it on that journey.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer observed that the parameters passed to Create&shy;Process were not being interpreted correctly. char commandLine[] = &#8220;reg.exe query &#8221; &#8220;HKLM\\\\SYSTEM\\\\CurrentControlSet\\\\&#8221; &#8220;Enum\\\\HID\\VID_0461^&amp;PID_4D15&#8221;; CreateProcess(NULL, commandLine, &#8230;); The process is created successfully, but it prints the message ERROR: The system was unable to find the specified registry key or value.. Why aren&#8217;t the parameters being parsed correctly [&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-3773","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer observed that the parameters passed to Create&shy;Process were not being interpreted correctly. char commandLine[] = &#8220;reg.exe query &#8221; &#8220;HKLM\\\\SYSTEM\\\\CurrentControlSet\\\\&#8221; &#8220;Enum\\\\HID\\VID_0461^&amp;PID_4D15&#8221;; CreateProcess(NULL, commandLine, &#8230;); The process is created successfully, but it prints the message ERROR: The system was unable to find the specified registry key or value.. Why aren&#8217;t the parameters being parsed correctly [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3773","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=3773"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3773\/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=3773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}