![]() |
Desktop Development »
Miscellaneous »
Miscellaneous Controls
Intermediate
Hosting EXE Applications in a WinForm projectBy Jay NelsonA custom control to launch and embed an EXE into a WinForm based application. |
C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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);
}
General
News
Question
Answer
Joke
Rant
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 |