5,662,937 members and growing! (21,813 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate

Hosting EXE Applications in a WinForm project

By Jay Nelson

A custom control to launch and embed an EXE into a WinForm based application.
C#, Windows, .NET 1.0, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 21 Dec 2004
Updated: 21 Dec 2004
Views: 74,643
Bookmarked: 58 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
25 votes for this Article.
Popularity: 5.86 Rating: 4.20 out of 5
1 vote, 4.2%
1
0 votes, 0.0%
2
5 votes, 20.8%
3
4 votes, 16.7%
4
14 votes, 58.3%
5

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

About the Author

Jay Nelson


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.
Occupation: Web Developer
Location: United States United States

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 46 (Total in Forum: 46) (Refresh)FirstPrevNext
Generalnice articlememberMember 237557710:59 29 Oct '08  
GeneralWow... ExcellentmemberMrWolfy9:53 14 Sep '08  
GeneralCan't load sample code.membersupermankelly4:30 31 Jul '08  
QuestionQuestions about really cool componentmemberjbyrneiu17:05 22 Jul '08  
GeneralVista incompatibilitymemberAndrei Pana13:14 23 Mar '08  
GeneralRe: Vista incompatibilitymemberEnquiren21:32 4 Jul '08  
GeneralRe: Vista incompatibilitymemberEnquiren21:38 4 Jul '08  
QuestionForm visibilitymemberputzol22:35 27 Sep '07  
GeneralNice man!memberBohemianDre9:57 20 Jul '07  
GeneralErase backgroundmemberHHGClark5:51 18 Jul '07  
GeneralRe: Erase backgroundmemberbenjamin2310:41 13 Apr '08  
GeneralA call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong'membersinclas6:21 18 May '07  
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong'memberrimblock13:26 10 Jun '07  
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong'memberhrdpatel7:30 23 Jul '07  
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong'memberWindows M5:12 11 Aug '07  
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong'membersagyla14:19 31 Aug '07  
Generalcan't start programe that have login window.membercaixrz17:56 11 Apr '07  
QuestionHosting exe in Com applicationmemberVismay_Dhonsale5:40 10 Apr '07  
GeneralError "this.Controls.Add(this.applicationControl1);"memberSheikko0:50 9 Jan '07  
AnswerRe: Error "this.Controls.Add(this.applicationControl1);"memberElric-Wang15:27 25 Feb '07  
GeneralRe: Error "this.Controls.Add(this.applicationControl1);"memberdontknow6113:28 15 Apr '07  
GeneralRe: Error "this.Controls.Add(this.applicationControl1);"membersupermankelly4:37 31 Jul '08  
GeneralAppControle class rewrite for vb.netmembermadbison13:10 28 Jul '06  
Generalget the current directory of hosted iexploremembermalai kofta11:13 9 Oct '05  
Generalerrormembermalai kofta10:47 11 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Dec 2004
Editor: Smitha Vijayan
Copyright 2004 by Jay Nelson
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project