{"id":12843,"date":"2010-09-16T07:00:00","date_gmt":"2010-09-16T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/09\/16\/how-is-the-commandlinetoargvw-function-intended-to-be-used\/"},"modified":"2010-09-16T07:00:00","modified_gmt":"2010-09-16T07:00:00","slug":"how-is-the-commandlinetoargvw-function-intended-to-be-used","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100916-00\/?p=12843","title":{"rendered":"How is the CommandLineToArgvW function intended to be used?"},"content":{"rendered":"<p>\nThe <code>CommandLineToArgvW<\/code> function does some basic\ncommand line parsing.\nA customer reported that it was producing strange results when\nyou passed an empty string as the first parameter:\n<\/p>\n<pre>\nLPWSTR argv = CommandLineToArgvW(L\"\", &amp;argc);\n<\/pre>\n<p>\nWell, okay, yeah, but huh?\n<\/p>\n<p>\nThe first parameter to <code>CommandLineToArgvW<\/code> is supposed\nto be the value returned by <code>GetCommandLineW<\/code>.\nThat&#8217;s the command line, and that&#8217;s what\n<code>CommandLineToArgvW<\/code> was designed to parse.\nIf you pass something else, then\n<code>CommandLineToArgvW<\/code> will try to cope,\nbut it&#8217;s not really doing what it was designed for.\n<\/p>\n<p>\nIt turns out that the customer was mistakenly passing the\n<code>lpCmdLine<\/code> parameter that was passed to the\n<code>wWinMain<\/code> function:\n<\/p>\n<pre>\nint WINAPI wWinMain(\n    HINSTANCE hInstance,\n    HINSTANCE <a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/06\/15\/156022.aspx\">hPrevInstance<\/a>,\n    LPWSTR lpCmdLine,\n    int nCmdShow)\n{\n    int argc;\n    <i>LPWSTR argv = CommandLineToArgvW(lpCmdLine, &amp;argc);<\/i>\n    ...\n}\n<\/pre>\n<p>\nThat command line is not in the format that <code>CommandLineToArgvW<\/code>\nexpects.\nThe\n<code>CommandLineToArgvW<\/code> function wants the full, unexpurgated\ncommand line as returned by the <code>GetCommandLineW<\/code> function,\nand it breaks it up on the assumption that\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/05\/15\/597984.aspx\">\nthe first word on the command line is the program name<\/a>.\nIf you hand it an empty string,\nthe <code>CommandLineToArgvW<\/code> function says,\n&#8220;Whoa,\nwhoever generated this command line totally screwed up.\nI&#8217;ll try to muddle through as best I can.&#8221;\n<\/p>\n<p>\nNext time, we&#8217;ll look at the strange status of quotation marks\nand backslashes\nin <code>CommandLineToArgvW<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The CommandLineToArgvW function does some basic command line parsing. A customer reported that it was producing strange results when you passed an empty string as the first parameter: LPWSTR argv = CommandLineToArgvW(L&#8221;&#8221;, &amp;argc); Well, okay, yeah, but huh? The first parameter to CommandLineToArgvW is supposed to be the value returned by GetCommandLineW. That&#8217;s the command [&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-12843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The CommandLineToArgvW function does some basic command line parsing. A customer reported that it was producing strange results when you passed an empty string as the first parameter: LPWSTR argv = CommandLineToArgvW(L&#8221;&#8221;, &amp;argc); Well, okay, yeah, but huh? The first parameter to CommandLineToArgvW is supposed to be the value returned by GetCommandLineW. That&#8217;s the command [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12843","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=12843"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12843\/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=12843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=12843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=12843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}