Get processes list for WinNT and Win9x

Share your advanced PureBasic knowledge/code with the community.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Get processes list for WinNT and Win9x

Post by Fred »

Code updated for 5.20+

Here we go for win98

Code: Select all

;
; List processes on Win9x.
;


#TH32CS_SNAPPROCESS = $2

Procedure GetProcessList9x()
	
	If OpenLibrary(0, "Kernel32.dll")
		
		CreateToolhelpSnapshot = GetFunction(0, "CreateToolhelp32Snapshot")
		ProcessFirst           = GetFunction(0, "Process32First")
		ProcessNext            = GetFunction(0, "Process32Next")
		
		If CreateToolhelpSnapshot And ProcessFirst And ProcessNext ; Ensure than all the functions are found
			
			Process.PROCESSENTRY32\dwSize = SizeOf(PROCESSENTRY32)
			
			Snapshot = CallFunctionFast(CreateToolhelpSnapshot, #TH32CS_SNAPPROCESS, 0)
			If Snapshot
				
				ProcessFound = CallFunctionFast(ProcessFirst, Snapshot, Process)
				While ProcessFound
					Debug PeekS(@Process\szexeFile)
					ProcessFound = CallFunctionFast(ProcessNext, Snapshot, Process)
				Wend
			EndIf
			
			CloseHandle_(Snapshot)
		EndIf
		
		CloseLibrary(0)
	EndIf
	
EndProcedure

GetProcessList9x()
And now for NT (note: the Win98 code seems to work ok on NT, but hey, who knows):

Code: Select all

; 
; List processes on WinNT.
;

Structure PROCESS_MEMORY_COUNTERS
   cb.l
   PageFaultCount.l
   PeakWorkingSetSize.l
   WorkingSetSize.l
   QuotaPeakPagedPoolUsage.l
   QuotaPagedPoolUsage.l
   QuotaPeakNonPagedPoolUsage.l
   QuotaNonPagedPoolUsage.l
   PageFileUsage.l
   PeakPagefileUsage.l
EndStructure

#OWNER_SECURITY_INFORMATION = $00000001
#GROUP_SECURITY_INFORMATION = $00000002
#DACL_SECURITY_INFORMATION  = $00000004
#SACL_SECURITY_INFORMATION  = $00000008
#PROCESS_TERMINATE          = $0001
#PROCESS_CREATE_THREAD      = $0002  
#PROCESS_SET_SESSIONID      = $0004  
#PROCESS_VM_OPERATION       = $0008  
#PROCESS_VM_READ            = $0010  
#PROCESS_VM_WRITE           = $0020  
#PROCESS_DUP_HANDLE         = $0040  
#PROCESS_CREATE_PROCESS     = $0080  
#PROCESS_SET_QUOTA          = $0100  
#PROCESS_SET_INFORMATION    = $0200  
#PROCESS_QUERY_INFORMATION  = $0400  
#PROCESS_ALL_ACCESS         = #STANDARD_RIGHTS_REQUIRED | #SYNCHRONIZE | $FFF


#NbProcessesMax = 10000
Global Dim ProcessesArray(#NbProcessesMax)


Procedure GetProcessListNt()
  
  If OpenLibrary(0, "psapi.dll")
  
    EnumProcesses      = GetFunction(0, "EnumProcesses")
    EnumProcessModules = GetFunction(0, "EnumProcessModules")
    GetModuleBaseName  = GetFunction(0, "GetModuleBaseNameA")

    If EnumProcesses And EnumProcessModules And GetModuleBaseName  ; Be sure we have detected all the functions
      
      CallFunctionFast(EnumProcesses, ProcessesArray(), #NbProcessesMax, @nProcesses)
      
      For k=1 To nProcesses/4
        hProcess = OpenProcess_(#PROCESS_QUERY_INFORMATION | #PROCESS_VM_READ, 0, ProcessesArray(k-1))
        
        If hProcess
          CallFunctionFast(EnumProcessModules, hProcess, @BaseModule, 4, @cbNeeded)
          
          Name$ = Space(255)
          CallFunctionFast(GetModuleBaseName, hProcess, BaseModule, @Name$, Len(Name$))
          Debug Name$
          CloseHandle_(hProcess)
        EndIf
      Next
     
    EndIf
    CloseLibrary(0)
  EndIf
     
EndProcedure

GetProcessListNt()
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

:!: Confirmed :!: the win98 works for 2k !!!
User avatar
Inner
PureBasic Expert
PureBasic Expert
Posts: 714
Joined: Fri Apr 25, 2003 4:47 pm
Location: New Zealand

Post by Inner »

Both work on Windows XP
CoderLaureate
User
User
Posts: 50
Joined: Fri Apr 25, 2003 7:21 pm
Location: The World is my country, all mankind are my brethren, and to do good is my religion.
Contact:

Post by CoderLaureate »

What is the structure for in the Windows NT listing?
I don't see it being used anywhere in the program.

-Jim
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

The code for W89 did not work on NT4 machines(its okay while Toolhelp-dll is not available).
SPAMINATOR NR.1
Hi-Toro
Enthusiast
Enthusiast
Posts: 269
Joined: Sat Apr 26, 2003 3:23 pm

Just found this...

Post by Hi-Toro »

Hi Fred,

Just found this post via Andre's excellent Code Archive site.

Any idea why your code works on '98 but mine fails? It's here: viewtopic.php?t=8086&highlight=

The only difference I can see is that you used CallFunctionFast -- would this account for the difference (ie. does it do things differently?)...
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Hi-Toro
Enthusiast
Enthusiast
Posts: 269
Joined: Sat Apr 26, 2003 3:23 pm

Post by Hi-Toro »

Gah! I tried converting my version to CallFunctionFast and I still only get the first process on '98! I don't get what's happening here! (:O
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Post Reply