如何使用 winapi 等待非子进程?

how to wait for a non-child process with winapi?

我阅读了:

代码片段:

HANDLE  hProcess = OpenProcess(SYNCHRONIZE, TRUE, inProcessID); 

if (NULL == hProcess)
{
    WaitForSingleObject(hProcess,INFINITE);
}

我试过 WaitForSingleObjectWaitForSingleObjectEx,实际上都没有在等待。

例如,假设记事本是 运行,我想等待它被某些用户关闭。我该怎么办 ?

来自 OpenProcess 的文档:

If the function succeeds, the return value is an open handle to the specified process.

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

所以你的 if 语句应该是:

if (NULL != hProcess) ...