Enumerating threads in a process

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Enumerating threads in a process

Post by Mistrel »

Source: http://blogs.msdn.com/oldnewthing/archi ... 37856.aspx
MSDN wrote:To identify the threads that belong to a specific process, compare its process identifier to the th32OwnerProcessID member of the THREADENTRY32 structure when enumerating the threads.

Code: Select all

Define te.THREADENTRY32

h=CreateToolhelp32Snapshot_(#TH32CS_SNAPTHREAD,0);

If (h<>#INVALID_HANDLE_VALUE)
	te\dwSize=SizeOf(te)
	If (Thread32First_(h,@te))
			Repeat
			If (te\dwSize>=OffsetOf(THREADENTRY32\th32OwnerProcessID)+SizeOf(te\th32OwnerProcessID))
				Debug "Process "+RSet(Hex(te\th32OwnerProcessID),4,"0")+" Thread "+RSet(Hex(te\th32ThreadID),4,"0")
			EndIf
			te\dwSize=SizeOf(te);
		Until Not (Thread32Next_(h,@te));
	EndIf
	CloseHandle_(h);
EndIf
I love PureBasic. :D

Can someone confirm if 'Process 0' and 'Thread 0' is not an error?
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Post by jpd »

Hi Mistrel,

the PID 0 is the System Idle Process,

so is Right!

Best
jpd
PB 5.10 Windows 7 x64 SP1
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Thanks! :D
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

Post by Hi-Toro »

Nice little piece of code!
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Post by jpd »

Mistrel wrote:Thanks! :D
Yepp!
:)
PB 5.10 Windows 7 x64 SP1
Post Reply