Click here to Skip to main content
12,898,392 members (56,947 online)
Click here to Skip to main content
Add your own
alternative version


159 bookmarked
Posted 21 Dec 2004

Hosting EXE Applications in a WinForm project

, 21 Dec 2004
Rate this:
Please Sign up or sign in to vote.
A custom control to launch and embed an EXE into a WinForm based application.

Sample Image


Though not a common task, recently I needed to take an existing executable application and embed it into an application I was building. Oddly enough, I did not need any interaction between my application and the existing EXE. As it ends up, this is not a difficult thing to do. To make it even easier, I created a custom C# control that allows you to specify the name of an executable you want embedded into your application. The control takes care of all the rest.

How does it work

In design time, the user can specify the name of the executable to embed. When the control is created in runtime, it launches the application as follows:

Process p = null; 
  // Start the process 
  p = System.Diagnostics.Process.Start(this.exeName); 

  // Wait for process to be created and enter idle condition 

  // Get the main handle
  appWin = p.MainWindowHandle; 
catch (Exception ex) 
  MessageBox.Show(this, ex.Message, "Error"); 

After launching, the code must then set the parent of the executable's main window to the control handle. It does this as follows:

// Put it into this form
SetParent(appWin, this.Handle);

// Remove border and whatnot
SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);

// Move the window to overlay it on this window
MoveWindow(appWin, 0, 0, this.Width, this.Height, true);

Any time the control is resized, it must also resize the executable window. To do so, it does this in the Resize function:

protected override void OnResize(EventArgs e)
  if (this.appWin != IntPtr.Zero)
    MoveWindow(appWin, 0, 0, this.Width, this.Height, true);
  base.OnResize (e);

Lastly, when the control is destroyed, it should shut down the executable. To do so, it does the following:

protected override void OnHandleDestroyed(EventArgs e)
  // Stop the application
  if (appWin != IntPtr.Zero)

    // Post a colse message
    PostMessage(appWin, WM_CLOSE, 0, 0);

    // Delay for it to get the message

    // Clear internal handle
    appWin = IntPtr.Zero;


  base.OnHandleDestroyed (e);


  • 12-20-2004: Released.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jay Nelson
Architect Omron Adept Technologies, Inc
United States United States
I have been developing software professionaly since 1991 writing software in automation and manufacturing environments. For 14 years I worked for companies that built custom robotic automated equipment for the semiconductor, telecommunications, and other industies. Presently, I work for a company that manufacturers industrial robots where I write high level coordination and control software.

My undergraduate degrees are in Mathematics and Philosopy. My graduate degree is in Management Information Systems. I am MCSD certified in Visual C++ 6.0 and MCSD.NET certified in C#. I am also have the PMI-ACP certification.

I enjoy karate and reading.

You may also be interested in...

Comments and Discussions

Questionhow can i host a C# application (exe file) in a win form in this project? Pin
mehrdad heidari24-Aug-13 10:06
membermehrdad heidari24-Aug-13 10:06 
GeneralFor those who can't get it working for any application other than notepad Pin
Christopher Sippel9-Apr-13 7:33
memberChristopher Sippel9-Apr-13 7:33 
Questiontoopstrip disabled Pin
Member 902164117-Feb-13 23:27
memberMember 902164117-Feb-13 23:27 
AnswerRe: toopstrip disabled Pin
bryonmvi16-Sep-14 7:46
memberbryonmvi16-Sep-14 7:46 
Questionwindows 7 problem Pin
Member 930102825-Jul-12 12:26
memberMember 930102825-Jul-12 12:26 
AnswerRe: windows 7 problem Pin
RaRi200323-Nov-12 2:46
memberRaRi200323-Nov-12 2:46 
AnswerRe: windows 7 problem Pin
Member 103874327-Nov-13 12:27
memberMember 103874327-Nov-13 12:27 
QuestionHosting External Exe Application in a WPF Window. Pin
sukesh.gudikandulla27-May-12 6:35
membersukesh.gudikandulla27-May-12 6:35 
Please help me, how to open External exe application in a WPF Window. Thanks...

