Click here to Skip to main content
11,483,003 members (75,096 online)
Click here to Skip to main content

Hosting EXE Applications in a WinForm project

, 21 Dec 2004 265.5K 13K 148
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

 
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 
I've used this sample to create a Windows Home Server Add-In that hosts another exe file. Works fint except for one thing. The application in questions apparently uses a timer to refresh it's content. When running standalone there is no problem, but when running inside a WinForm using the code from this article the applications "flickers" once a second. I assume this is some kind of refresh thread/timer doing something.

Is there any double buffering or other technique I can use that might help?
QuestionRewritten with bug fix? Pin
Thord Johansson9-Dec-08 23:07
memberThord Johansson9-Dec-08 23:07 
Generalnice article Pin
Member 237557729-Oct-08 10:59
memberMember 237557729-Oct-08 10:59 
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 
GeneralErase background Pin
HHGClark18-Jul-07 5:51
memberHHGClark18-Jul-07 5:51 

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