Click here to Skip to main content
15,867,985 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 590.4K   27.2K   173   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

 
AnswerRe: The EXE application is a separate window, not hosted by the control! Pin
mbaocha6-May-09 16:43
mbaocha6-May-09 16:43 
GeneralRe: The EXE application is a separate window, not hosted by the control! Pin
Blasterwurm10-Nov-09 3:37
Blasterwurm10-Nov-09 3:37 
QuestionAwesome!!! Pin
Member 440744029-Jan-09 5:30
Member 440744029-Jan-09 5:30 
QuestionRe: Awesome!!! Pin
Krishna01 Valluri5-Mar-10 0:20
Krishna01 Valluri5-Mar-10 0:20 
AnswerRe: Awesome!!! Pin
cs_can3-Apr-10 5:54
cs_can3-Apr-10 5:54 
GeneralWorks great but... Pin
Pål Andreassen25-Jan-09 8:31
Pål Andreassen25-Jan-09 8:31 
QuestionRewritten with bug fix? Pin
Thord Johansson9-Dec-08 22:07
Thord Johansson9-Dec-08 22:07 
Generalnice article Pin
Member 237557729-Oct-08 9:59
Member 237557729-Oct-08 9:59 
GeneralWow... Excellent Pin
Saul Johnson14-Sep-08 8:53
Saul Johnson14-Sep-08 8:53 
GeneralCan't load sample code. Pin
supermankelly31-Jul-08 3:30
supermankelly31-Jul-08 3:30 
QuestionQuestions about really cool component Pin
jbyrneiu22-Jul-08 16:05
jbyrneiu22-Jul-08 16:05 
This is exactly what I was looking for.

Is there a way to keep the app in "exename" from starting in design mode? Not a problem, per se, but I only want to start the app at runtime.

Will this work in Vista?

Thanks for your great component. It has made my app much easier to use!

John Byrne Smile | :)
GeneralVista incompatibility Pin
Andrei Pana23-Mar-08 12:14
Andrei Pana23-Mar-08 12:14 
GeneralRe: Vista incompatibility Pin
Enquiren4-Jul-08 20:32
Enquiren4-Jul-08 20:32 
GeneralRe: Vista incompatibility Pin
Enquiren4-Jul-08 20:38
Enquiren4-Jul-08 20:38 
AnswerRe: Vista incompatibility PinPopular
Andre Wells22-Aug-09 4:04
Andre Wells22-Aug-09 4:04 
GeneralRe: Vista incompatibility Pin
Atchyuta Rao9-Dec-09 20:03
Atchyuta Rao9-Dec-09 20:03 
GeneralRe: Vista incompatibility Pin
Member 869768711-Apr-12 6:15
Member 869768711-Apr-12 6:15 
GeneralRe: Vista incompatibility Pin
Shulha Yahya8-Jun-12 10:51
Shulha Yahya8-Jun-12 10:51 
GeneralRe: Vista incompatibility Pin
Shulha Yahya8-Jun-12 11:15
Shulha Yahya8-Jun-12 11:15 
GeneralRe: Vista incompatibility Pin
pb1513-Dec-12 6:39
pb1513-Dec-12 6:39 
QuestionForm visibility Pin
putzol27-Sep-07 21:35
putzol27-Sep-07 21:35 
GeneralNice man! Pin
BohemianDre20-Jul-07 8:57
BohemianDre20-Jul-07 8:57 
GeneralErase background Pin
HHGClark18-Jul-07 4:51
HHGClark18-Jul-07 4:51 
GeneralRe: Erase background Pin
benjamin2313-Apr-08 9:41
benjamin2313-Apr-08 9:41 
GeneralA call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pin
sinclas18-May-07 5:21
sinclas18-May-07 5:21 

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.