• Re: GenerateConsoleCtrlEvent() does not work on child process

    From J Decker@21:1/5 to Alex Oren on Sun Jun 11 23:02:03 2023
    On Tuesday, March 23, 1999 at 12:00:00 AM UTC-8, Alex Oren wrote:
    Hello.
    I have a problem sending a GenerateConsoleCtrlEvent() to a child process with a new
    console. Doesn't matter if the CTRL_BREAK_EVENT or the CTRL_C_EVENT is sent, the process
    just keeps running.
    Here's the source of a test program:
    int main()
    {
    BOOL rc;
    STARTUPINFO si = { sizeof (STARTUPINFO) };
    PROCESS_INFORMATION pi;
    rc = CreateProcess(NULL, "loop.exe",
    NULL, NULL, FALSE,
    CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP,
    NULL, NULL,
    &si, &pi);
    Sleep(2000);
    rc = GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pi.dwProcessId); WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
    return 0;
    }
    loop.exe is a simple console program that outputs 10000 lines to the screen and
    terminates.
    Manually hitting Ctrl-Break on the console of loop.exe works OK.
    Omitting the CREATE_NEW_CONSOLE flag from the CreateProcess() call works too but the
    problem is that I *have* to use a separate console in the real app. Suggestions please.
    Thanxalot!
    Have fun,
    Alex.
    --
    My email address is intentionally mangled to foil spambots.
    Please remove the "---filter---" from the address for replying.
    Sorry for the inconvenience.

    I realize this is an ancient thread; but I did finally figure this out myself. AttachConsole( processId ) and then GenerateCtrlCEvent(). When you're done you can AttachConsole( ATTACH_PARENT_PROCESS /*-1*/); to return back to your own console. This is
    especially for CREATE_NEW_PROCESS_GROUP; if you don't create a new process group then your own process will get the break event, which isn't what most of us want....

    No CreateRemoteThreads required :)

    J

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)