OpenProcess error 87 invalid parameter

拥有回忆 提交于 2019-12-02 05:33:07

问题


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?


回答1:


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.




回答2:


Although the post is old: I noticed that I got ERROR_INVALID_PARAMETER when the process existed, but owned by different user and/or Windows desktop and/or terminal server session.

Strange enough, the WTSEnumerateProcess() function does not suffer from this error, but is much more expensive, especially on a system that is already under heavy load with many processes (and a call my even exhaust windows kernel resources).

So, it is not possible to make a 'real' invalid parameter provided and access errors. I would have expected ERROR_ACCESS_DENIED instead (but a task manager invoked as regular/non-elevated user still shows all processes).

Look like some inconsistencies in Windows?



来源:https://stackoverflow.com/questions/4988082/openprocess-error-87-invalid-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!