Windows > Software Development for Windows Client Forums > Application Compatibility for Windows Development > problems with cleaning up application in Windows 7 launched by service (GetExitCodeProcess returning FALSE with error INVALID_HANDLE_VALUE
Ask a questionAsk a question
 

General Discussionproblems with cleaning up application in Windows 7 launched by service (GetExitCodeProcess returning FALSE with error INVALID_HANDLE_VALUE

  • Thursday, October 15, 2009 3:01 AMNeelTiwari Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Our product installs a service which in turn launches an EXE in the current open user session (or in session 0 if no one is logged in). It uses the WaitForSingleObject API to wait for the exe to exit (the exe here can exit in response to icon events, "action schedules", etc. with specific exit codes), and then uses the GetExitCodeProcess API to figure out the exit code of the exe. Below is a sample of the code in the service which does this:

    <start of code>
    newProc = CreateProcessAsUser(
            hToken,             // client's access token, we get this by finding the "owner" of winlogon.exe and grabbing the access token... basically, to figure out                                 // which session to launch the application in
            program,        // file to execute
            cmdline,            // command line
            NULL,               // pointer to process SECURITY_ATTRIBUTES
            NULL,               // pointer to thread SECURITY_ATTRIBUTES
            TRUE,              // handles are not inheritable
            dwCreationFlags,    // creation flags
            pEnv,               // pointer to new environment block
            currentDirectory,               // name of current directory
            &info,                // pointer to STARTUPINFO structure
            &processInfo                 // receives information about new process
            );

     WaitForInputIdle(processInfo.hProcess, 15000);
        if (waitForStatus == TRUE) {
        int wfp = WaitForSingleObject(processInfo.hProcess, INFINITE);
        switch(wfp) {
            case WAIT_ABANDONED:
                PrintEvent(_T("forkProcess wait abandoned"),VERBOSE);
                break;
            case WAIT_FAILED:
                ErrorPrinter(_T("forkProcess wait failed"));
                break;
            case WAIT_TIMEOUT:
                PrintEvent(_T("forkProcess wait timed out"),VERBOSE);
                break;
        } //PrintEvent and ErrorPrinter are "wrappers" for writing debug output
    __try
        {
            if (GetExitCodeProcess(processInfo.hProcess, &status) == TRUE) {
                if (processInfo.hProcess != INVALID_HANDLE_VALUE) {
                    CloseHandle(processInfo.hProcess);
                    processInfo.hProcess = INVALID_HANDLE_VALUE;
                }
                if (processInfo.hThread != INVALID_HANDLE_VALUE) {
                    CloseHandle(processInfo.hThread);
                    processInfo.hThread = INVALID_HANDLE_VALUE;
                }

                if (status != STILL_ACTIVE) {
               
                   /* we got the exit code, "else" app is still running */

               }
            }
            else {
                     /* THIS IS WHERE EXECUTION OF CODE IS ENDING UP*/
                    int exitCode = GetLastError();
             }
     }__except ( EvalException(ecode = GetExceptionCode()) ) {
    /* ecode contains exception code if something bad happened */
    }
    </end of code>

    As seen here, it looks like the GetExitCodeProcess() API is returning FALSE.
    The value of exitCode is 6, which (according to MSDN) translates to INVALID_HANDLE_VALUE.
    I can only assume that the handle referred to here is the "hProcess" (or handle to the created process). I'm 100% sure that I am not deliberately closing (or invalidating in any other way) the handle returned by CreateProcessAsUser, before the call to GetExitCodeProcess. Also note that WaitForSingleObject returns success, and return code is NOT any of the cases for which I've added debugs (which basically means the process handle got "signaled" successfully when the "launched" exe exited).

    I've recently read somewhere on a Microsoft site that GetExitCodeProcess can be used to check to see if the handle to any process we wish to monitor still exists or not (GetExitCodeProcess() returning FALSE with the passed handle means that the handle is invalid). Is it possible that the exe is exiting so fast that the handle is getting invalidated before the GetExitCodeProcess (I know this is HIGHLY unlikely, if not impossible, but I thought I'd ask)?

    Also, to give more details:
    1. This is only happening on Windows 7. We don't see this on Windows Vista, or on other NT variants like XP, Win2003, etc.
    2. This is an intermittent issue. Most times we are successfully able to grab the exit code, but not always.

All Replies

  • Thursday, October 15, 2009 10:54 AMJialiang Ge [MSFT]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello

    As long as you have a handle to that process open, the handle should be valid forever. When the problem happens, could you please run !handle command in windbg or launch process explorer to view handles of your current process? Do you see Process handles of the child process?

    Regards,
    Jialiang Ge
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Thursday, October 29, 2009 11:17 AMJialiang Ge [MSFT]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello

    How are you? Could you please check the above-mentioned information for me?
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Tuesday, November 03, 2009 7:16 AMJialiang Ge [MSFT]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    We are changing the issue type to “General Discussion” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.