Click here to Skip to main content
15,887,027 members
Articles / Desktop Programming / WPF
Tip/Trick

Hosting EXE Applications in a WPF Window Application

Rate me:
Please Sign up or sign in to vote.
4.97/5 (20 votes)
25 Oct 2013CPOL1 min read 364.6K   9.1K   32   80
Introduce a simple way to embed an EXE into a WPF window application.

Image 1

Introduction 

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.  

Background 

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. 

C#
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.

C#
try
{
  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
  _childp.WaitForInputIdle();

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

C#
// 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.

C#
if (!_isdisposed)
{
  if (disposing)
  {
      if (_iscreated && _appWin != IntPtr.Zero && !_childp.HasExited)
      {
          // Stop the application
          _childp.Kill();

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

History

  • 2013-10-25: Released.

License

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


Written By
Architect MicroFocus
China China
Love programming and have fun!

Comments and Discussions

 
AnswerRe: Multiple Instances in ToolWindow Pin
Erxin2-Jun-14 2:08
Erxin2-Jun-14 2:08 
GeneralRe: Multiple Instances in ToolWindow Pin
VishalShah92-Jun-14 18:44
VishalShah92-Jun-14 18:44 
GeneralRe: Multiple Instances in ToolWindow Pin
Erxin2-Jun-14 19:23
Erxin2-Jun-14 19:23 
GeneralRe: Multiple Instances in ToolWindow Pin
VishalShah92-Jun-14 20:17
VishalShah92-Jun-14 20:17 
GeneralRe: Multiple Instances in ToolWindow Pin
Erxin3-Jun-14 2:08
Erxin3-Jun-14 2:08 
GeneralRe: Multiple Instances in ToolWindow Pin
VishalShah94-Jun-14 0:49
VishalShah94-Jun-14 0:49 
GeneralRe: Multiple Instances in ToolWindow Pin
Erxin4-Jun-14 2:12
Erxin4-Jun-14 2:12 
GeneralRe: Multiple Instances in ToolWindow Pin
VishalShah98-Jun-14 18:19
VishalShah98-Jun-14 18:19 
Hi so I started from scratch again and I'm seeing some weird behavior.

If the toolwindow is originally docked (during initialization) then the positions that I specify in MoveWindow need to be absolute positions on the screen. (0,0) refers to the extreme top left of the visual studio window and thus overlapping with everything. Also, on moving the toolwindow, the external application does not move along with it.

However, if the toolwindow is not docked originally (on initialization) then the positions specified in the toolwindow are relative to the toolwindow. (0,0) will refer to the topleft of the toolwindow and not the complete visual studio window. This works well for me but upon docking the application gets killed. I've put breakpoints for the dispose function in the appcontrol but it never seems to be called when docking the toolwindow and yet the application still exits. Any idea why?
GeneralRe: Multiple Instances in ToolWindow Pin
Erxin7-Aug-14 22:16
Erxin7-Aug-14 22:16 
QuestionAppContainer Pin
Uni Chen15-May-14 22:41
Uni Chen15-May-14 22:41 
AnswerRe: AppContainer Pin
Erxin24-May-14 3:28
Erxin24-May-14 3:28 
Questionmspaint opens as a pop up Pin
Member 811577528-Feb-14 2:19
Member 811577528-Feb-14 2:19 
AnswerRe: mspaint opens as a pop up Pin
Erxin20-Mar-14 15:33
Erxin20-Mar-14 15:33 
GeneralRe: mspaint opens as a pop up Pin
KevinQiang.K5-May-14 2:51
KevinQiang.K5-May-14 2:51 
GeneralRe: mspaint opens as a pop up Pin
Erxin5-May-14 3:41
Erxin5-May-14 3:41 
GeneralRe: mspaint opens as a pop up Pin
KevinQiang.K12-May-14 17:32
KevinQiang.K12-May-14 17:32 
GeneralRe: mspaint opens as a pop up Pin
Member 1044935116-Oct-14 1:30
Member 1044935116-Oct-14 1:30 
GeneralRe: mspaint opens as a pop up Pin
Member 1134536520-Mar-18 8:58
Member 1134536520-Mar-18 8:58 
GeneralRe: mspaint opens as a pop up Pin
Erxin28-Mar-18 21:09
Erxin28-Mar-18 21:09 

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.