Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming / Windows Forms

Code to Host a Third Party Application in our Process Window (e.g. Google Chrome, Internet Explorer 8)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (13 votes)
15 Aug 2010CPOL2 min read 81.5K   4.3K   51   22
This code shows how to host a third party application inside your own process Window, like Google Chrome & Internet Explorer 8

Introduction

This code shows how to host a third party application UI in your application's Window. In the sample application given below, NOTEPAD.EXE will be hosted in my process. The NOTEPAD application is also made to resize itself whenever our application is resized. Likewise any GUI application can be hosted in a separate process. For delayed GUI application, the hosting process has to wait till the MainWindow of the hosted process is created, hence as an alternative, I have used Thread.Sleep to wait for a second or two. The sample application can be downloaded from here.

Application_UI.jpg

Advantages

  1. The lifetime of the Hosted process can be managed within the hosting process.
  2. If the hosted process crashes, it can be handled appropriately.

  3. This kind of hosting and managing process can be very useful for watchdog kind of applications, wherein if the hosted process goes down or crashes, then it can be brought up again. Again all these are done within one Window.
  4. With some Window coordinate calculations, the hosted application can be tiled/cascaded, etc.

Examples

The famous Google Chrome, Internet Explorer 8, where-in each tab of the Internet Explorer/Chrome Window is a process. I have hosted GUIs in splitter window, whereas Internet Explorer 8/Chrome have them hosted in Tabs.

Using the Code

Two Win32 APIs are used to host a 3rd part application's Window in our process:

SetParent and SetWindowPos

C#
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, 
	int X, int Y, int cx, int cy, uint uFlags);

SetParent API is used to set the parent of the 3rd party application's main Window handle. In this case, my application's window handle is the parent of the 3rd party application's (Notepad.exe) main window.

C#
SetParent(p.MainWindowHandle, this.splitContainer2.Panel1.Handle);        

SetWindowPos API is used to resize the 3rd party application's Window whenever our application is resized.

C#
SetWindowPos(p.MainWindowHandle, HWND_TOP, 
	this.splitContainer2.Panel1.ClientRectangle.Left, 
this.splitContainer2.Panel1.ClientRectangle.Top,
	this.splitContainer2.Panel1.ClientRectangle.Width,
this.splitContainer2.Panel1.ClientRectangle.Height, SWP_NOACTIVATE | SWP_SHOWWINDOW);

Points of Interest

Any GUI application can be hosted in a separate process. The advantage of this is, if the GUI application crashes, the application that is hosting it still remains, it can take appropriate action. I was not able to host some of the applications like Internet Explorer (Iexplore.exe), Microsoft Word (winword.exe) into my own application's Window. To host these processes, I had to wait for a second (call Thread.Sleep(1000)) after WaitForInputIdle function is called. I really don't know the reason behind it. Please let me know if you know the reason.

History

  • 16th August, 2010: Initial post

License

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


Written By
Team Leader
India India
He is a graduate, currently working as a Software Engineer in Bangalore, India. You can reach him at sharan34@yahoo.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 44851126-Jul-22 22:21
Member 44851126-Jul-22 22:21 
QuestionAnyway to fix - hosted application will terminate once MdiParent property is set Pin
zeddosan10-Dec-18 2:04
zeddosan10-Dec-18 2:04 
QuestionIs there a way to know from parent when child is closed. Pin
p.dhiren13-Feb-15 8:50
p.dhiren13-Feb-15 8:50 
AnswerRe: Is there a way to know from parent when child is closed. Pin
zeddosan10-Dec-18 2:04
zeddosan10-Dec-18 2:04 
QuestionNot working with my own two exe Pin
Member 1119831910-Nov-14 2:45
Member 1119831910-Nov-14 2:45 
GeneralMy vote of 1 Pin
tinku5nov9-May-13 1:55
tinku5nov9-May-13 1:55 
QuestionAwesome Pin
lkdgnsl11-Jan-13 11:21
lkdgnsl11-Jan-13 11:21 
QuestionIs this possible in Java please? Pin
drequinox6-Jun-12 0:21
drequinox6-Jun-12 0:21 
GeneralMy vote of 5 Pin
Lidran3-Mar-11 8:31
Lidran3-Mar-11 8:31 
GeneralNOT WORKING ON OTHER EXE FILE as mdichild form Pin
R&D Ninja17-Oct-10 23:10
R&D Ninja17-Oct-10 23:10 
GeneralMy vote of 2 Pin
DanWalker15-Oct-10 8:00
DanWalker15-Oct-10 8:00 
GeneralMy vote of 5 Pin
S.H.Bouwhuis24-Aug-10 7:37
S.H.Bouwhuis24-Aug-10 7:37 
I haven't checked it thoroughly yet, but if it does what you claim it does, this would be extremely useful!

Thanks in advance.
GeneralTake a look at this... Pin
richardw4823-Aug-10 22:47
richardw4823-Aug-10 22:47 
GeneralMy vote of 3 Pin
karabax16-Aug-10 5:50
karabax16-Aug-10 5:50 
GeneralNice Pin
ManjunathGovindaraju16-Aug-10 0:23
ManjunathGovindaraju16-Aug-10 0:23 
GeneralAny application.. Pin
Ajay Vijayvargiya15-Aug-10 16:52
Ajay Vijayvargiya15-Aug-10 16:52 
GeneralRe: Any application.. Pin
Sharan Basappa15-Aug-10 18:55
Sharan Basappa15-Aug-10 18:55 
GeneralRe: Any application.. Pin
cara_ionutz12-Aug-12 7:38
cara_ionutz12-Aug-12 7:38 
GeneralRe: Any application.. Pin
vu dinh minh13-Aug-15 20:25
vu dinh minh13-Aug-15 20:25 
GeneralRe: Any application.. Pin
vu dinh minh13-Aug-15 20:36
vu dinh minh13-Aug-15 20:36 
GeneralA basic tip Pin
Trollslayer12-Aug-10 19:48
mentorTrollslayer12-Aug-10 19:48 
GeneralRe: A basic tip Pin
Sharan Basappa12-Aug-10 23:03
Sharan Basappa12-Aug-10 23:03 

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.