Click here to Skip to main content
11,922,641 members (27,783 online)
Click here to Skip to main content
Add your own
alternative version


74 bookmarked

ByPass difficult Automation and add applications "as is" in your .NET application

, 29 Dec 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Fed up with automation? Need to add components as is to your application? Try this out.


I was given so many tasks of automation. That I was really fed up with them. Once my manager asked me to automate IE as in our application, so that user feels that it's the application's IE browser. One other day, I was asked to add Word control and make a Window in the application with Word look and feel and same Toolbars. So then I thought there must be a way in Dotnet to add the applications "as is" and some kind of communication with them as well.

Note: Of course, the kind of flexibility you achieve with automation can not be achieved via this way. But "according to the requirements", it can reduce time to market and prevent adding heavy automation code and DLLs.

Using the code

I have shown how different applications can be added easily in a control or Form. If you add it in a Form, user will feel that it will become kind of MDI application holding other application. First demo application shows how to add Command Prompt, Internet Explorer, Windows Media Player and WinWord. Second application shows how to interact with them.

Add Applications Demo

Main functions which are used in this case are from user32. That are SetParent, ShowWindow and SetForegroundWindow. Rest of the help is provided by managed Process, ProcessInfo and SendKeys classes.

Declare Function ShowWindow Lib "user32" (ByVal hWnd As System.IntPtr, 
        ByVal nCmdShow As Integer) As Boolean
Declare Function SetParent Lib "user32" (ByVal hWndChild As System.IntPtr,
        ByVal hWndNewParent As System.IntPtr)
        As System.IntPtr
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As System.IntPtr)
                As Integer

Following code is written on "Open -> Command Prompt-> Open as Normal and Add as Min" menu. It opens the command window using Process.Start() as normal and then adds it to parent window (which is a .NET window) as a sub window using ShowWindow function and makes its state minimized.

Dim pinfo As New ProcessStartInfo("cmd")
Dim p As Process = System.Diagnostics.Process.Start(pinfo)
SetParent(p.MainWindowHandle, Me.Handle)
ShowWindow(p.MainWindowHandle, SW_MINIMIZE)

Second menu is Open ->Get Running Browser and make it full screen. You have to have at least one IE window open for this functionality. It captures it and adds it in the main form as maximized. Closing the application will also close this window.

Dim p As Process() = System.Diagnostics.Process.GetProcessesByName("iexplore")
    SetParent(p(0).MainWindowHandle, Me.Handle)
    ShowWindow(p(0).MainWindowHandle, SW_MAXIMIZE)
Catch ex As Exception
    MessageBox.Show("No IE window open - Please open one")
End Try

Third menu is opening a Media Player and adding it to a button and play a file present in XP Media directory. Open -> Media Player - in a Button - Max. Another interesting point is, Media folder is not listed in Enviornment.SpecialFolders. So you have to get it like the way I have used below:

Dim p As Process = System.Diagnostics.Process.Start("mplayer2", 
Environment.GetEnvironmentVariable("WINDIR") + "\\Media\\Windows XP Startup.wav")
SetParent(p.MainWindowHandle, Me.Button1.Handle)
ShowWindow(p.MainWindowHandle, SW_MAXIMIZE)

Fourth and last menu is adding WinWord and add it in a Label. Open -> WinWord - In a Label. WaitForInputIdle helps in getting the Winword to an idle state.

Dim pinfo As New ProcessStartInfo("WINWORD")
Dim p As Process = System.Diagnostics.Process.Start(pinfo)
SetParent(p.MainWindowHandle, Me.Label1.Handle)

Interact with Application (Calc.exe and IE)

Run the demo. Open any website using IE window external from the demo. Click "Get Running IE Window". It will get the running IE window instance and add it to the control and get its HTMLDocument and display all the source in the TextBox. It will also display its title on the label next to the button. Click on the "Open Calc" button. It will open the calc in user Window. Do all the calculations and close the calculator. The result will come back to the TextBox next to "Open Calc" button.

