Click here to Skip to main content
15,867,141 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 362.5K   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

 
QuestionAnother Custom WPF Application Pin
NicholasTan27-Oct-14 13:52
NicholasTan27-Oct-14 13:52 
AnswerRe: Another Custom WPF Application Pin
Erxin30-Oct-14 23:50
Erxin30-Oct-14 23:50 
GeneralRe: Another Custom WPF Application Pin
NicholasTan31-Oct-14 21:04
NicholasTan31-Oct-14 21:04 
GeneralRe: Another Custom WPF Application Pin
Erxin17-Nov-14 1:45
Erxin17-Nov-14 1:45 
GeneralThumbs Frickin Up! Pin
Member 1094594615-Oct-14 11:36
Member 1094594615-Oct-14 11:36 
GeneralRe: Thumbs Frickin Up! Pin
Erxin24-Oct-14 6:07
Erxin24-Oct-14 6:07 
Questionerror, please can helpme Pin
Member 1087520219-Aug-14 2:03
Member 1087520219-Aug-14 2:03 
AnswerRe: error, please can helpme Pin
Erxin20-Aug-14 2:33
Erxin20-Aug-14 2:33 
You could try to use design model property to avoid run the relative codes which trigger the exception, http://msdn.microsoft.com/en-us/library/system.componentmodel.designerproperties.getisindesignmode.aspx[^]
QuestionType of exe VB6 Pin
andrea giuntoli2-Aug-14 21:13
andrea giuntoli2-Aug-14 21:13 
AnswerRe: Type of exe VB6 Pin
Erxin8-Aug-14 16:52
Erxin8-Aug-14 16:52 
QuestionHosted Buttons Disabled Pin
dokydok27-Jun-14 12:54
dokydok27-Jun-14 12:54 
AnswerRe: Hosted Buttons Disabled Pin
Erxin7-Aug-14 22:22
Erxin7-Aug-14 22:22 
QuestionMultiple Instances in ToolWindow Pin
VishalShah92-Jun-14 0:32
VishalShah92-Jun-14 0:32 
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 
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 

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.