WINSDK:确定任意pid是否标识Windows上正在运行的进程

问题描述:

试图实现穷人对进程是否仍在运行的测试(基本上等同于简单的kill(pid, 0)。)WINSDK:确定任意pid是否标识Windows上正在运行的进程

希望能够简单地调用OpenProcess并获得一些最小的所需访问权限,然后测试GetLastError() == ERROR_INVALID_PARAMETERGetExitCodeProcess(...) != STILL_ACTIVE

很好的尝试...在Windows XP上运行,作为管理员:

HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); 
if (!hProc) { 
    DWORD dwLastError = GetLastError(); 
} 

...当pid由不同的(非SYSTEM)用户拥有时,与dwLastError == ERROR_ACCESS_DENIED失败。此外,如果pid最初由不同的用户拥有但后来终止,OpenProcess也失败了ERROR_ACCESS_DENIED(而不是ERROR_INVALID_PARAMETER。)

我是否必须使用Process32First/Process32NextEnumProcesses

我绝对不想使用SeDebugPrivilege

谢谢, V

+1

另请参阅http://*.com/questions/1591342/how-to-determine-if-a-wi ndows-process-is-running可能有其他更可靠的方法 – rogerdpack 2012-09-28 00:17:51

+0

@rogerdpack在这种情况下唯一可用的信息是'pid'。没有手柄可用。我知道关于http://*.com/questions/1591342/how-to-determine-if-a-windows-process-is-running,我甚至在那里提出了一条评论。 :) – vladr 2012-10-09 18:44:52

+0

是的,更多我正在发表评论以提醒用户我已经添加了一个可能适用于此问题的答案,但也不希望将其添加到这两个地方。干杯! – rogerdpack 2012-10-10 21:29:15

static BOOL 
isProcessAlive(DWORD th32ProcessID) { 
    BOOL bSuccess = FALSE; 

    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
    if (hSnap != INVALID_HANDLE_VALUE) { 
    PROCESSENTRY32 pe32 = { sizeof(pe32), 0 }; 
    if (Process32First(hSnap, &pe32)) { 
     while (pe32.th32ProcessID != pid && Process32Next(hSnap, &pe32)); 
     _ASSERT(GetLastError() == 0 || GetLastError() == ERROR_NO_MORE_FILES); 
     bSuccess = (pe32.th32ProcessID == th32ProcessID); 
    } 
    CloseHandle(hSnap); 
    } 
    return bSuccess; 
} 

如果你有一个进程ID:

// this should succeed even when a medium integrity process 
// requests access to a high integrity process 
if (HANDLE h = OpenProcess(SYNCHRONIZE, FALSE, pid)) 
{ 
    // do a wait, if the handle is signaled: not running 
    DWORD wait = WaitForSingleObject(h, 0); 
    if (wait == WAIT_OBJECT_0) return FALSE; 
} 
// cannot get a handle to the process: 
// probably running at system integrity level 
// I'm not sure how reliable this check is, but it seems to work: 
// if access is denied: running 
// if invalid parameter: not running 
else if (GetLastError() != ERROR_ACCESS_DENIED) return FALSE; 

如果你有一个窗口句柄应该只要进程正在运行是有效的,这是一个很好的选择:

if (hWnd && !IsWindow(hWnd)) return FALSE; 
+0

Windows XP之后是否修复了这个问题? “如果'pid'最初由不同的用户拥有,但已经终止,'OpenProcess'也会因'ERROR_ACCESS_DENIED'(而不是'ERROR_INVALID_PARAMETER')失败。” – vladr 2015-02-10 00:42:39