IE demo important points

IE demo uses IEDom class to retrieve the DOM of IE window. IsIEServerWindow function looks for Internet Explorer_Server class. WM_HTML_GETOBJECT message is send to Windows handle using RegisterWindowMessage Win32 call. Then ObjectFromLresult function to get IHTMLDocument object.

If Not IsIEServerWindow(hWnd) Then

   ' Get 1st child IE server window
   win32.EnumChildWindows(hWnd, AddressOf EnumChild, hWnd)

End If

If Not hWnd.Equals(0) Then

  ' Register the message
  lMsg = win32.RegisterWindowMessage("WM_HTML_GETOBJECT")

  ' Get the object
  Call win32.SendMessageTimeout(hWnd, lMsg, 0, 0, _
  win32.SMTO_ABORTIFHUNG, 1000, lRes)

  If lRes Then

    ' Get the object from lRes
    hr = ObjectFromLresult(lRes, IID_IHTMLDocument, 0, IEDOMFromhWnd)

    If hr Then Throw New COMException(hr)

     End If 
  End If
End If

Also used RemoveMenuBar function to remove IE Rebar.

If ClassName.ToString() = "ReBarWindow32" Then
  ' Hide menuBar
  win32.ShowWindow(hWnd, 0)
  Return 0
  Return 1
End If
Calc demo important points

ExteranlCalc form opens calc.exe on load and adds it to itself. removeTitleBarAndRescale function is used to remove the titlebar, stop resizing.SetWindowPos function is used to set the position of the Window and place it in a way to hide menubar of calc.

