{"id":15293,"date":"2010-01-13T07:00:00","date_gmt":"2010-01-13T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/01\/13\/why-does-getcommandline-give-me-a-corrupted-command-line\/"},"modified":"2010-01-13T07:00:00","modified_gmt":"2010-01-13T07:00:00","slug":"why-does-getcommandline-give-me-a-corrupted-command-line","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100113-00\/?p=15293","title":{"rendered":"Why does GetCommandLine give me a corrupted command line?"},"content":{"rendered":"<p>A customer had the following problem:<\/p>\n<blockquote class=\"q\"><p> We&#8217;re calling <code>GetCommandLine<\/code> to retrieve the command line, and the documentation says that it returns a single null-terminated string. However, when we call it in our application, we find that it is actually a <i>double<\/i>-null-terminated string. The buffer returned consists of a series of null-terminated strings, one string per word on the command line, all stored one after the other, and with two null terminators at the end. How do I get the original string? <\/p><\/blockquote>\n<p> Recall that <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/02\/23\/9440784.aspx\"> the command line is just a conveniently-initialized variable in a process<\/a> and once it&#8217;s set up, the kernel doesn&#8217;t really care about it any more.\n What is most likely happening is that somebody is taking the raw command line returned by <code>GetCommandLine<\/code> and <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/11\/25\/9928372.aspx\"> writing to it<\/a>. The customer can confirm this by dumping the command line just as the process starts, even before any DLLs get to run their <code>DllMain<\/code>s, and then setting a write breakpoint on the command line to see who is writing to it.<\/p>\n<p> And in fact, the customer did find the culprit. <\/p>\n<blockquote class=\"q\"><p> It turns out it was some other part of the code (not written by me!) which was parsing the command line and writing into it in the process. <\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>A customer had the following problem: We&#8217;re calling GetCommandLine to retrieve the command line, and the documentation says that it returns a single null-terminated string. However, when we call it in our application, we find that it is actually a double-null-terminated string. The buffer returned consists of a series of null-terminated strings, one string per [&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-15293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer had the following problem: We&#8217;re calling GetCommandLine to retrieve the command line, and the documentation says that it returns a single null-terminated string. However, when we call it in our application, we find that it is actually a double-null-terminated string. The buffer returned consists of a series of null-terminated strings, one string per [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15293","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=15293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15293\/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=15293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=15293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=15293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}