GetExitCodeProcess failing for process run in another session?

时间:2016-07-11 20:23:33

标签: windows api session process

I'm using the API function GetExitCodeProcess in the current logged on user session to monitor the exit code of a process. This process is run in another session (SYSTEM). The function returns immediately with an exit code of 0. I don't think this is correct.

Can anybody tell if this function will always fail / give wrong results if the process has been started in a different session?

My VB6 code is this, if anybody is interested:

Public Function GetProcExitCode(ByVal uProcID As Long) As Long

    Const STILL_ACTIVE = &H103&
    Const PROCESS_QUERY_INFORMATION = &H400&

    Dim lProcHnd As Long
    lProcHnd = OpenProcess(PROCESS_QUERY_INFORMATION, True, uProcID)

    Dim lTick As Long
    lTick = GetTickCount

    Dim lRet&
    lRet = 0

    'Wait for process end
    Do

        GetExitCodeProcess lProcHnd, lRet

    Loop While lRet = STILL_ACTIVE

    CloseHandle lProcHnd

    GetProcExitCode = lRet

    Dim lTicks&
    lTicks = GetTickCount() - lTick

    WriteLog "!!! GetExitCodeProcess needed " & lTicks & " ms."

End Function

Thank you.

1 个答案:

答案 0 :(得分:1)

From Microsoft MSDN:

Return value

If the function succeeds, the return value is nonzero.

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


Giving a return value of zero seems to be an error.