win32.SetParent(p.MainWindowHandle, Me.Handle)
win32.SetWindowPos(p.MainWindowHandle, Me.Handle, -LEFT_CONST,
             -TOP_CONST, Me.Width + LEFT_CONST + RIGHT_CONST, 
             Me.Height + TOP_CONST + BOTTOM_CONST, 
Note: I have tested with standard calculator. If you want to show scientific calc, adjust the size of ExternalCalc form. You may also use SendKeys to show the calc in any form. Error checking in all scenarios has not been done on the demos.

Points of Interest

It's very easy way to add applications in your .NET based application. Thanks to Microsoft for making such things Smile | :)

Revision History


  • Added a Picture with calc and IE Control and their interaction.
  • Added the source code for calc and IE demo.


  • Original article


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


About the Author

Armoghan Asif
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionSet Parent is not working Pin
Member 1027924611-Oct-13 23:21
memberMember 1027924611-Oct-13 23:21 
Questionwindow becomes transparent in aero theme Pin
Member 96015293-Dec-12 0:05
memberMember 96015293-Dec-12 0:05 
Questionhow to open OUTLOOK in Panel or as MDI Chile, i mean inside window form Pin
hardikdarji13-Aug-10 5:50
memberhardikdarji13-Aug-10 5:50 
GeneralMy vote of 1 Pin
kamlesh88513-Feb-09 1:53
memberkamlesh88513-Feb-09 1:53 
GeneralProblem with word Pin
parthmankad4-Jul-08 1:35
memberparthmankad4-Jul-08 1:35 
Generalstill not working Pin
djsoul20-Mar-08 4:41
memberdjsoul20-Mar-08 4:41 
QuestionHow to call a process By speciying its directory and make displayed inside a label Pin
djsoul1-Mar-08 14:32
memberdjsoul1-Mar-08 14:32 
GeneralRe: How to call a process By speciying its directory and make displayed inside a label Pin
Armoghan Asif3-Mar-08 23:29
memberArmoghan Asif3-Mar-08 23:29 
GeneralRe: How to call a process By speciying its directory and make displayed inside a label Pin
djsoul4-Mar-08 11:34
memberdjsoul4-Mar-08 11:34 
GeneralRe: How to call a process By speciying its directory and make displayed inside a label Pin
Armoghan Asif6-Mar-08 23:45
memberArmoghan Asif6-Mar-08 23:45 
GeneralRe: How to call a process By speciying its directory and make displayed inside a label Pin
djsoul7-Mar-08 10:41
memberdjsoul7-Mar-08 10:41 
GeneralRe: How to call a process By speciying its directory and make displayed inside a label Pin
xiaolinmantis11-Dec-08 5:00
memberxiaolinmantis11-Dec-08 5:00 
QuestionHow to run my application with for eg, notepad.exe as the child Pin
--=A J E E S H=--18-Jul-07 0:57
member--=A J E E S H=--18-Jul-07 0:57 
AnswerRe: How to run my application with for eg, notepad.exe as the child Pin
Armoghan Asif18-Jul-07 21:14
memberArmoghan Asif18-Jul-07 21:14 
AnswerRe: How to run my application with for eg, notepad.exe as the child Pin
Mathew Uthup24-Oct-07 18:27
memberMathew Uthup24-Oct-07 18:27 
QuestionHow to expand to other applications Pin
TonyL@cox13-Jul-07 3:17
memberTonyL@cox13-Jul-07 3:17 
AnswerRe: How to expand to other applications Pin
Armoghan Asif13-Jul-07 4:23
memberArmoghan Asif13-Jul-07 4:23 
I dont fully understand what you want to do.

The idea is to add applications as is in your own .NET application.

If you have an exe and you want to add it in your .NET application, you just have to invoke it like i have done in Calc example, using Process class, once it is invoked you can add it into your application.. using the code present

GeneralRe: How to expand to other applications Pin
TonyL@cox13-Jul-07 14:10
memberTonyL@cox13-Jul-07 14:10 
GeneralRe: How to expand to other applications Pin
xiaolinmantis11-Dec-08 5:03
memberxiaolinmantis11-Dec-08 5:03 
QuestionC# Example? Pin
ccaspanello8-Jul-07 20:22
memberccaspanello8-Jul-07 20:22 
AnswerRe: C# Example? Pin
Armoghan Asif13-Jul-07 4:49
memberArmoghan Asif13-Jul-07 4:49 
GeneralI need a help Pin
tamilarasi31-May-07 0:08
membertamilarasi31-May-07 0:08 
AnswerRe: I need a help Pin
Armoghan Asif1-Jun-07 2:26
memberArmoghan Asif1-Jun-07 2:26 
GeneralThank You!!! Pin
Stonebraker18-May-07 12:39
memberStonebraker18-May-07 12:39 
GeneralOpen a web Browser with the correct user and password! Pin
Assaf Koren6-Mar-07 14:45
memberAssaf Koren6-Mar-07 14:45 
GeneralRe: Open a web Browser with the correct user and password! Pin
Armoghan Asif11-Apr-07 0:47
memberArmoghan Asif11-Apr-07 0:47 
GeneralAccess Denied Pin
malcomm2-Mar-07 15:17
membermalcomm2-Mar-07 15:17 
GeneralRe: Access Denied Pin
Armoghan Asif10-Apr-07 22:20
memberArmoghan Asif10-Apr-07 22:20 
Generalnull pointer Pin
Member #36582222-Jan-07 22:13
memberMember #36582222-Jan-07 22:13 
GeneralRe: null pointer Pin
Armoghan Asif24-Jan-07 21:44
memberArmoghan Asif24-Jan-07 21:44 
GeneralFocus Issue Pin
Abhijeet Sheorey21-Jul-05 21:12
sussAbhijeet Sheorey21-Jul-05 21:12 
GeneralRe: Focus Issue Pin
Armoghan Asif22-Jul-05 2:14
memberArmoghan Asif22-Jul-05 2:14 
GeneralWord Demo not working Pin
iampud30-Nov-04 7:58
sussiampud30-Nov-04 7:58 
GeneralRe: Word Demo not working Pin
Bluetears7630-Nov-04 19:27
memberBluetears7630-Nov-04 19:27 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.1 | Last Updated 29 Dec 2004
Article Copyright 2004 by Armoghan Asif
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid