{"id":15923,"date":"2009-11-25T07:00:00","date_gmt":"2009-11-25T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2009\/11\/25\/how-do-i-get-the-command-line-of-another-process\/"},"modified":"2009-11-25T07:00:00","modified_gmt":"2009-11-25T07:00:00","slug":"how-do-i-get-the-command-line-of-another-process","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20091125-00\/?p=15923","title":{"rendered":"How do I get the command line of another process?"},"content":{"rendered":"<p>\nWin32 doesn&#8217;t expose a process&#8217;s command line to other processes.\nFrom Win32&#8217;s point of view, the command line is just a\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/02\/23\/9440784.aspx\">\nconveniently\ninitialized parameter to the process&#8217;s startup code<\/a>,\nsome data copied from the launching process to the new process\nand forgotten.\nWe&#8217;ll get back to the Win32 point of view a little later.\n<\/p>\n<p>\nIf you look around in WMI, you&#8217;ll find a\n<code>Win32_Process<\/code> object, and lo and behold,\nit has a <code>CommandLine<\/code> property.\nLet&#8217;s check it out,\nusing the\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/12\/18\/9233149.aspx\">\nstandard WMI application<\/a>:\n<\/p>\n<pre>\nstrComputer = \".\"\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet colItems = objWMIService.ExecQuery(\"Select * from <font COLOR=\"blue\">Win32_Process<\/font>\")\nFor Each objItem in colItems\n     Wscript.Echo objItem.<font COLOR=\"blue\">Name<\/font>\n     Wscript.Echo objItem.<font COLOR=\"blue\">CommandLine<\/font>\nNext\n<\/pre>\n<p>\nI fully anticipate that half of my readers will stop right there.\n&#8220;Thanks for the script. Bye!&#8221;\nAnd they won&#8217;t bother reading the analysis.\n&#8220;Because analysis is boring,\nand it&#8217;ll just tell me stuff I don&#8217;t want to hear.\nThe analysis is going to tell me why this won&#8217;t work,\nor why it&#8217;s a bad idea,\nand that just cramps my style.&#8221;\n<\/p>\n<p>\nRemember that from Win32&#8217;s point of view,\nthe command line is\njust a string that is copied into the address space of the new process.\nHow the launching process and the new process interpret this string\nis governed\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/05\/15\/597984.aspx\">\nnot by rules but by convention<\/a>.\n<\/p>\n<p>\nWhat&#8217;s more, since the string is merely a &#8220;preinitialized variable&#8221;,\na process could in principle (and many do in practice,\nalthough usually inadvertently) write to the memory that holds the\ncommand line, in which case, if you go snooping around for it,\nyou&#8217;ll see the modified command line.\nThere is no secret hiding place where the kernel keeps\nthe &#8220;real original command line,&#8221;\nany more than there is a secret hiding place where the C compiler\nkeeps the &#8220;real original parameters to a function.&#8221;\n<\/p>\n<p>\nThis is just another manifestation of the principle of\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/02\/16\/9425124.aspx\">\nnot keeping track of information you don&#8217;t need<\/a>.\n<\/p>\n<p>\nWhat does this mean for people who disregard this principle and\ngo after the command line of another process?\nYou have to understand what you are getting is non-authoritative\ninformation.\nIn fact, it&#8217;s worse.\nIt&#8217;s information <i>the application itself may have changed\nin order to try to fool you<\/i>,\nso don&#8217;t use it to make important decisions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Win32 doesn&#8217;t expose a process&#8217;s command line to other processes. From Win32&#8217;s point of view, the command line is just a conveniently initialized parameter to the process&#8217;s startup code, some data copied from the launching process to the new process and forgotten. We&#8217;ll get back to the Win32 point of view a little later. If [&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-15923","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Win32 doesn&#8217;t expose a process&#8217;s command line to other processes. From Win32&#8217;s point of view, the command line is just a conveniently initialized parameter to the process&#8217;s startup code, some data copied from the launching process to the new process and forgotten. We&#8217;ll get back to the Win32 point of view a little later. If [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15923","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=15923"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15923\/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=15923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=15923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=15923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}