Click here to Skip to main content
15,884,298 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.2K   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

 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Erxin12-Mar-15 22:11
Erxin12-Mar-15 22:11 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Vergil Castelo6-Apr-15 7:56
Vergil Castelo6-Apr-15 7:56 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Member 114833817-May-15 9:25
Member 114833817-May-15 9:25 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
L0J1R11-May-17 10:18
L0J1R11-May-17 10:18 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
AZ2Tonez18-May-17 12:13
AZ2Tonez18-May-17 12:13 
QuestionChange the Control location on screen Pin
Member 356206518-Dec-14 3:01
Member 356206518-Dec-14 3:01 
AnswerRe: Change the Control location on screen Pin
Erxin14-Feb-15 14:54
Erxin14-Feb-15 14:54 
AnswerRe: Change the Control location on screen Pin
Erxin14-Feb-15 14:57
Erxin14-Feb-15 14:57 
Sorry for the late response due to busy working in these monthes. Here is one of the way to locate the control with X, Y coordinates.

You could find the method from my previous replay at
Quote:
Re: Multiple Instances in ToolWindow Pin member Erxin 4-Jun-14 8:12


Thank you.
Erxin

AnswerRe: Change the Control location on screen Pin
Sacha Barber29-May-15 5:48
Sacha Barber29-May-15 5:48 
QuestionHosting Multiple Applications Pin
kpolecastro10-Nov-14 12:57
professionalkpolecastro10-Nov-14 12:57 
AnswerRe: Hosting Multiple Applications Pin
Erxin17-Nov-14 1:32
Erxin17-Nov-14 1:32 
GeneralRe: Hosting Multiple Applications Pin
kpolecastro18-Nov-14 7:27
professionalkpolecastro18-Nov-14 7:27 
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 
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 

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.