i'm trying to create a program that will run another program. but the program i want to run has a gui and i don't want the gui to be visible, i just want the program to run in the background.

But i have to do it without editing the gui program

here's my code:

    TCHAR* path = L"C:\\Myfile\\test.exe";
    STARTUPINFO info = {0};
    PROCESS_INFORMATION processInfo;

    ZeroMemory( &info, sizeof(info) );
    info.cb = sizeof(info);
    ZeroMemory( &processInfo, sizeof(processInfo) );

    info.dwFlags = STARTF_USESHOWWINDOW;
    info.wShowWindow = FALSE; 

    if (CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
    {
        ::WaitForSingleObject(processInfo.hProcess, INFINITE);
        CloseHandle(processInfo.hProcess);
        CloseHandle(processInfo.hThread);
    }

the code does run the program but the GUI is still displayed. How can i hide the GUI?

[b]i don't have access to the GUI programs code[/b]

You can modify the show state for another process' window using ShowWindow:

#include <iostream>
#include <tchar.h>
#include <Windows.h>

using namespace std;

int main()
{
    TCHAR *title = _T("Untitled - Notepad");

    if (HWND h = FindWindow(0, title)) {
        ShowWindow(h, SW_HIDE);
    }
    else {
        cerr << "Window not found\n";
    }
}

However, if the application isn't designed to run in the background, you'll have issues in making it do things after being hidden, and will also need to close it by killing the process.

You can combine info.wShowWindow = SW_HIDE with info.dwFlags = STARTF_USESHOWWINDOW. But the GUI app you are trying to run could always ignore the value of wShowWindow of STARTUPINFO structure (which is passed as int nCmdShow in the WinMain entry point) and show its own window the way it wants (the way the programmer of that particular app wanted). So, to achieve the purpose, I suggest you do the following steps:

  1. Run the app still specifying those values of the two STARTUPINFO struct members
  2. Use WaitForInputIdle on that process so you can get the ideal timing in calling the FindWindow API
  3. Call FindWindow
  4. If calling FindWindow yields the correct window handle, check if it is still visible via IsWindowVisible and...I think you already know what's the next appropriate action here.

Hope this helps. Good luck!

Run the app still specifying those values of the two STARTUPINFO struct members

ok so info.wShowWindow = SW_HIDE and info.dwFlags = STARTF_USESHOWWINDOW don't need to change?

Use WaitForInputIdle on that process so you can get the ideal timing in calling the FindWindow API

like this?

WaitForInputIdle(processInfo.hProcess, 10);

Call FindWindow

what parameters will be used in this function?

Edited 3 Years Ago by beginner91

This article has been dead for over six months. Start a new discussion instead.