Click here to Skip to main content
11,791,088 members (61,202 online)
Click here to Skip to main content

Hosting .exe applications into a dialog

, 9 May 2007 CPOL 72.4K 1.8K 71
Rate this:
Please Sign up or sign in to vote.
An article on launching and embeding .exe applications into a dialog-based application

Screenshot - hostexe.jpg


This is a simple application that will explain how to host or embed an .exe application into a dialog. The same application is available in C# here. But if we want to do this in Visual C++ 6.0, it is something different. I have searched for this on different sites and did not get it in Visual C++ 6.0, so I decided to do it myself.


The basic procedure for this is:

  1. Declare the two required variables
  2. Start the application using 'CreateProcess'
  3. Write a call-back function to get the HWND of the process window
  4. Set the parent of the .exe application
  5. Set the position of the window

Technical details

This program is an MFC Dialog-based application. The steps are outlined below:

Step 1: Declare two variables

You must first declare the following variables:

HWND apphwnd;
HANDLE handle;

Step 2: Start the application using 'CreateProcess'

Here is the function that will do this task and return the handle of the process:

HANDLE StartNewProcess(LPCTSTR program, LPCTSTR args)
         HANDLE hProcess = NULL;
         PROCESS_INFORMATION processInfo;
         STARTUPINFO startupInfo;
         ::ZeroMemory(&startupInfo, sizeof(startupInfo));
         startupInfo.cb = sizeof(startupInfo);
             if(::CreateProcess(program, (LPTSTR)args,
                                NULL,  // process security
                                NULL,  // thread security
                                FALSE, // no inheritance
                                0,     // no startup flags
                                NULL,  // no special environment
                                NULL,  // default startup directory
                { /* success */
            Sleep(5000);//wait for the window of exe application created
            ::EnumWindows(&EnumWindowsProc, processInfo.dwThreadId);
                 hProcess = processInfo.hProcess;
                } /* success */
     return hProcess;//Return HANDLE of process.

Here we are starting the process using CreateProces. The WaitForInputIdle function enables a thread to suspend its execution until the specified process has finished its initialization. The EnumWindows function enumerates all top-level windows on the screen by passing the handle to each window and, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.

Our callback function will get the HWND of new the process window. The returned handle hprocess can then be used to terminate the process:


Step 3: Write a call-back function to get the HWND of the process window

This step is needed to find the HWND of our .exe application's window. We can use this window handle in different functions such as SetParent, MoveWindow, etc.

int CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param)
    DWORD pID;
    DWORD TpID = GetWindowThreadProcessId(hwnd, &pID);
        if (TpID == (DWORD)param)
            return false;
    return true;

The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window. apphwnd is the window handle of the newly created window.

Step 4: Set the parent of the .exe application

Here we are setting the parent window of the .exe application's window. The parent should be our application.


If we need to, we can remove the title bar of the window by using the following:

::SetWindowLong(apphwnd, GWL_STYLE, WS_VISIBLE);

Step 5: Set the position of the window

This can be done with the code below:

CRect rect;
::MoveWindow(apphwnd, rect.left,,rect.right, rect.bottom, true);

GetClientRect will get the window size of our dialog. MoveWindow will position the .exe appplication's window inside our dialog.


Note that after calling StartNewProcess we should only do Steps 4 and 5, and these should be inside a function handler of our dialog-based program.


  • May 9, 2007 - Original version posted


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


About the Author

Sherin Iranimose
Software Developer
India India
By the way, I'm not workaholic. I'm just love to program. Smile | :)

You may also be interested in...

Comments and Discussions

QuestionAny idea about hiding MS Paint before it is attached to dialog? Pin
rohitshinde18-Nov-14 23:39
memberrohitshinde18-Nov-14 23:39 
GeneralMy vote of 5 Pin
FordXu7-Apr-13 16:58
memberFordXu7-Apr-13 16:58 
GeneralMy vote of 5 Pin
Kingsley Chu7-Feb-13 7:07
memberKingsley Chu7-Feb-13 7:07 
QuestionHow to capture the App Wnd WM_CLOSE Message Pin
Member 311992617-Aug-12 5:01
memberMember 311992617-Aug-12 5:01 
AnswerRe: How to capture the App Wnd WM_CLOSE Message Pin
Rex_he17-Nov-13 16:54
memberRex_he17-Nov-13 16:54 
Generalclose application Pin
kip_chuko26-May-10 21:47
memberkip_chuko26-May-10 21:47 
GeneralSeems only work with SDI exe, does work with MDI Pin
frank123129-Oct-08 7:30
memberfrank123129-Oct-08 7:30 
GeneralRe: Seems only work with SDI exe, does work with MDI Pin
frank123129-Oct-08 7:34
memberfrank123129-Oct-08 7:34 
GeneralOpen any Program (*.exe) as dialog based window in our running program Pin
tajasmtk5-Aug-08 1:35
membertajasmtk5-Aug-08 1:35 
GeneralVC6 is not supported Pin
PatLeCat11-Feb-08 21:09
memberPatLeCat11-Feb-08 21:09 
QuestionNot Able to Embed Other exe applications Pin
girish1239-Sep-07 5:36
membergirish1239-Sep-07 5:36 
GeneralA slight bug Pin
WalderMort29-Aug-07 13:54
memberWalderMort29-Aug-07 13:54 
GeneralRe: A slight bug Pin
Sherin Iranimose29-Aug-07 16:06
memberSherin Iranimose29-Aug-07 16:06 
GeneralRe: A slight bug Pin
kartsam26-Jul-12 23:54
memberkartsam26-Jul-12 23:54 
Questionunable to control focus and window position of IE browser created by createProcess Pin
sdittmer8-Aug-07 13:05
membersdittmer8-Aug-07 13:05 
GeneralRefresh fix Pin
ntnass20-Jun-07 2:36
memberntnass20-Jun-07 2:36 
GeneralCannot find window Pin
Perry Zhu16-May-07 16:00
memberPerry Zhu16-May-07 16:00 
GeneralRe: Cannot find window Pin
Perry Zhu16-May-07 16:08
memberPerry Zhu16-May-07 16:08 
GeneralVery Interesting Pin
.dan.g.9-May-07 14:28
member.dan.g.9-May-07 14:28 
GeneralRe: Very Interesting Pin
vsherin9-May-07 18:26
membervsherin9-May-07 18:26 
GeneralRe: Very Interesting Pin
Justin Hallet11-May-07 6:06
memberJustin Hallet11-May-07 6:06 
GeneralRe: Very Interesting Pin
Magnus15-May-07 4:20
memberMagnus15-May-07 4:20 
GeneralRe: Very Interesting Pin
Magnus15-May-07 21:22
memberMagnus15-May-07 21:22 
GeneralRe: Very Interesting Pin
fengerfafa200617-May-07 4:56
memberfengerfafa200617-May-07 4:56 
GeneralRe: Very Interesting Pin
Sherin Iranimose14-Nov-07 0:57
memberSherin Iranimose14-Nov-07 0:57 

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
Web04 | 2.8.1509028.1 | Last Updated 9 May 2007
Article Copyright 2007 by Sherin Iranimose
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid