{"id":2278,"date":"2013-05-30T21:50:01","date_gmt":"2013-05-30T12:59:58","guid":{"rendered":"https:\/\/programresource.net\/?p=2278"},"modified":"2013-05-30T21:59:58","modified_gmt":"2013-05-30T12:59:58","slug":"crun-command-within-program-without-showing-window-takeown-example","status":"publish","type":"post","link":"https:\/\/programresource.net\/en\/2013\/05\/30\/2278.html","title":{"rendered":"[C++]Run command within program without showing window \/ Takeown example"},"content":{"rendered":"<p>When developing simple tool or doing automation, calling shell command might be easy and fast.<\/p>\n<p>However, calling &#8220;cmd \/c&#8221; with ShellExecute brings up shell window, and might return to code before command completes.<\/p>\n<p>In such case, use CreateProcess to call cmd. You can hide shell window and wait for process to complete, and even get return value by using CreateProcess API.<\/p>\n<p>Below is simple sample code for calling takeown command, which is a command to take ownership of system files which can&#8217;t be modified even with Administrator privilege.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">BOOL runcmdproc(CString cmd)\r\n{\r\n\tSTARTUPINFO  si;\r\n\tPROCESS_INFORMATION pi;\r\n\tDWORD ret;\r\n\tHANDLE hWndmain;\r\n\r\n\tmemset(&si, 0, sizeof(si));\r\n\tsi.cb = sizeof(si);\r\n\tsi.dwFlags = STARTF_USESHOWWINDOW;\r\n\tsi.wShowWindow = SW_HIDE;\r\n\r\n\tret = CreateProcess(NULL, cmd.GetBuffer(0), NULL, NULL, FALSE,\r\n                      CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS,\r\n                      NULL, NULL, &si, &pi);\r\n\thWndmain = pi.hProcess;\r\n\tCloseHandle(pi.hThread);\r\n\tWaitForSingleObject(hWndmain, INFINITE);\r\n\tCloseHandle(hWndmain);\r\n\treturn (ret);\r\n}\r\n\r\nBOOL TakeOwn(CString filename)\r\n{\r\n\tCString param;\r\n\tparam.Format(&quot;cmd \/c takeown \/f \\&quot;%s\\&quot; && icacls \\&quot;%s\\&quot; \/grant administrators:F&quot;,filename,filename);\r\n\tif (runcmdproc(param))\r\n\t\treturn TRUE;\r\n\telse\r\n\t\treturn FALSE;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When developing simple tool or doing automation, calling shell command might be easy and fast. However, calling &#8220;cmd \/c&#8221; with ShellExecute brings up shell window, and might return to code before command completes. In such case, use CreateProcess to call cmd. You can hide shell window and wait for process to complete, and even get return value by using CreateProcess API. Below is simple sample code for calling takeown command, which is a command to take ownership of system files which can&#8217;t be modified even with Administrator privilege.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[331],"tags":[497,498,500,499,496,502,501],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3pJyQ-AK","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/2278"}],"collection":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/comments?post=2278"}],"version-history":[{"count":1,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/2278\/revisions"}],"predecessor-version":[{"id":2281,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/2278\/revisions\/2281"}],"wp:attachment":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/media?parent=2278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/categories?post=2278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/tags?post=2278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}