Click here to Skip to main content
11,490,003 members (71,662 online)
Click here to Skip to main content

Hosting EXE Applications in a WPF Window Application

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

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. 

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.

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. 

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

Share

About the Author

Erxin
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 cn.linkedin.com/pub/shang-erxin/65/798/12a
Follow on   LinkedIn

Comments and Discussions

 
QuestionHow to send data / values to the embedded exe applications from the Host WPF Pin
Member 1148338113-May-15 6:10
memberMember 1148338113-May-15 6:10 
QuestionCan Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Member 114833812-Mar-15 11:35
memberMember 114833812-Mar-15 11:35 
AnswerRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Erxin2-Mar-15 19:59
memberErxin2-Mar-15 19:59 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Member 114833813-Mar-15 5:25
memberMember 114833813-Mar-15 5:25 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Member 114833816-Mar-15 6:32
memberMember 114833816-Mar-15 6:32 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Erxin12-Mar-15 23:11
memberErxin12-Mar-15 23:11 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Vergil Castelo6-Apr-15 8:56
memberVergil Castelo6-Apr-15 8:56 
GeneralRe: Can Unity3D game file be embedded in WPF application using C# Visual Studio2012? Pin
Member 114833817-May-15 10:25
memberMember 114833817-May-15 10:25 
QuestionChange the Control location on screen [modified] Pin
Member 356206518-Dec-14 4:01
memberMember 356206518-Dec-14 4:01 
AnswerRe: Change the Control location on screen Pin
Erxin14-Feb-15 15:54
memberErxin14-Feb-15 15:54 
AnswerRe: Change the Control location on screen Pin
Erxin14-Feb-15 15:57
memberErxin14-Feb-15 15:57 
QuestionHosting Multiple Applications Pin
kpolecastro10-Nov-14 13:57
professionalkpolecastro10-Nov-14 13:57 
AnswerRe: Hosting Multiple Applications Pin
Erxin17-Nov-14 2:32
memberErxin17-Nov-14 2:32 
GeneralRe: Hosting Multiple Applications Pin
kpolecastro18-Nov-14 8:27
professionalkpolecastro18-Nov-14 8:27 
QuestionAnother Custom WPF Application Pin
Member 1061413027-Oct-14 14:52
memberMember 1061413027-Oct-14 14:52 
AnswerRe: Another Custom WPF Application Pin
Erxin31-Oct-14 0:50
memberErxin31-Oct-14 0:50 
GeneralRe: Another Custom WPF Application Pin
NicholasTan31-Oct-14 22:04
memberNicholasTan31-Oct-14 22:04 
GeneralRe: Another Custom WPF Application Pin
Erxin17-Nov-14 2:45
memberErxin17-Nov-14 2:45 
GeneralThumbs Frickin Up! Pin
Member 1094594615-Oct-14 12:36
memberMember 1094594615-Oct-14 12:36 
GeneralRe: Thumbs Frickin Up! Pin
Erxin24-Oct-14 7:07
memberErxin24-Oct-14 7:07 
Questionerror, please can helpme Pin
Member 1087520219-Aug-14 3:03
memberMember 1087520219-Aug-14 3:03 
AnswerRe: error, please can helpme Pin
Erxin20-Aug-14 3:33
memberErxin20-Aug-14 3:33 
QuestionType of exe VB6 Pin
andrea giuntoli2-Aug-14 22:13
memberandrea giuntoli2-Aug-14 22:13 
AnswerRe: Type of exe VB6 Pin
Erxin8-Aug-14 17:52
memberErxin8-Aug-14 17:52 
QuestionHosted Buttons Disabled Pin
dokydok27-Jun-14 13:54
memberdokydok27-Jun-14 13:54 

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 | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 25 Oct 2013
Article Copyright 2013 by Erxin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid