c++winapicreateprocess

Process id returned by CreateProcess is not there in task manager


I create a process with Windows API CreateProcess(). Process Id returned by this function is not there in task manager. When I try to terminate this process using TerminateProcess(). GetLastError() returns 5. Using chrome as sample browser.

My goal is to create a browser process and then shut down the browser after authentication.

My sample code is like this:

int main()
{
    wstring app{L"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"};
    PROCESS_INFORMATION browserProcessInfo;
    STARTUPINFO browserStartupInfo;
    ZeroMemory(&browserStartupInfo, sizeof(browserStartupInfo));
    ZeroMemory(&browserProcessInfo, sizeof(browserProcessInfo));
    wstring cmdline;
    cmdline = app + wstring{ { L" --new-windows" } };
    bool bProcessCreated = CreateProcess(NULL,(LPWSTR) (cmdline.c_str()), NULL, NULL, TRUE,     NORMAL_PRIORITY_CLASS , NULL, NULL, &browserStartupInfo, &browserProcessInfo);
    auto handle = OpenProcess(PROCESS_TERMINATE, FALSE, browserProcessInfo.dwProcessId);
    auto terminateStatus = TerminateProcess(handle, 1);
    auto errorNumber = GetLastError();
    CloseHandle(browserProcessInfo.hProcess);
    CloseHandle(browserProcessInfo.hThread);
}

Solution

  • It's not a problem with CreateProcess. chrome simply exits pretty much immediately after starting up if Chrome is already running. It simply tells the existing process to create a new window and exits.