fix: Windows PID check

This commit is contained in:
Xu Lingrui 2026-06-03 00:02:55 +08:00
parent 824d504f61
commit 228f8f7807
No known key found for this signature in database

View file

@ -38,14 +38,30 @@ def _is_pid_alive(pid: int) -> bool:
try:
if sys.platform == "win32":
import ctypes
kernel32 = ctypes.windll.kernel32
from ctypes import wintypes
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
STILL_ACTIVE = 259
ERROR_ACCESS_DENIED = 5
kernel32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
kernel32.OpenProcess.restype = wintypes.HANDLE
kernel32.GetExitCodeProcess.argtypes = [
wintypes.HANDLE,
ctypes.POINTER(wintypes.DWORD),
]
kernel32.GetExitCodeProcess.restype = wintypes.BOOL
kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
kernel32.CloseHandle.restype = wintypes.BOOL
handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
if not handle:
if ctypes.get_last_error() == ERROR_ACCESS_DENIED:
return True
return False
try:
exit_code = ctypes.c_ulong()
exit_code = wintypes.DWORD()
if kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)):
return exit_code.value == STILL_ACTIVE
return False