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

Hosting EXE Applications in a WPF Window Application

, 25 Oct 2013
Rate this:
Please Sign up or sign in to vote.
Introduce a simple way to embed an EXE into a WPF window application.


This article introduces a way to host an EXE in a WPF window. To make the code easy to reuse, it is set into a WPF user control and also implements the IDisposable interface. A test WPF window app is added as a test project.  


As a development request I needed to embed a wPython Python edit and compare tool, built by me, into a newly developed WPF application. With the magic power of search engines, I found an article in CodeProject on embedding an EXE into a WinForms program. It's needed to make several changes to become a new WPF control. I also added the IDisposable interface to improve the resource control ability. The links to the reference articles are listed here:

Using the code 

To use the control, we only need to specify the full path of the executable to be embedded and when to dispose it. 

appControl.ExeName = "notepad.exe";
this.Unloaded += new RoutedEventHandler((s, e) => { appControl.Dispose(); }); 

How does it works 

The embedded application is launched with its containing directory as a working directory.

  var procInfo = new System.Diagnostics.ProcessStartInfo(this.exeName);
  procInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(this.exeName);
  // Start the process
  _childp = System.Diagnostics.Process.Start(procInfo);

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

  // Get the main handle
  _appWin = _childp.MainWindowHandle;
catch (Exception ex)
  Debug.Print(ex.Message + "Error");

Get the container WPF control's handle and set the embedded application's parent window to the container. Change the style for the embedded application. 

// Put it into this form
var helper = new WindowInteropHelper(Window.GetWindow(this.AppContainer));
SetParent(_appWin, helper.Handle);

// Remove border and whatnot
SetWindowLongA(_appWin, GWL_STYLE, WS_VISIBLE);

// Move the window to overlay it on this window
MoveWindow(_appWin, 0, 0, (int)this.ActualWidth, (int)this.ActualHeight, true); 

The embedded application is disposed when the container is disposed.

if (!_isdisposed)
  if (disposing)
      if (_iscreated && _appWin != IntPtr.Zero && !_childp.HasExited)
          // Stop the application

          // Clear internal handle
          _appWin = IntPtr.Zero;
  _isdisposed = true;

Points of Interest 

Some of the embedded applications can't be closed as mentioned in the article: Hosting EXE Application in a WinForms project. So it is changed to use the Process.Kill() method to make sure the embedded application is closed.  

Also the signature of SetWindowLongA is changed a little as the previous one throws an error in the VS2012 compiler.  


  • 2013-10-25: Released.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Architect Micron
China China
My undergraduate degree is Electrical Engineering and Automation. Graduate degree is Power Electronics and Power Drives. Personal favorite programming languages are C, C++, C# and Python.
Currently I works as an architect/ software engineer/ programmer in Micron. During the free time, I also work as a free-lance programmer. Have good knowledge of, WCF, WPF, WF, Silverlight, Django, wxPython, jquery, SQL Server, SQLite, Mongo DB, Mobile development with Mono and Unity. If you want to find a free-lance programmer to solve problems with these techniques feel free to contact me.
Find me @linkedin from
Follow on   LinkedIn

Comments and Discussions

QuestionHosted Buttons Disabled Pinmemberdokydok27-Jun-14 12:54 
QuestionMultiple Instances in ToolWindow PinmemberVishalShah92-Jun-14 0:32 
AnswerRe: Multiple Instances in ToolWindow PinmemberErxin2-Jun-14 2:08 
GeneralRe: Multiple Instances in ToolWindow [modified] PinmemberVishalShah92-Jun-14 18:44 
GeneralRe: Multiple Instances in ToolWindow PinmemberErxin2-Jun-14 19:23 
GeneralRe: Multiple Instances in ToolWindow PinmemberVishalShah92-Jun-14 20:17 
GeneralRe: Multiple Instances in ToolWindow PinmemberErxin3-Jun-14 2:08 
GeneralRe: Multiple Instances in ToolWindow PinmemberVishalShah94-Jun-14 0:49 
GeneralRe: Multiple Instances in ToolWindow PinmemberErxin4-Jun-14 2:12 
GeneralRe: Multiple Instances in ToolWindow PinmemberVishalShah98-Jun-14 18:19 
QuestionAppContainer PinmemberUni Chen15-May-14 22:41 
AnswerRe: AppContainer PinmemberErxin24-May-14 3:28 
Questionmspaint opens as a pop up PinmemberfarighAadmi28-Feb-14 2:19 
AnswerRe: mspaint opens as a pop up PinmemberErxin20-Mar-14 15:33 
GeneralRe: mspaint opens as a pop up PinmemberKevinQiang.K5-May-14 2:51 
GeneralRe: mspaint opens as a pop up PinmemberErxin5-May-14 3:41 
GeneralRe: mspaint opens as a pop up PinmemberKevinQiang.K12-May-14 17:32 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 25 Oct 2013
Article Copyright 2013 by Erxin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid