This seems like it should print the thread ID of the first thread in the snapshot, but it always prints 0
. What is wrong with it?
<code>import ctypes
from ctypes import wintypes
pid = 1234
TH32CS_SNAPTHREAD = 0x00000004
kernel32 = ctypes.windll.kernel32
class THREADENTRY32(ctypes.Structure):
_fields_ = [
('dwSize', wintypes.DWORD),
('cntUsage', wintypes.DWORD),
('th32ThreadID', wintypes.DWORD),
('th32OwnerProcessID', wintypes.DWORD),
('tpBasePri', wintypes.LONG),
('tpDeltaPri', wintypes.LONG),
('dwFlags', wintypes.DWORD)
]
CreateToolhelp32Snapshot = kernel32.CreateToolhelp32Snapshot
CreateToolhelp32Snapshot.argtypes = (wintypes.DWORD, wintypes.DWORD, )
CreateToolhelp32Snapshot.restype = wintypes.HANDLE
h_snapshot = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pid)
LPTHREADENTRY32 = ctypes.POINTER(THREADENTRY32)
Thread32First = kernel32.Thread32First
Thread32First.argtypes = (wintypes.HANDLE, LPTHREADENTRY32, )
Thread32First.restype = wintypes.BOOL
thread_entry = THREADENTRY32()
thread_entry.dwSize = ctypes.sizeof(THREADENTRY32)
if kernel32.Thread32First(h_snapshot, ctypes.byref(thread_entry)):
print(thread_entry.th32ThreadID)
</code>
<code>import ctypes
from ctypes import wintypes
pid = 1234
TH32CS_SNAPTHREAD = 0x00000004
kernel32 = ctypes.windll.kernel32
class THREADENTRY32(ctypes.Structure):
_fields_ = [
('dwSize', wintypes.DWORD),
('cntUsage', wintypes.DWORD),
('th32ThreadID', wintypes.DWORD),
('th32OwnerProcessID', wintypes.DWORD),
('tpBasePri', wintypes.LONG),
('tpDeltaPri', wintypes.LONG),
('dwFlags', wintypes.DWORD)
]
CreateToolhelp32Snapshot = kernel32.CreateToolhelp32Snapshot
CreateToolhelp32Snapshot.argtypes = (wintypes.DWORD, wintypes.DWORD, )
CreateToolhelp32Snapshot.restype = wintypes.HANDLE
h_snapshot = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pid)
LPTHREADENTRY32 = ctypes.POINTER(THREADENTRY32)
Thread32First = kernel32.Thread32First
Thread32First.argtypes = (wintypes.HANDLE, LPTHREADENTRY32, )
Thread32First.restype = wintypes.BOOL
thread_entry = THREADENTRY32()
thread_entry.dwSize = ctypes.sizeof(THREADENTRY32)
if kernel32.Thread32First(h_snapshot, ctypes.byref(thread_entry)):
print(thread_entry.th32ThreadID)
</code>
import ctypes
from ctypes import wintypes
pid = 1234
TH32CS_SNAPTHREAD = 0x00000004
kernel32 = ctypes.windll.kernel32
class THREADENTRY32(ctypes.Structure):
_fields_ = [
('dwSize', wintypes.DWORD),
('cntUsage', wintypes.DWORD),
('th32ThreadID', wintypes.DWORD),
('th32OwnerProcessID', wintypes.DWORD),
('tpBasePri', wintypes.LONG),
('tpDeltaPri', wintypes.LONG),
('dwFlags', wintypes.DWORD)
]
CreateToolhelp32Snapshot = kernel32.CreateToolhelp32Snapshot
CreateToolhelp32Snapshot.argtypes = (wintypes.DWORD, wintypes.DWORD, )
CreateToolhelp32Snapshot.restype = wintypes.HANDLE
h_snapshot = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pid)
LPTHREADENTRY32 = ctypes.POINTER(THREADENTRY32)
Thread32First = kernel32.Thread32First
Thread32First.argtypes = (wintypes.HANDLE, LPTHREADENTRY32, )
Thread32First.restype = wintypes.BOOL
thread_entry = THREADENTRY32()
thread_entry.dwSize = ctypes.sizeof(THREADENTRY32)
if kernel32.Thread32First(h_snapshot, ctypes.byref(thread_entry)):
print(thread_entry.th32ThreadID)