Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8411977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:33:41+00:00 2026-06-10T00:33:41+00:00

I have a MFC application that launches other (generic windowed, black box) applications as

  • 0

I have a MFC application that launches other (generic windowed, black box) applications as a pop-up and waits for their completion. No communication/interaction between parent and child is required and should be avoided. Only the “child app behaves as modal dialog of parent app” behavior is wanted.
What is the correct way to do this?

An example of “launch another application as child window” can be seen at: Activating an application as a child/popup of another application which leads to http://www.codeproject.com/Articles/18724/Hosting-exe-applications-into-a-dialog (this is not what I want, I want modal popup behavior)

For simplicity we can assume that both launching and launched applications have single “stack” of windows (one main window with modal dialogs that can have own modal dialogs).

My current pseudo-code (error handling and callback function omitted for simplicity)

//get the current MFC dialog of launcher program we are launching the other app from
parentWnd = AfxGetMainWnd()->GetActiveWindow(); 
parentHwnd = parentWnd->GetSafeHwnd(); //HWND

// launch child and retrieve basic info from PROCESSINFO structure
CreateProcess(childExecutable); // => childProcessHandle, childProcessId 

//get the "main" window of child application
EnumWindows(EnumProc_That_Retrieves_TopLevelWindow_With_childProcessId); // => childHwnd

//link the child window as popup
SetWindowLong(childHwnd, GW_OWNER, parentHwnd);

//disable input into parent window
parentWnd->EnableWindow(FALSE);

//remove taskbar entry for child
SetWindowLong(child, GWL_EXSTYLE, GetWindowLong(child, GWL_EXSTYLE)&~WS_APPWINDOW);

//now keep waiting for the child process termination and process parent messages (e.g. WM_PAINT)
while (MsgWaitForMultipleObjects(childProcessHandle and process QS_ALLINPUT) {
   while (PeekMessage(PM_NOREMOVE)) AfxGetApp()->PumpMessage();
}

//re-enable input into parent window
parentWnd->EnableWindow(TRUE);

Now what my lesser problems are with foreground visual style (e.g. foreground = blue titlebar vs. background = gray titlebar) and keyboard input focus behavior:

1) initial removal of childs WS_APPWINDOW style removes foreground visual and input focus from child app. No application has focus at that point.

2) when user clicks any parent application window, the child foreground visual style is toggled. Keyboard focus is retained in the child application.
example: child app has foreground + focus -> click parent 1st time -> child loses foreground, retains focus -> click parent 2nd time -> child gains foreground, keeps focus -> etc.

expected behavior (what “normal” MFC popups do):
child app has foreground + focus -> click parent -> child titlebar flashes briefly and retains foreground + focus
child app does not have foreground + focus -> click parent -> child titlebar gains foreground and keyboard focus

And now this is worst problem:
3) I have encountered a MFC application that when launched separately user can open a “stack of modal dialogs” A->B->C->D->E, the ownership of windows is exactly matching this (E is owned by D, D owned by C etc.). But if I open it from my MFC application (M), the ownership looks like M->A->B->C,D,E (C,D,E are all owned by B, B owned by A, A owned by my app window M). This leads to “stack without support” http://blogs.msdn.com/b/oldnewthing/archive/2005/02/24/379635.aspx problem.
This behavior disappears when I remove SetWindowLong(childHwnd, GW_OWNER, parentHwnd) so messing with ownership probably triggers the unwanted behavior of child application, but without that I can’t seem to guarantee the “one is positioned over the other” premise of modal dialog.

So the grand question again: what is the correct way to do this task and avoid the problems I have described.

Edit

Solution so far

We must avoid messing with owner-owned structures as @mfc suggests below, so the task is basically to reimplement this aspect of window manager for just our parent-child pair in another way. I have prototyped part of solution using Windows Hooks. However it seems quite complex and tedious to complete, so I decided to go with another primitive approach (deadlines, oh deadlines). For the sake of example, I will describe basic ideas of both.

Hook solution

Disclaimer: only parent focus hook has been confirmed to work, rest is theorycrafting. Maybe there is be cleaner/more lightweight implementation, one could get inspiration in actual Windows window manager implementation (remember the whole point is avoiding setting GW_OWNER which works fine for window manager, but can break the child black-box app behavior).

  • add some “ignore input while child is running” into parent message loop for when child app is running (intermittently) without windows
  • create shared memory and structures to hold [parentPid, parentHwnd,childPid] for each invocation
  • create DLL instanced memory for [list of parent nonowned windows, their UI thread, child hook]
  • hook systemwide to WH_CBT -> HCBT_CREATEWND, if childPid matches, register window in list, register another hook HCBT_ACTIVATE just for that child thread if not already present
  • hook systemwide to WH_CBT -> HCBT_DESTROYWND, if childPid matches, unregister window in list, unregister HCBT_ACTIVATE hook if this was last window for given thread, if this was last window for child app, unhook parent HCBT_ACTIVATE hook and focus parent
  • parent thread HCBT_ACTIVATE hook prevents gaining focus and focuses child app instead using EnumWindows.
  • child thread HCBT_ACTIVATE hooks prevent focus loss if the target is parent, keep parent just below child in Z-order
  • create child process suspended and resume only when hooks are in place
  • remember to unhook everywhere

Primitive approach

basically applying focus switch in first point of previous solution, when parent is clicked it flickers over child as focus is transferred back and forth.

  • “ignore input while child is running” (discard various keypress, click, etc. messages) in parent message loop for when child app is running, focus child app instead using EnumWindows.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:33:42+00:00Added an answer on June 10, 2026 at 12:33 am

    Changing parent/child relationship of other windows that does not belong to you is tricky and error prone.
    If the launcher program has no communication with the launched program and the main purposes is to avoid any UI with the launcher, you can simply hide the launcher after the secondary program is launched successfully using ShowWindow(SW_HIDE).
    In hidden mode, it continues to monitor the launched program and un-hide itself when the secondary program terminates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MFC application that launches a IWebBrowser2 window. On users computers where
I have an MFC application that runs on my computer, but when I try
I have an application that outputs large amounts of text data to an MFC
I'm working on a bug where I have an MFC application that does weird
I have an MFC application that spawns a number of different worker threads and
I have an MFC application that creates a CDialog. I'd like this CDialog to
I have an MFC application that I was given (without source code) which opens
I have a Windows MFC application that: (1) Loads the JVM ( JNI_CreateJavaVM() )
I have an MFC application (using a legacy library that I can't change). When
I have an MFC application that uses an ancient (circa 1999) third-party ActiveX control.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.