Click here to Skip to main content
13,256,716 members (54,227 online)
Click here to Skip to main content
Add your own
alternative version


162 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

GeneralWow... Excellent Pin
MrWolfy14-Sep-08 9:53
memberMrWolfy14-Sep-08 9:53 
GeneralCan't load sample code. Pin
supermankelly31-Jul-08 4:30
membersupermankelly31-Jul-08 4:30 
QuestionQuestions about really cool component Pin
jbyrneiu22-Jul-08 17:05
memberjbyrneiu22-Jul-08 17:05 
GeneralVista incompatibility Pin
Andrei Pana23-Mar-08 13:14
memberAndrei Pana23-Mar-08 13:14 
GeneralRe: Vista incompatibility Pin
Enquiren4-Jul-08 21:32
memberEnquiren4-Jul-08 21:32 
GeneralRe: Vista incompatibility Pin
Enquiren4-Jul-08 21:38
memberEnquiren4-Jul-08 21:38 
AnswerRe: Vista incompatibility Pin
Andre Wells22-Aug-09 5:04
memberAndre Wells22-Aug-09 5:04 
GeneralRe: Vista incompatibility Pin
Atchyuta Rao9-Dec-09 21:03
memberAtchyuta Rao9-Dec-09 21:03 
GeneralRe: Vista incompatibility Pin
Member 869768711-Apr-12 7:15
memberMember 869768711-Apr-12 7:15 
GeneralRe: Vista incompatibility Pin
Shulha Yahya8-Jun-12 11:51
memberShulha Yahya8-Jun-12 11:51 
GeneralRe: Vista incompatibility Pin
Shulha Yahya8-Jun-12 12:15
memberShulha Yahya8-Jun-12 12:15 
GeneralRe: Vista incompatibility Pin
pb1513-Dec-12 7:39
memberpb1513-Dec-12 7:39 
QuestionForm visibility Pin
putzol27-Sep-07 22:35
memberputzol27-Sep-07 22:35 
GeneralNice man! Pin
BohemianDre20-Jul-07 9:57
memberBohemianDre20-Jul-07 9:57 
This is really cool, thanks so much for posting this.

Andres Becerra
GeneralErase background Pin
HHGClark18-Jul-07 5:51
memberHHGClark18-Jul-07 5:51 
GeneralRe: Erase background Pin
benjamin2313-Apr-08 10:41
memberbenjamin2313-Apr-08 10:41 
GeneralA call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pin
sinclas18-May-07 6:21
membersinclas18-May-07 6:21 
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pin
rimblock10-Jun-07 13:26
memberrimblock10-Jun-07 13:26 
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pin
Elbe Alves Miranda24-Feb-11 4:18
memberElbe Alves Miranda24-Feb-11 4:18 
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pin
hrdpatel23-Jul-07 7:30
memberhrdpatel23-Jul-07 7:30 
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pin
Windows M11-Aug-07 5:12
memberWindows M11-Aug-07 5:12 
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pin
sagyla31-Aug-07 14:19
membersagyla31-Aug-07 14:19 
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pin
RoyalRikyoo25-Feb-09 22:08
memberRoyalRikyoo25-Feb-09 22:08 
Generalcan't start programe that have login window. Pin
caixrz11-Apr-07 17:56
membercaixrz11-Apr-07 17:56 
AnswerRe: can't start programe that have login window. Pin
Member 899490321-May-12 4:32
memberMember 899490321-May-12 4:32 
QuestionHosting exe in Com application Pin
Vismay_Dhonsale10-Apr-07 5:40
memberVismay_Dhonsale10-Apr-07 5:40 
GeneralError "this.Controls.Add(this.applicationControl1);" Pin
Sheikko9-Jan-07 0:50
memberSheikko9-Jan-07 0:50 
AnswerRe: Error "this.Controls.Add(this.applicationControl1);" Pin
Elric-Wang25-Feb-07 15:27
memberElric-Wang25-Feb-07 15:27 
GeneralRe: Error "this.Controls.Add(this.applicationControl1);" Pin
dontknow6115-Apr-07 13:28
memberdontknow6115-Apr-07 13:28 
GeneralRe: Error "this.Controls.Add(this.applicationControl1);" Pin
supermankelly31-Jul-08 4:37
membersupermankelly31-Jul-08 4:37 
GeneralAppControle class rewrite for Pin
madbison28-Jul-06 13:10
membermadbison28-Jul-06 13:10 
Generalget the current directory of hosted iexplore Pin
malai kofta9-Oct-05 11:13
membermalai kofta9-Oct-05 11:13 
Generalerror Pin
malai kofta11-Aug-05 10:47
membermalai kofta11-Aug-05 10:47 
GeneralRe: error Pin
malai kofta9-Oct-05 2:25
membermalai kofta9-Oct-05 2:25 
GeneralRe: error Pin
malai kofta9-Oct-05 11:12
membermalai kofta9-Oct-05 11:12 
GeneralVB Exe problem Pin
John Bizios25-Feb-05 9:31
memberJohn Bizios25-Feb-05 9:31 
GeneralRe: VB Exe problem Pin
dgsconseil14-Mar-05 7:15
memberdgsconseil14-Mar-05 7:15 
GeneralRe: VB Exe problem Pin
John Bizios14-Mar-05 8:02
memberJohn Bizios14-Mar-05 8:02 
GeneralRe: VB Exe problem Pin
dgsconseil17-Apr-05 0:19
memberdgsconseil17-Apr-05 0:19 
Questionhow? Pin
_lapa10-Jan-05 1:34
member_lapa10-Jan-05 1:34 
Generalembed console application Pin
tlc66031-Dec-04 5:08
membertlc66031-Dec-04 5:08 
GeneralI was excited, but... Pin
wyx200030-Dec-04 11:01
memberwyx200030-Dec-04 11:01 
GeneralRe: I was excited, but... Pin
Jay Nelson4-Jan-05 8:53
memberJay Nelson4-Jan-05 8:53 
GeneralGood job Pin
hayazh26-Dec-04 0:52
susshayazh26-Dec-04 0:52 
GeneralNot running Pin
romias22-Dec-04 1:57
memberromias22-Dec-04 1:57 
GeneralRe: Not running Pin
Jay Nelson22-Dec-04 3:20
memberJay Nelson22-Dec-04 3:20 
GeneralRe: Not running Pin
romias22-Dec-04 5:41
memberromias22-Dec-04 5:41 
GeneralRe: Not running Pin
Jay Nelson22-Dec-04 9:32
memberJay Nelson22-Dec-04 9:32 
GeneralNice control for simple appications Pin
Sreejumon22-Dec-04 0:05
memberSreejumon22-Dec-04 0:05 
GeneralRe: Nice control for simple appications Pin
Jay Nelson22-Dec-04 3:13
memberJay Nelson22-Dec-04 3:13 

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
Web03 | 2.8.171114.1 | Last Updated 21 Dec 2004
Article Copyright 2004 by Jay Nelson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid