GetExitCodeProcess returns random values?

Hi Gurus,

I just don't seem to be able to figure this out. I wrote some program which spawns a child process with redirected input/output which is used to execute other programs specified by an .xml file via an exec element. All are good and working except one thing.

The return code of the program. For testing purposes I have a dummy program e.g.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
 
int main(int argc, char *argv[])
{
   std::cout<<"<failure>test failure</failure>"<<std::endl;
   std::cerr<<"Something went wrong..."<<std::endl;
   for (int x=0; x<argc; x++)
	   std::cout<<argv[x]<<std::endl;
   return 0;
}


This always returns 0.

While the output/input redirection works just fine, when I call

GetExitCodeProcess

I get random codes..

1
2
3
4
5
6
7
   WaitForSingleObject(piProcInfo.hProcess, timeOut_in);
   CloseHandle(piProcInfo.hProcess);
   CloseHandle(piProcInfo.hThread);
   DWORD exitCode;
   if(!GetExitCodeProcess(piProcInfo.hProcess, &exitCode)){
	   printf("Exit code = %X\n", exitCode);
   }


output :

Exit code = 150178
Exit code = 159758 etc..

Does this have to do something with the redirection, or I do something else completely wrong?
closed account (1vRz3TCk)
if(!GetExitCodeProcess(piProcInfo.hProcess, &exitCode))...
Isn't that; if failed to get GetExitCodeProcess here is the exit code I failed to get?

Try something like:
1
2
3
4
5
6
7
8
9
10
    if(GetExitCodeProcess(piProcInfo.hProcess, &exitCode))
    {
        //Check the code here
        if(exitCode == STATUS_PENDING)
            out_str("still alive");
        else
            printf("code = %X\n", exitCode);
    }
    else
        out_str("Failed to get exit code");
Last edited on
Thanks for the reply @CodeMonkey. Stupid error is stupid.

I get this now : displayMsg = 0x00157170 funtion failed with error 6: The handle is invalid.

Shouldn't I close the handles prior to using GetExitCodeProcess? Any other idea on where to look at?

Edit : to answer my own question. Do not close handles prior to checking the return code.
Last edited on
:-)

I do usually close the thread handle immediately after a CreateProcess, so I only need to worry about the process handle later.
Last edited on
Topic archived. No new replies allowed.