Click here to Skip to main content
15,861,168 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 361.9K   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

 
QuestionError Value can not be null Pin
tsampika3-Aug-20 23:00
tsampika3-Aug-20 23:00 
QuestionCustom .exe does not getting embaded Pin
arindamrudra29-Aug-18 3:37
professionalarindamrudra29-Aug-18 3:37 
QuestionNot working even with sleep Pin
morenoshad29-Jan-18 0:41
morenoshad29-Jan-18 0:41 
AnswerRe: Not working even with sleep Pin
Member 144722847-Jul-19 20:46
Member 144722847-Jul-19 20:46 
AnswerRe: Not working even with sleep Pin
Brian C Hart28-Jul-22 3:58
professionalBrian C Hart28-Jul-22 3:58 
QuestionNot Working Pin
Member 1232950126-Apr-17 11:09
Member 1232950126-Apr-17 11:09 
AnswerRe: Not Working Pin
Erxin20-May-17 15:47
Erxin20-May-17 15:47 
AnswerRe: Not Working Pin
Member 1089718724-May-17 5:48
Member 1089718724-May-17 5:48 
QuestionNot working in VS2013 / VS2015 Pin
tk79_de14-Feb-17 23:33
tk79_de14-Feb-17 23:33 
AnswerRe: Not working in VS2013 / VS2015 Pin
AZ2Tonez18-May-17 11:37
AZ2Tonez18-May-17 11:37 
PraiseThanksgiving Pin
tkdmsg11-Aug-16 4:18
tkdmsg11-Aug-16 4:18 
GeneralRe: Thanksgiving Pin
Erxin21-Sep-16 20:10
Erxin21-Sep-16 20:10 
Questionwhen minimized and restore unity app did not diaplay anything Pin
umer jamil3-Feb-16 6:24
umer jamil3-Feb-16 6:24 
AnswerRe: when minimized and restore unity app did not diaplay anything Pin
Erxin25-Feb-16 19:16
Erxin25-Feb-16 19:16 
Questionthanks Pin
sahinci29-Jan-16 1:56
sahinci29-Jan-16 1:56 
AnswerRe: thanks Pin
Erxin25-Feb-16 19:18
Erxin25-Feb-16 19:18 
QuestionCode can not be used with other "*.exe" files. Why? Pin
Member 109481535-Oct-15 6:01
Member 109481535-Oct-15 6:01 
AnswerRe: Code can not be used with other "*.exe" files. Why? Pin
Member 120375076-Oct-15 21:42
Member 120375076-Oct-15 21:42 
GeneralRe: Code can not be used with other "*.exe" files. Why? Pin
Member 1094815310-Oct-15 8:54
Member 1094815310-Oct-15 8:54 
GeneralRe: Code can not be used with other "*.exe" files. Why? Pin
Erxin12-Nov-15 20:14
Erxin12-Nov-15 20:14 
GeneralRe: Code can not be used with other "*.exe" files. Why? Pin
enrmadriv17-Aug-18 8:06
enrmadriv17-Aug-18 8:06 
GeneralRe: Code can not be used with other "*.exe" files. Why? Pin
Erxin30-Aug-18 15:45
Erxin30-Aug-18 15:45 
AnswerRe: Code can not be used with other "*.exe" files. Why? Pin
Erxin16-Dec-15 15:24
Erxin16-Dec-15 15:24 
QuestionHow to detect mouse click over rehosted app screen Pin
Morgan11117-Aug-15 6:37
Morgan11117-Aug-15 6:37 
AnswerRe: How to detect mouse click over rehosted app screen Pin
Erxin13-Sep-15 17:11
Erxin13-Sep-15 17:11 

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.