Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenProcess error 87 invalid parameter

I'm trying to write a program which executes make.exe from MinGW distribution in the current directory and makes use of its STDOUT data and exit code. I have a handle to process STDOUT where I fetch data from, created with CreatePipe. When I get an ERROR_HANDLE_EOF on that pipe I assume the process has exited and try to get its exit code:

if(session->pid == 0) return;
HANDLE hp = OpenProcess(PROCESS_QUERY_INFORMATION |
            PROCESS_TERMINATE, TRUE, session->pid);
if(hp == NULL) {
    printf("OpenProcess(%i) failed, error: %i\n",
        session->pid, (int)GetLastError());
    return;
}

My code works on all other MinGW utilities I tested (like pwd, ls, etc.), I get the STDOUT and the exit code with no problem. But when I try it on make, the above code displays the following message:

"OpenProcess(2032) failed, error: 87"

I googled for error code 87, and it says "Invalid parameter". I don't see what could be invalid about a positive process id like 2032. Any ideas?

like image 621
user389419 Avatar asked Nov 14 '25 13:11

user389419


1 Answers

You should use the handle from CreateProcess instead of using OpenProcess on the PID.

OpenProcess only works if the process object still exists. By the time you call OpenProcess if the process object is gone - the result is a call with invalid parameter.

The success you got with other utilities is either due to a race condition (which may fail some times) or you kept the original handle to the child process open.

like image 168
John Avatar answered Nov 17 '25 10:11

John



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!