Below is the code...sample right now..i am using.. let me know what i need to modify in the code work for external exe to launch in WPF window. This is opening application in separately not in WPF Window.

namespace WpfApplication_Blend
/// <summary>
/// Interaction logic for Window1.xaml
public partial class Window1 : Window
public IntPtr MainWindowHandle { get; set; }

[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

//[DllImport("user32.dll", SetLastError = true)]
//private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

public Window1()

//External exe inside WPF Window
System.Windows.Forms.Panel _pnlSched = new System.Windows.Forms.Panel();

WindowsFormsHost windowsFormsHost1 = new WindowsFormsHost();

windowsFormsHost1.Child = _pnlSched;


ProcessStartInfo psi = new ProcessStartInfo(@"C:\Program Files\Atwin\Atwin2k2.exe");

psi.WindowStyle = ProcessWindowStyle.Normal;

Process PR = Process.Start(psi);

PR.WaitForInputIdle(); // true if the associated process has reached an idle state.
SetParent(PR.MainWindowHandle, _pnlSched.Handle); // loading exe to the wpf window.

catch (Exception ex)


modified 27-May-12 13:12pm.

QuestionIt doesn't work in window 7 Pin
dhojonline3-Apr-12 2:44
memberdhojonline3-Apr-12 2:44 
AnswerRe: It doesn't work in window 7 Pin
k3v1n5215-May-12 2:19
memberk3v1n5215-May-12 2:19 
GeneralRe: It doesn't work in window 7 Pin
Member 899490321-May-12 3:40
memberMember 899490321-May-12 3:40 
QuestionHow to stop exe flashing up or appearing in taskbar Pin
JamesA_Dev26-Jan-12 1:24
memberJamesA_Dev26-Jan-12 1:24 
QuestionMenus are disabled Pin
delfo8-Aug-11 22:07
memberdelfo8-Aug-11 22:07 
AnswerRe: Menus are disabled Pin
bryonmvi16-Sep-14 7:47
memberbryonmvi16-Sep-14 7:47 
GeneralMy vote of 4 Pin
yanghe11173-May-11 4:16
memberyanghe11173-May-11 4:16 
GeneralMy vote of 5 Pin
Yusuf18-Jan-11 9:35
memberYusuf18-Jan-11 9:35 
QuestionAdvice needed Pin
HollyY18-Nov-10 12:57
memberHollyY18-Nov-10 12:57 
GeneralWin7 and Vista Incompatable Pin
cs_can3-Apr-10 3:28
membercs_can3-Apr-10 3:28 
QuestionPowerPoint Viewer Does Not seem to work Pin
Member 265354228-Dec-09 21:47
memberMember 265354228-Dec-09 21:47 
GeneralAnother problem Pin
il172316-Nov-09 5:36
memberil172316-Nov-09 5:36 
GeneralProblem Pin
il172316-Nov-09 4:59
memberil172316-Nov-09 4:59 
GeneralRe: Problem Pin
Elbe Alves Miranda24-Feb-11 3:24
memberElbe Alves Miranda24-Feb-11 3:24 
GeneralReally great, works perfectly after bug fix - thank you very much! Pin
Blasterwurm10-Nov-09 4:39
memberBlasterwurm10-Nov-09 4:39 
GeneralRe: Really great, works perfectly after bug fix - thank you very much! Pin
Atchyuta Rao9-Dec-09 20:29
memberAtchyuta Rao9-Dec-09 20:29 
GeneralRe: Really great, works perfectly after bug fix - thank you very much! Pin
cs_can3-Apr-10 5:41
membercs_can3-Apr-10 5:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 21 Dec 2004
Article Copyright 2004 by Jay Nelson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid