Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#

Hosting EXE Applications in a WinForm Project

Rate me:
Please Sign up or sign in to vote.
4.70/5 (57 votes)
21 Dec 20041 min read 593K   27.2K   174   123
A custom control to launch and embed an EXE into a WinForm based application
In this post, you will learn about a custom C# control that allows you to specify the name of an executable you want embedded into your 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:

C#
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:

C#
// 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:

C#
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:

C#
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

  • 30th December, 2004: Initial release

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.


Written By
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.

Comments and Discussions

 
Questiontoopstrip disabled Pin
Member 902164117-Feb-13 23:27
Member 902164117-Feb-13 23:27 
AnswerRe: toopstrip disabled Pin
gotorg16-Sep-14 7:46
gotorg16-Sep-14 7:46 
Questionwindows 7 problem Pin
Member 930102825-Jul-12 12:26
Member 930102825-Jul-12 12:26 
AnswerRe: windows 7 problem Pin
RaRi200323-Nov-12 2:46
RaRi200323-Nov-12 2:46 
AnswerRe: windows 7 problem Pin
Member 103874327-Nov-13 12:27
Member 103874327-Nov-13 12:27 
QuestionHosting External Exe Application in a WPF Window. Pin
sukesh.gudikandulla27-May-12 6:35
sukesh.gudikandulla27-May-12 6:35 
QuestionIt doesn't work in window 7 Pin
dhojonline3-Apr-12 2:44
dhojonline3-Apr-12 2:44 
AnswerRe: It doesn't work in window 7 Pin
k3v1n5215-May-12 2:19
k3v1n5215-May-12 2:19 
Same problem for me.
Notepad is running fine in the panel. iexplore, excel, acrobat reader ... open as a popup.
GeneralRe: It doesn't work in window 7 Pin
Member 899490321-May-12 3:40
Member 899490321-May-12 3:40 
QuestionHow to stop exe flashing up or appearing in taskbar Pin
JamesA_Dev26-Jan-12 1:24
JamesA_Dev26-Jan-12 1:24 
QuestionMenus are disabled Pin
delfo8-Aug-11 22:07
delfo8-Aug-11 22:07 
AnswerRe: Menus are disabled Pin
gotorg16-Sep-14 7:47
gotorg16-Sep-14 7:47 
GeneralMy vote of 4 Pin
yanghe11173-May-11 4:16
yanghe11173-May-11 4:16 
GeneralMy vote of 5 Pin
Yusuf18-Jan-11 9:35
Yusuf18-Jan-11 9:35 
QuestionAdvice needed Pin
HollyY18-Nov-10 12:57
HollyY18-Nov-10 12:57 
GeneralWin7 and Vista Incompatable Pin
cs_can3-Apr-10 3:28
cs_can3-Apr-10 3:28 
QuestionPowerPoint Viewer Does Not seem to work Pin
Member 265354228-Dec-09 21:47
Member 265354228-Dec-09 21:47 
GeneralAnother problem Pin
il172316-Nov-09 5:36
il172316-Nov-09 5:36 
GeneralProblem Pin
il172316-Nov-09 4:59
il172316-Nov-09 4:59 
GeneralRe: Problem Pin
Elbe Alves Miranda24-Feb-11 3:24
Elbe Alves Miranda24-Feb-11 3:24 
GeneralReally great, works perfectly after bug fix - thank you very much! Pin
Blasterwurm10-Nov-09 4:39
Blasterwurm10-Nov-09 4:39 
GeneralRe: Really great, works perfectly after bug fix - thank you very much! Pin
Atchyuta Rao9-Dec-09 20:29
Atchyuta Rao9-Dec-09 20:29 
GeneralRe: Really great, works perfectly after bug fix - thank you very much! Pin
cs_can3-Apr-10 5:41
cs_can3-Apr-10 5:41 
GeneralApplication dosen't work Pin
treuveni26-Sep-09 9:37
treuveni26-Sep-09 9:37 
GeneralNice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified] Pin
Nikkiee28-May-09 7:00
Nikkiee28-May-09 7:00 

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.