Click here to Skip to main content
Click here to Skip to main content

Hosting EXE Applications in a WinForm project

By , 21 Dec 2004
 

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralFor those who can't get it working for any application other than notepadmemberChristopher Sippel9 Apr '13 - 7:33 
Questiontoopstrip disabledmemberMember 902164117 Feb '13 - 23:27 
Questionwindows 7 problemmemberMember 930102825 Jul '12 - 12:26 
i tryed with windows 7 and dont works.. Some1 have a solution?
AnswerRe: windows 7 problemmemberRaRi200323 Nov '12 - 2:46 
QuestionHosting External Exe Application in a WPF Window. [modified]membersukesh.gudikandulla27 May '12 - 6:35 
QuestionIt doesn't work in window 7memberdhojonline3 Apr '12 - 2:44 
AnswerRe: It doesn't work in window 7memberk3v1n5215 May '12 - 2:19 
GeneralRe: It doesn't work in window 7memberMember 899490321 May '12 - 3:40 
QuestionHow to stop exe flashing up or appearing in taskbarmemberJamesA_Dev26 Jan '12 - 1:24 
QuestionMenus are disabledmemberdelfo8 Aug '11 - 22:07 
GeneralMy vote of 4memberyanghe11173 May '11 - 4:16 
GeneralMy vote of 5memberYusuf18 Jan '11 - 9:35 
QuestionAdvice neededmemberHollyY18 Nov '10 - 12:57 
GeneralWin7 and Vista Incompatablemembercs_can3 Apr '10 - 3:28 
QuestionPowerPoint Viewer Does Not seem to workmemberMember 265354228 Dec '09 - 21:47 
GeneralAnother problemmemberil172316 Nov '09 - 5:36 
GeneralProblemmemberil172316 Nov '09 - 4:59 
GeneralRe: ProblemmemberElbe Alves Miranda24 Feb '11 - 3:24 
GeneralReally great, works perfectly after bug fix - thank you very much!memberBlasterwurm10 Nov '09 - 4:39 
GeneralRe: Really great, works perfectly after bug fix - thank you very much!memberAtchyuta Rao9 Dec '09 - 20:29 
GeneralRe: Really great, works perfectly after bug fix - thank you very much!membercs_can3 Apr '10 - 5:41 
GeneralApplication dosen't workmembertreuveni26 Sep '09 - 9:37 
GeneralNice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified]memberNikkiee28 May '09 - 7:00 
GeneralRe: Nice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified]memberBlasterwurm10 Nov '09 - 2:18 
GeneralRe: Nice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified]memberbencodeproject2515 Mar '11 - 11:33 
GeneralRe: Nice Article... But one clarification required ... Running the Code..it;s launching the custom control and IE .. separately [modified]membersukesh.gudikandulla27 May '12 - 7:43 
GeneralScrollingmemberWallbert Elicot22 Apr '09 - 1:39 
QuestionAll good for Microsoft Programs like notepad and Excel but what about others like winamp?membernoka036 Apr '09 - 20:31 
AnswerRe: All good for Microsoft Programs like notepad and Excel but what about others like winamp? [modified]memberBlasterwurm10 Nov '09 - 2:20 
QuestionThe EXE application is a separate window, not hosted by the control!memberNotGood27 Feb '09 - 5:07 
QuestionRe: The EXE application is a separate window, not hosted by the control!memberpcm_it14 Mar '09 - 1:45 
AnswerRe: The EXE application is a separate window, not hosted by the control!membermbaocha6 May '09 - 16:43 
GeneralRe: The EXE application is a separate window, not hosted by the control!memberBlasterwurm10 Nov '09 - 3:37 
QuestionAwesome!!!memberMember 440744029 Jan '09 - 5:30 
QuestionRe: Awesome!!!memberKrishna01 Valluri5 Mar '10 - 0:20 
AnswerRe: Awesome!!!membercs_can3 Apr '10 - 5:54 
GeneralWorks great but...memberMember 447052825 Jan '09 - 8:31 
QuestionRewritten with bug fix?memberThord Johansson9 Dec '08 - 22:07 
Generalnice articlememberMember 237557729 Oct '08 - 9:59 
GeneralWow... ExcellentmemberMrWolfy14 Sep '08 - 8:53 
GeneralCan't load sample code.membersupermankelly31 Jul '08 - 3:30 
QuestionQuestions about really cool componentmemberjbyrneiu22 Jul '08 - 16:05 
GeneralVista incompatibilitymemberAndrei Pana23 Mar '08 - 12:14 
GeneralRe: Vista incompatibilitymemberEnquiren4 Jul '08 - 20:32 
GeneralRe: Vista incompatibilitymemberEnquiren4 Jul '08 - 20:38 
AnswerRe: Vista incompatibilitymemberAndre Wells22 Aug '09 - 4:04 
GeneralRe: Vista incompatibilitymemberAtchyuta Rao9 Dec '09 - 20:03 
GeneralRe: Vista incompatibilitymemberMember 869768711 Apr '12 - 6:15 
GeneralRe: Vista incompatibilitymemberShulha Yahya8 Jun '12 - 10:51 
GeneralRe: Vista incompatibilitymemberShulha Yahya8 Jun '12 - 11:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 21 Dec 2004
Article Copyright 2004 by Jay Nelson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid