Skip to content

Mustafa Ateş UZUN Blog

mustafauzun.co

  • About
  • Blog
  • Home
  • »
  • C#
  • »
  • C#, how to open an .exe in WinForm
  • »
    • September 16, 2022April 18, 2023
    • by mustafauzun
  • C#

C#, how to open an .exe in WinForm

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 – Allows to open .exe in winform.
SetWindowLong – Sets the .exe’s window style.
MoveWindow – Changes the location of the .exe in WinForm.

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);

const int WM_SYSCOMMAND = 274;
const int SC_MAXIMIZE = 61488;

[DllImport("user32.dll")]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

private const int GWL_STYLE = (-16);
private const int WS_VISIBLE = 0x10000000;

For example;

Process p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p = Process.Start("charmap.exe");
p.WaitForInputIdle();
SetParent(p.MainWindowHandle, this.Handle);
this.Text = p.MainWindowTitle;
Also, if we can using SetWindowLong & MoveWindow in this code.

It looks like this.

try
{
   SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE);
}
catch { }
MoveWindow(p.MainWindowHandle, 0, 0, this.Width, this.Height, true);

Notes;
Some .exe files may not open in winform.
And you can open .cpl .msc files as well as .exe file.

Tags: c#, winform
Share

C#, how to create your own variable type

Javascript, turn function to string based calculator

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Javascript, Task Queue
  • Javascript, Thread Pool
  • Javascript, Lexical Environment
  • Javascript, Execution Context
  • Javascript, Event Delegation

Recent Comments

No comments to show.

Archives

  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • January 2022

Categories

  • C#
  • CMD
  • CSS
  • HTML
  • JavaScript
  • MongoDB
  • Node.js
  • Podcast
  • Powershell
  • Uncategorized
Theme: Puskar by Template Sell.