Click here to Skip to main content
12,890,424 members (58,996 online)
Click here to Skip to main content
Add your own
alternative version


159 bookmarked
Posted 21 Dec 2004

Hosting EXE Applications in a WinForm project

, 21 Dec 2004
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


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; 
  // Start the process 
  p = System.Diagnostics.Process.Start(this.exeName); 

  // Wait for process to be created and enter idle condition 

  // 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

    // Clear internal handle
    appWin = IntPtr.Zero;


  base.OnHandleDestroyed (e);


  • 12-20-2004: Released.


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
Architect Omron Adept Technologies, Inc
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 company that manufacturers industrial robots where I write high level coordination and control software.

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 am also have the PMI-ACP certification.

I enjoy karate and reading.

You may also be interested in...

Comments and Discussions

Questionfail Pin
machado194-Apr-16 9:48
membermachado194-Apr-16 9:48 
QuestionNot Working the way it should Pin
Member 1207493623-Nov-15 21:43
memberMember 1207493623-Nov-15 21:43 
QuestionHow to send data / values to the embedded exe applications from the Host WPF Pin
Member 1148338119-May-15 5:26
memberMember 1148338119-May-15 5:26 
QuestionEmbed Unity3D Exe in WPF Pin
Member 114833814-Mar-15 11:16
memberMember 114833814-Mar-15 11:16 
AnswerRe: Embed Unity3D Exe in WPF Pin
Muhammad Umair9-Mar-15 17:56
memberMuhammad Umair9-Mar-15 17:56 
GeneralRe: Embed Unity3D Exe in WPF Pin
Member 1148338110-Mar-15 3:30
memberMember 1148338110-Mar-15 3:30 
GeneralRe: Embed Unity3D Exe in WPF Pin
Muhammad Umair27-Mar-15 6:37
memberMuhammad Umair27-Mar-15 6:37 
GeneralRe: Embed Unity3D Exe in WPF Pin
Member 1148338127-Mar-15 7:18
memberMember 1148338127-Mar-15 7:18 
QuestionGet if is hosted application still running. Pin
Member 109639464-Mar-15 5:38
memberMember 109639464-Mar-15 5:38 
QuestionThe application is not opening in the panel. Pin
Athirams00120-Oct-14 1:48
memberAthirams00120-Oct-14 1:48 
AnswerRe: The application is not opening in the panel. Pin
RoykinNg23-Nov-14 19:31
memberRoykinNg23-Nov-14 19:31 
QuestionSending input from the host Pin
bluebristolian22-Jul-14 17:57
memberbluebristolian22-Jul-14 17:57 
QuestionIexplore launches separately Pin
Ob1kanami18-Jul-14 4:16
memberOb1kanami18-Jul-14 4:16 
AnswerRe: Iexplore launches separately Pin
Member 1075482013-Aug-14 21:23
memberMember 1075482013-Aug-14 21:23 
QuestionI need to be able to exit and restart the embedded app? Any ideas how? Pin
Jybs2811-Jun-14 11:47
memberJybs2811-Jun-14 11:47 
AnswerRe: I need to be able to exit and restart the embedded app? Any ideas how? Pin
ElectroMonster4-Nov-15 18:58
memberElectroMonster4-Nov-15 18:58 
Questionwhy can't I download it Pin
Member 1078704930-Apr-14 16:35
memberMember 1078704930-Apr-14 16:35 
QuestionChlid forms in the hosted application don't show up Pin
Member 1077006325-Apr-14 6:58
memberMember 1077006325-Apr-14 6:58 
QuestionEmbed same application Pin
tieugiang9422-Mar-14 6:26
membertieugiang9422-Mar-14 6:26 
Questiondisabling title bar/borders Pin
Catronia17-Mar-14 1:54
memberCatronia17-Mar-14 1:54 
I was able to use this to embed my .exe Unity3d application into windows form. However, I cannot host it without title bar. I have removed it inside my .exe app code:

* Custom fullscreen and Borderless window script by Martijn Dekker (Pixelstudio) 
* For questions pls contact met at 
* version 0.1 
using System;  
using System.Collections;  
using System.Runtime.InteropServices;  
using System.Diagnostics;  
using UnityEngine;  
public class WindowMod : MonoBehaviour  
public Rect screenPosition;  
static extern IntPtr SetWindowLong (IntPtr hwnd,int  _nIndex ,int  dwNewLong);  
static extern bool SetWindowPos (IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); 
static extern IntPtr GetForegroundWindow ();  
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
// not used rigth now  
//const uint SWP_NOMOVE = 0x2;  
//const uint SWP_NOSIZE = 1;  
//const uint SWP_NOZORDER = 0x4;  
//const uint SWP_HIDEWINDOW = 0x0080;  
const uint SWP_SHOWWINDOW = 0x0040;  
const int GWL_EXSTYLE = -20;  
const int GWL_STYLE = -16;  
const int WS_BORDER = 1;  
const int WS_EX_LAYERED = 0x80000;  
public const int LWA_ALPHA = 0x2;
public const int LWA_COLORKEY = 0x1;
void Start ()  {  
string WINDOW_NAME = "Test Rig"; 
IntPtr window = FindWindowByCaption(IntPtr.Zero, WINDOW_NAME);
SetWindowLong(window, GWL_EXSTYLE, WS_EX_LAYERED);  
SetWindowLong(window, GWL_STYLE, WS_BORDER);  
SetLayeredWindowAttributes(window, 0, 0, LWA_COLORKEY);
bool result = SetWindowPos (window, 0,(int)screenPosition.x,(int)screenPosition.y, (int)screenPosition.width,(int) screenPosition.height, SWP_SHOWWINDOW);  

When I run the app outside, it runs without title bar and borders, but when I run it from application control it shows borders and title bar.

Any suggestions?
AnswerRe: disabling title bar/borders Pin
Michael Hall11-Jan-17 13:13
memberMichael Hall11-Jan-17 13:13 
Questioncant download Pin
Member 1059546813-Feb-14 14:44
memberMember 1059546813-Feb-14 14:44 
GeneralMy vote of 1 Pin
Member 475388716-Jan-14 3:29
memberMember 475388716-Jan-14 3:29 
Questionhow can i host a C# application (exe file) in a win form in this project? Pin
mehrdad heidari24-Aug-13 10:06
membermehrdad heidari24-Aug-13 10:06 
GeneralFor those who can't get it working for any application other than notepad Pin
Christopher Sippel9-Apr-13 7:33
memberChristopher Sippel9-Apr-13 7:33 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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