Windows – How to make a child process window to appear modal in the process

winapiwindows

I have an application that calls some other utility application to set some settings for a particular device. That utility application is called using ShellExecuteEx.

So as not to confuse the user, it would be better to made the window of the utility application modal to my main window. How does one do this?

Things I've tried:

  1. WaitForSingleObjectEx on the process after ShellExecuteEx, INFINITE TIMEOUT – window is modal, but main application does not repaint (because it's waiting for the single object!)
  2. WaitForSingleObjectEx on the process after ShellExecuteEx, some small timeout, then call Peekmessage and DispatchMessage – repaint now works, but utility application is no longer "modal". The main application responds to mouse clicks, button clicks, etc
  3. EnableWindow(FALSE), then do method #2, then EnableWindow(TRUE) – WORKS!!!, but after this, the z-order of my application changed. (it's now below some other window). why?!

Best Solution

You have two things to simulate: ownership and modality.

To simulate ownership: You need to set the owner of your new child process window to your window. This should alleviate any z ordering issues. Though I don't know if this works from another process. If not then you might have to attach your thread input queues and then call it. Or use some other code injection technique.

SetWindowLong <target window handle>, GWL_HWNDPARENT, <new owner handle>

To simulate modality, I think you are on the right track with EnableWindow and the WaitForSingleObjectEx.

Related Question