{"id":983,"date":"2014-06-24T21:43:33","date_gmt":"2014-06-24T21:43:33","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=983"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"running-another-application-as-a-process","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/processes\/running-another-application-as-a-process","title":{"rendered":"Running Another Application As A Process"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<pre>\r\n<code>\r\nusing namespace System::Diagnostics;\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h5>\nCreate Process<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tif (File::Exists(&quot;C:\\\\Program Files\\\\Some Folder\\\\SomeApp.exe&quot;))\r\n\t{\r\n\t\tProcess1 = gcnew Process();\r\n\t\tProcess1-&gt;StartInfo-&gt;FileName = &quot;C:\\\\Program Files\\\\Some Folder\\\\SomeApp.exe&quot;;\r\n\t\tProcess1-&gt;StartInfo-&gt;Arguments = &quot;&quot;;\r\n\t\tProcess1-&gt;Start();\r\n\t\tProcess1-&gt;WaitForInputIdle();\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h5>\nIs Process Active<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tif ((Process1 != nullptr) &amp;&amp; (!Process1-&gt;HasExited))\r\n\t{\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h5>\nClose The Process<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tif ((Process1 != nullptr) &amp;&amp; (!Process1-&gt;HasExited))\r\n\t{\r\n\t\t\/\/Process1-&gt;CloseMainWindow();\r\n\t\t\/\/Process1-&gt;Close();\r\n\t\tProcess1-&gt;Kill();\t\t\t\/\/Use this instead of Close or CloseMainWindow as they leave the process active in windows task manager and stop us from beign able to open Control Manager again\r\n\t\tProcess1-&gt;WaitForExit();\t\/\/Can use this if we want to\r\n\t}\r\n\tProcess1 = nullptr;\r\n<\/code><\/pre>\n<h4>\nHas user selected other process<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tSystem::IntPtr WinHandle = GetForegroundWindow();\r\n\tif (WinHandle == Process1-&gt;MainWindowHandle)\r\n\t{\r\n<\/code><\/pre>\n<h4>\nIs A Process Already Running<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tarray&lt;Process^&gt; ^CurrentProcesses = System::Diagnostics::Process::GetProcessesByName(&quot;some_process_name&quot;);\r\n\tif (CurrentProcesses-&gt;Length &gt; 0)\r\n\t{\r\n\t\tMessageBox::Show(L&quot;Process is running:&quot; , L&quot;Running&quot;, MessageBoxButtons::OK, MessageBoxIcon::Information);\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; using namespace System::Diagnostics; &nbsp; Create Process if (File::Exists(&quot;C:\\\\Program Files\\\\Some Folder\\\\SomeApp.exe&quot;)) { Process1 = gcnew Process(); Process1-&gt;StartInfo-&gt;FileName = &quot;C:\\\\Program Files\\\\Some Folder\\\\SomeApp.exe&quot;; Process1-&gt;StartInfo-&gt;Arguments = &quot;&quot;; Process1-&gt;Start(); Process1-&gt;WaitForInputIdle(); } &nbsp; Is Process Active if ((Process1 != nullptr) &amp;&amp; (!Process1-&gt;HasExited)) { &nbsp; Close The Process if ((Process1 != nullptr) &amp;&amp; (!Process1-&gt;HasExited)) { \/\/Process1-&gt;CloseMainWindow(); \/\/Process1-&gt;Close(); Process1-&gt;Kill(); \/\/Use this instead [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-983","post","type-post","status-publish","format-standard","hentry","category-processes"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=983"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/983\/revisions"}],"predecessor-version":[{"id":1310,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/983\/revisions\/1310"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}