pid_alive() now works under Windows64

This commit is contained in:
David Campey 2010-03-19 12:33:50 +02:00
parent 9a72b97768
commit 4a864f71cd

View file

@ -250,7 +250,7 @@ def pid_alive():
if os.name == 'nt': if os.name == 'nt':
try: try:
from ctypes import (windll, c_ulong, c_int, Structure, c_char) from ctypes import (windll, c_ulong, c_int, Structure, c_char)
from ctypes import (POINTER, pointer, ) from ctypes import (POINTER, pointer, sizeof)
except Exception: except Exception:
return True return True
@ -267,26 +267,26 @@ def pid_alive():
('dwFlags', c_ulong, ), ('dwFlags', c_ulong, ),
('szExeFile', c_char*512, ), ('szExeFile', c_char*512, ),
] ]
def __init__(self):
super(PROCESSENTRY32, self).__init__(self, 512+9*4)
k = windll.kernel32 kernel = windll.kernel32
k.CreateToolhelp32Snapshot.argtypes = c_ulong, c_ulong, kernel.CreateToolhelp32Snapshot.argtypes = c_ulong, c_ulong,
k.CreateToolhelp32Snapshot.restype = c_int kernel.CreateToolhelp32Snapshot.restype = c_int
k.Process32First.argtypes = c_int, POINTER(PROCESSENTRY32), kernel.Process32First.argtypes = c_int, POINTER(PROCESSENTRY32),
k.Process32First.restype = c_int kernel.Process32First.restype = c_int
k.Process32Next.argtypes = c_int, POINTER(PROCESSENTRY32), kernel.Process32Next.argtypes = c_int, POINTER(PROCESSENTRY32),
k.Process32Next.restype = c_int kernel.Process32Next.restype = c_int
def get_p(pid_): def get_p(pid_):
h = k.CreateToolhelp32Snapshot(2, 0) # TH32CS_SNAPPROCESS TH32CS_SNAPPROCESS = 2
assert h > 0, 'CreateToolhelp32Snapshot failed' CreateToolhelp32Snapshot = kernel.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
b = pointer(PROCESSENTRY32()) assert CreateToolhelp32Snapshot > 0, 'CreateToolhelp32Snapshot failed'
f3 = k.Process32First(h, b) pe32 = PROCESSENTRY32()
pe32.dwSize = sizeof( PROCESSENTRY32 )
f3 = kernel.Process32First(CreateToolhelp32Snapshot, pointer(pe32))
while f3: while f3:
if b.contents.th32ProcessID == pid_: if pe32.th32ProcessID == pid_:
return b.contents.szExeFile return pe32.szExeFile
f3 = k.Process32Next(h, b) f3 = kernel.Process32Next(CreateToolhelp32Snapshot, pointer(pe32))
if get_p(pid) in ('python.exe', 'gajim.exe'): if get_p(pid) in ('python.exe', 'gajim.exe'):
return True return True