gpt4 book ai didi

windows - WINSDK : Determining whether an arbitrary pid identifies a running process on Windows

转载 作者:可可西里 更新时间:2023-11-01 10:40:33 25 4
gpt4 key购买 nike

尝试实现一个进程是否仍在运行的穷人测试(本质上等同于琐碎的 kill(pid, 0)。)

希望能够简单地调用 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/Process32Next 还是 EnumProcesses

我绝对不想使用SeDebugPrivilege

谢谢,V

最佳答案

如果您有进程 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;

关于windows - WINSDK : Determining whether an arbitrary pid identifies a running process on Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2384022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com