{"id":109,"date":"2022-09-16T10:23:34","date_gmt":"2022-09-16T10:23:34","guid":{"rendered":"https:\/\/mustafauzun.co\/blog\/?p=109"},"modified":"2023-04-18T20:37:42","modified_gmt":"2023-04-18T20:37:42","slug":"c-how-to-open-an-exe-in-winform","status":"publish","type":"post","link":"https:\/\/mustafauzun.co\/blog\/c-how-to-open-an-exe-in-winform\/","title":{"rendered":"C#, how to open an .exe in WinForm"},"content":{"rendered":"\n<p>Did you know that you can open .exe files in WinForm?<\/p>\n\n\n\n<p>You can use user32.dll in Windows to do this.<\/p>\n\n\n\n<p>In user32.dll;<br>SetParent &#8211; Allows to open .exe in winform.<br>SetWindowLong &#8211; Sets the .exe&#8217;s window style.<br>MoveWindow &#8211; Changes the location of the .exe in WinForm.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>[DllImport(&quot;user32.dll&quot;)]\npublic static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);\n\n[DllImport(&quot;user32.dll&quot;, EntryPoint = &quot;SetWindowLongA&quot;, SetLastError = true)]\nprivate static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);\n\nconst int WM_SYSCOMMAND = 274;\nconst int SC_MAXIMIZE = 61488;\n\n[DllImport(&quot;user32.dll&quot;)]\nprivate static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);\n\nprivate const int GWL_STYLE = (-16);\nprivate const int WS_VISIBLE = 0x10000000;<\/code><\/pre><\/div>\n\n\n\n<p>For example;<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>Process p = new Process();\np.StartInfo.RedirectStandardOutput = true;\np.StartInfo.RedirectStandardInput = true;\np.StartInfo.UseShellExecute = true;\np.StartInfo.CreateNoWindow = true;\np.StartInfo.WindowStyle = ProcessWindowStyle.Normal;\np = Process.Start(&quot;charmap.exe&quot;);\np.WaitForInputIdle();\nSetParent(p.MainWindowHandle, this.Handle);\nthis.Text = p.MainWindowTitle;<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"601\" src=\"https:\/\/mustafauzun.co\/blog\/wp-content\/uploads\/2022\/09\/image.png\" alt=\"\" class=\"wp-image-110\" srcset=\"https:\/\/mustafauzun.co\/blog\/wp-content\/uploads\/2022\/09\/image.png 650w, https:\/\/mustafauzun.co\/blog\/wp-content\/uploads\/2022\/09\/image-300x277.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><figcaption class=\"wp-element-caption\">Also, if we can using SetWindowLong &amp; MoveWindow in this code.<\/figcaption><\/figure>\n\n\n\n<p>It looks like this.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>try\n{\n   SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE);\n}\ncatch { }\nMoveWindow(p.MainWindowHandle, 0, 0, this.Width, this.Height, true);<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"703\" height=\"574\" src=\"https:\/\/mustafauzun.co\/blog\/wp-content\/uploads\/2022\/09\/image-1.png\" alt=\"\" class=\"wp-image-111\" srcset=\"https:\/\/mustafauzun.co\/blog\/wp-content\/uploads\/2022\/09\/image-1.png 703w, https:\/\/mustafauzun.co\/blog\/wp-content\/uploads\/2022\/09\/image-1-300x245.png 300w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><\/figure>\n\n\n\n<p>Notes;<br>Some .exe files may not open in winform.<br>And you can open .cpl .msc files as well as .exe file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you know that you can open .exe files in WinForm? You can use user32.dll in Windows to do this. In user32.dll;SetParent &#8211; Allows to<\/p>\n","protected":false},"author":1,"featured_media":110,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[16,35],"class_list":["post-109","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","tag-c","tag-winform"],"_links":{"self":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":2,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":899,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions\/899"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media\/110"}],"wp:attachment":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}