Search code examples
c++windowsvisual-studiochild-processcreateprocess

Toggle child process window visibility in Visual C++ without relaunching the child


Using Visual Studio 2017.

I use

siStartInfo.dwFlags &= STARTF_USESHOWWINDOW;

siStartInfo.wShowWindow = SW_HIDE;

to create a child process (through CreateProcess()) with a hidden window. It works perfectly.

What I would like, if at all possible, is to be able to toggle the visibility status of the child process' window, post-creation, from the main program on demand (with a press of a button, for example). I want to control the child process' window visibility without relaunching it through CreateProcess().

Modifying the child process code is not an option - it can be many different programs with not necessarily available source code.

Thanks in advance.


Solution

  • If you know a unique window class or title of the child process, you can use FindWindow() to find that window.

    For a more generic approach, use EnumWindows() to enumerate all top-level windows. For each window call GetWindowThreadProcessId() to check if it belongs to the child process.

    Call ShowWindow() to toggle visibility.