Click here to Skip to main content
6,306,412 members and growing! (18,492 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.1VS.NET2003, Dev
Posted:21 Dec 2004
Views:87,543
Bookmarked:68 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
27 votes for this article.
Popularity: 6.03 Rating: 4.21 out of 5
1 vote, 3.8%
1

2
5 votes, 19.2%
3
5 votes, 19.2%
4
15 votes, 57.7%
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


Member
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
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 56 (Total in Forum: 56) (Refresh)FirstPrevNext
GeneralNice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified] PinmemberNikkiee8:00 28 May '09  
GeneralScrolling PinmemberWallbert Elicot2:39 22 Apr '09  
QuestionAll good for Microsoft Programs like notepad and Excel but what about others like winamp? Pinmembernoka0321:31 6 Apr '09  
QuestionThe EXE application is a separate window, not hosted by the control! PinmemberNotGood6:07 27 Feb '09  
QuestionRe: The EXE application is a separate window, not hosted by the control! Pinmemberpcm_it2:45 14 Mar '09  
AnswerRe: The EXE application is a separate window, not hosted by the control! Pinmembermbaocha17:43 6 May '09  
QuestionAwesome!!! PinmemberMember 44074406:30 29 Jan '09  
GeneralWorks great but... PinmemberMember 44705289:31 25 Jan '09  
GeneralRewritten with bug fix? PinmemberThord Johansson23:07 9 Dec '08  
Generalnice article PinmemberMember 237557710:59 29 Oct '08  
GeneralWow... Excellent PinmemberMrWolfy9:53 14 Sep '08  
GeneralCan't load sample code. Pinmembersupermankelly4:30 31 Jul '08  
QuestionQuestions about really cool component Pinmemberjbyrneiu17:05 22 Jul '08  
GeneralVista incompatibility PinmemberAndrei Pana13:14 23 Mar '08  
GeneralRe: Vista incompatibility PinmemberEnquiren21:32 4 Jul '08  
GeneralRe: Vista incompatibility PinmemberEnquiren21:38 4 Jul '08  
QuestionForm visibility Pinmemberputzol22:35 27 Sep '07  
GeneralNice man! PinmemberBohemianDre9:57 20 Jul '07  
GeneralErase background PinmemberHHGClark5:51 18 Jul '07  
GeneralRe: Erase background Pinmemberbenjamin2310:41 13 Apr '08  
GeneralA call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pinmembersinclas6:21 18 May '07  
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pinmemberrimblock13:26 10 Jun '07  
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pinmemberhrdpatel7:30 23 Jul '07  
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' PinmemberWindows M5:12 11 Aug '07  
GeneralRe: A call to PInvoke function 'AppControl!AppControl.ApplicationControl::SetWindowLong' Pinmembersagyla14:19 31 Aug '07  

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-2009
Web17 | Advertise on the Code Project