0

我正在使用在此处下载的 pydbg 二进制文件:http ://www.lfd.uci.edu/~gohlke/pythonlibs/#pydbg ,如先前答案中所建议的那样。

我可以让 32 位版本与 32 位 Python 解释器一起工作,但我无法让 64 位版本与 64 位 Python 一起工作。enumerate_processes()总是返回一个空列表。我做错了吗?

测试代码:

import pydbg

if __name__ == "__main__":
    print(pydbg.pydbg().enumerate_processes())

32位工作:

>C:\Python27-32\python-32bit.exe
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
...
>C:\Python27-32\python-32bit.exe pydbg_test.py
[(0L, '[System Process]'), (4L, 'System'), <redacted for brevity>]

64 位给出一个空列表:

>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
...
>python pydbg_test.py
[]
4

1 回答 1

1

Pydbg 定义的 PROCESSENTRY32 结构错误。

最好使用维护的包,例如psutil或直接使用 ctypes,例如:

from ctypes import windll, Structure, c_char, sizeof
from ctypes.wintypes import BOOL, HANDLE, DWORD, LONG, ULONG, POINTER

class PROCESSENTRY32(Structure):
    _fields_ = [
        ('dwSize', DWORD),
        ('cntUsage', DWORD),
        ('th32ProcessID', DWORD),
        ('th32DefaultHeapID', POINTER(ULONG)),
        ('th32ModuleID', DWORD),
        ('cntThreads', DWORD),
        ('th32ParentProcessID', DWORD),
        ('pcPriClassBase', LONG),
        ('dwFlags', DWORD),
        ('szExeFile', c_char * 260),
    ]

windll.kernel32.CreateToolhelp32Snapshot.argtypes = [DWORD, DWORD]
windll.kernel32.CreateToolhelp32Snapshot.restype = HANDLE
windll.kernel32.Process32First.argtypes = [HANDLE, POINTER(PROCESSENTRY32)]
windll.kernel32.Process32First.restype = BOOL
windll.kernel32.Process32Next.argtypes = [HANDLE, POINTER(PROCESSENTRY32)]
windll.kernel32.Process32Next.restype = BOOL
windll.kernel32.CloseHandle.argtypes = [HANDLE]
windll.kernel32.CloseHandle.restype = BOOL

pe = PROCESSENTRY32()
pe.dwSize = sizeof(PROCESSENTRY32)

snapshot = windll.kernel32.CreateToolhelp32Snapshot(2, 0)
found_proc = windll.kernel32.Process32First(snapshot, pe)
while found_proc:
    print(pe.th32ProcessID, pe.szExeFile)
    found_proc = windll.kernel32.Process32Next(snapshot, pe)

windll.kernel32.CloseHandle(snapshot)
于 2016-09-05T22:00:53.930 回答