Page 1 of 1

Enumerating threads in a process

Posted: Thu Dec 20, 2007 6:16 am
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?

Posted: Thu Dec 20, 2007 11:46 am
by jpd
Hi Mistrel,

the PID 0 is the System Idle Process,

so is Right!

Best
jpd

Posted: Thu Dec 20, 2007 7:01 pm
by Mistrel
Thanks! :D

Posted: Thu Dec 20, 2007 11:23 pm
by Hi-Toro
Nice little piece of code!

Posted: Fri Dec 21, 2007 11:39 am
by jpd
Mistrel wrote:Thanks! :D
Yepp!
:)