1
私はctypesモジュールとWinAPIを使ってプロセス名をPIDで探しています。 私はthisのC/C++で書かれた例を見てきましたが、私のszExeFile
のサイズがすべてのプロセスで0であるという事実を除いて動作しています。このAPIを使用している間に何か不足していますか?PIDでプロセス名を見つける
def find_pid_with_name(process_name: str):
entry = PROCESSENTRY32()
entry.dwSize = sizeof(PROCESSENTRY32)
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, None)
if Process32First(snapshot, byref(entry)) == TRUE:
while Process32Next(snapshot, byref(entry)) == TRUE:
print(libc.wcslen(entry.szExeFile))
CloseHandle(snapshot)
PROCESSENTRY32
のための私の構造体の定義:
MAX_PATH = 260
class PROCESSENTRY32(Structure):
_fields_ = [
("dwSize", c_ulong),
("cntUsage", c_ulong),
("th32ProcessID", c_ulong),
("th32DefaultHeapID", POINTER(c_ulong)),
("th32ModuleId", c_ulong),
("cntThreads", c_ulong),
("th32ParentProcessID", c_ulong),
("dwFlags", c_ulong),
("szExeFile", c_wchar * MAX_PATH)
]
そして、私の関数の定義:
CreateToolhelp32Snapshot = windll.kernel32.CreateToolhelp32Snapshot
CreateToolhelp32Snapshot.argtypes = [c_ulong, POINTER(c_ulong)]
CreateToolhelp32Snapshot.restype = c_ulong
libc = CDLL("msvcrt")
libc.wcslen.argtypes = [c_wchar_p]
Process32First = windll.kernel32.Process32First
Process32First.argtypes = [c_ulong, POINTER(PROCESSENTRY32)]
Process32First.restype = c_ubyte
Process32Next = windll.kernel32.Process32Next
Process32Next.argtypes = [c_ulong, POINTER(PROCESSENTRY32)]
Process32Next.restype = c_ubyte
ありがとうございました。 do-whileループの使用を指摘してくれてありがとう。 – jacob