Click here to Skip to main content
11,417,750 members (46,624 online)
Click here to Skip to main content

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

Introduction

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; 
try 
{
  // Start the process 
  p = System.Diagnostics.Process.Start(this.exeName); 

  // Wait for process to be created and enter idle condition 
  p.WaitForInputIdle(); 

  // 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
    System.Threading.Thread.Sleep(1000);

    // Clear internal handle
    appWin = IntPtr.Zero;

  }

  base.OnHandleDestroyed (e);
}

History

  • 12-20-2004: Released.

License

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

Share

About the Author

Jay Nelson
Web Developer
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 medical device manufacturer developing applications for the compact framework.

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 enjoy triathlons and reading.

Comments and Discussions

 
AnswerRe: Iexplore launches separately Pin
Member 1075482013-Aug-14 22:23
memberMember 1075482013-Aug-14 22:23 
QuestionI need to be able to exit and restart the embedded app? Any ideas how? Pin
Jybs2811-Jun-14 12:47
memberJybs2811-Jun-14 12:47 
Questionwhy can't I download it Pin
Member 1078704930-Apr-14 17:35
memberMember 1078704930-Apr-14 17:35 
QuestionChlid forms in the hosted application don't show up Pin
Member 1077006325-Apr-14 7:58
memberMember 1077006325-Apr-14 7:58 
QuestionEmbed same application Pin
tieugiang9422-Mar-14 7:26
membertieugiang9422-Mar-14 7:26 
Questiondisabling title bar/borders Pin
Catronia17-Mar-14 2:54
memberCatronia17-Mar-14 2:54 
Questioncant download Pin
Member 1059546813-Feb-14 15:44
memberMember 1059546813-Feb-14 15:44 
GeneralMy vote of 1 Pin
Member 475388716-Jan-14 4:29
memberMember 475388716-Jan-14 4:29 
Questionhow can i host a C# application (exe file) in a win form in this project? Pin
mehrdad heidari24-Aug-13 11:06
membermehrdad heidari24-Aug-13 11:06 
GeneralFor those who can't get it working for any application other than notepad Pin
Christopher Sippel9-Apr-13 8:33
memberChristopher Sippel9-Apr-13 8:33 
Questiontoopstrip disabled Pin
Member 902164118-Feb-13 0:27
memberMember 902164118-Feb-13 0:27 
AnswerRe: toopstrip disabled Pin
bryonmvi16-Sep-14 8:46
memberbryonmvi16-Sep-14 8:46 
Questionwindows 7 problem Pin
Member 930102825-Jul-12 13:26
memberMember 930102825-Jul-12 13:26 
AnswerRe: windows 7 problem Pin
RaRi200323-Nov-12 3:46
memberRaRi200323-Nov-12 3:46 
AnswerRe: windows 7 problem Pin
Member 103874327-Nov-13 13:27
memberMember 103874327-Nov-13 13:27 
QuestionHosting External Exe Application in a WPF Window. [modified] Pin
sukesh.gudikandulla27-May-12 7:35
membersukesh.gudikandulla27-May-12 7:35 
QuestionIt doesn't work in window 7 Pin
dhojonline3-Apr-12 3:44
memberdhojonline3-Apr-12 3:44 
AnswerRe: It doesn't work in window 7 Pin
k3v1n5215-May-12 3:19
memberk3v1n5215-May-12 3:19 
GeneralRe: It doesn't work in window 7 Pin
Member 899490321-May-12 4:40
memberMember 899490321-May-12 4:40 
QuestionHow to stop exe flashing up or appearing in taskbar Pin
JamesA_Dev26-Jan-12 2:24
memberJamesA_Dev26-Jan-12 2:24 
QuestionMenus are disabled Pin
delfo8-Aug-11 23:07
memberdelfo8-Aug-11 23:07 
Hi, i have tried to host an application inside a windows forms on Win7 (with the bug fix corrected) and all works except that the menus and toolbar don't works.
Some one could help me ???
Thanks in advance
Stefano
AnswerRe: Menus are disabled Pin
bryonmvi16-Sep-14 8:47
memberbryonmvi16-Sep-14 8:47 
GeneralMy vote of 4 Pin
yanghe11173-May-11 5:16
memberyanghe11173-May-11 5:16 
GeneralMy vote of 5 Pin
Yusuf18-Jan-11 10:35
memberYusuf18-Jan-11 10:35 
QuestionAdvice needed Pin
HollyY18-Nov-10 13:57
memberHollyY18-Nov-10 13:57 
GeneralWin7 and Vista Incompatable Pin
cs_can3-Apr-10 4:28
membercs_can3-Apr-10 4:28 
QuestionPowerPoint Viewer Does Not seem to work Pin
Member 265354228-Dec-09 22:47
memberMember 265354228-Dec-09 22:47 
GeneralAnother problem Pin
il172316-Nov-09 6:36
memberil172316-Nov-09 6:36 
GeneralProblem Pin
il172316-Nov-09 5:59
memberil172316-Nov-09 5:59 
GeneralRe: Problem Pin
Elbe Alves Miranda24-Feb-11 4:24
memberElbe Alves Miranda24-Feb-11 4:24 
GeneralReally great, works perfectly after bug fix - thank you very much! Pin
Blasterwurm10-Nov-09 5:39
memberBlasterwurm10-Nov-09 5:39 
GeneralRe: Really great, works perfectly after bug fix - thank you very much! Pin
Atchyuta Rao9-Dec-09 21:29
memberAtchyuta Rao9-Dec-09 21:29 
GeneralRe: Really great, works perfectly after bug fix - thank you very much! Pin
cs_can3-Apr-10 6:41
membercs_can3-Apr-10 6:41 
GeneralApplication dosen't work Pin
treuveni26-Sep-09 10:37
membertreuveni26-Sep-09 10:37 
GeneralNice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified] Pin
Nikkiee28-May-09 8:00
memberNikkiee28-May-09 8:00 
GeneralRe: Nice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified] Pin
Blasterwurm10-Nov-09 3:18
memberBlasterwurm10-Nov-09 3:18 
GeneralRe: Nice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified] Pin
bencodeproject2515-Mar-11 12:33
memberbencodeproject2515-Mar-11 12:33 
GeneralRe: Nice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified] Pin
sukesh.gudikandulla27-May-12 8:43
membersukesh.gudikandulla27-May-12 8:43 
GeneralScrolling Pin
Wallbert Elicot22-Apr-09 2:39
memberWallbert Elicot22-Apr-09 2:39 
QuestionAll good for Microsoft Programs like notepad and Excel but what about others like winamp? Pin
noka036-Apr-09 21:31
membernoka036-Apr-09 21:31 
AnswerRe: All good for Microsoft Programs like notepad and Excel but what about others like winamp? [modified] Pin
Blasterwurm10-Nov-09 3:20
memberBlasterwurm10-Nov-09 3:20 
QuestionThe EXE application is a separate window, not hosted by the control! Pin
NotGood27-Feb-09 6:07
memberNotGood27-Feb-09 6:07 
QuestionRe: The EXE application is a separate window, not hosted by the control! Pin
pcm_it14-Mar-09 2:45
memberpcm_it14-Mar-09 2:45 
AnswerRe: The EXE application is a separate window, not hosted by the control! Pin
mbaocha6-May-09 17:43
membermbaocha6-May-09 17:43 
GeneralRe: The EXE application is a separate window, not hosted by the control! Pin
Blasterwurm10-Nov-09 4:37
memberBlasterwurm10-Nov-09 4:37 
QuestionAwesome!!! Pin
Member 440744029-Jan-09 6:30
memberMember 440744029-Jan-09 6:30 
QuestionRe: Awesome!!! Pin
Krishna01 Valluri5-Mar-10 1:20
memberKrishna01 Valluri5-Mar-10 1:20 
AnswerRe: Awesome!!! Pin
cs_can3-Apr-10 6:54
membercs_can3-Apr-10 6:54 
GeneralWorks great but... Pin
Member 447052825-Jan-09 9:31
memberMember 447052825-Jan-09 9:31 
QuestionRewritten with bug fix? Pin
Thord Johansson9-Dec-08 23:07
memberThord Johansson9-Dec-08 23:07 

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

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

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