42 passed to TerminateProcess, sometimes GetExitCodeProcess returns 0

After receiving the handle returned by CreateProcess, I call TerminateProcess, passing 42 for the process exit code. Then I use WaitForSingleObject to complete the process, and finally I call GetExitCodeProcess.

None of the functions cause a report error. The baby process is an endless cycle and does not end on its own.

The problem is that sometimes GetExitCodeProcess returns 42 for the exit code (as it should), and sometimes it returns 0. Any idea why?

#include <string>
#include <sstream>
#include <iostream>
#include <assert.h>
#include <windows.h>

void check_call( bool result, char const * call );
#define CHECK_CALL(call) check_call(call,#call);

int
main( int argc, char const * argv[] )
    {
    if( argc>1 )
        {
        assert( !strcmp(argv[1],"inf") );
        for(;;)
            {
            }
        }
    int err=0;
    for( int i=0; i!=200; ++i )
        {
        STARTUPINFO sinfo;
        ZeroMemory(&sinfo,sizeof(STARTUPINFO));
        sinfo.cb=sizeof(STARTUPINFO);
        PROCESS_INFORMATION pe;
        char cmd_line[32768];
        strcat(strcpy(cmd_line,argv[0])," inf");
        CHECK_CALL((CreateProcess(0,cmd_line,0,0,TRUE,0,0,0,&sinfo,&pe)!=0));
        CHECK_CALL((CloseHandle(pe.hThread)!=0));
        CHECK_CALL((TerminateProcess(pe.hProcess,42)!=0));
        CHECK_CALL((WaitForSingleObject(pe.hProcess,INFINITE)==WAIT_OBJECT_0));
        DWORD ec=0;
        CHECK_CALL((GetExitCodeProcess(pe.hProcess,&ec)!=0));
        CHECK_CALL((CloseHandle(pe.hProcess)!=0));
        err += (ec!=42);
        }
    std::cout << err;
    return 0;
    }

std::string
get_last_error_str( DWORD err )
    {
    std::ostringstream s;
    s << err;
    LPVOID lpMsgBuf=0;
    if( FormatMessageA(
            FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
            0,
            err,
            MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
            (LPSTR)&lpMsgBuf,
            0,
            0) )
        {
        assert(lpMsgBuf!=0);
        std::string msg;
        try
            {
            std::string((LPCSTR)lpMsgBuf).swap(msg);
            }
        catch(
        ... )
            {
            }
        LocalFree(lpMsgBuf);
        if( !msg.empty() && msg[msg.size()-1]=='\n' )
            msg.resize(msg.size()-1);
        if( !msg.empty() && msg[msg.size()-1]=='\r' )
            msg.resize(msg.size()-1);
        s << ", \"" << msg << '"';
        }
    return s.str();
    }

void
check_call( bool result, char const * call )
    {
    assert(call && *call);
    if( !result )
        {
        std::cerr << call << " failed.\nGetLastError:" << get_last_error_str(GetLastError()) << std::endl;
        exit(2);
        }
    }
+3
source share
3 answers

I thought the noble comment was nailed ...

- , TerminateProcess(), , Windows , TerminateProcess(), , ? , , , , Sleep(1), ( Sleep(0)), , .

, , .

+1

, this this.

, TerminateProcess - , . .

.

+1

From the GetExitCodeProcess docs:

If the function does not work, the return value is zero. To get extended error information, call GetLastError.

From: http://msdn.microsoft.com/en-us/library/ms683189(VS.85).aspx

I would recommend calling GetLastError()to find out what is broken. You might also want to put sleep(1)in your loop: P

0
source

Source: https://habr.com/ru/post/1728400/


All Articles