Page 1 of 1

Get processes list for WinNT and Win9x

Posted: Thu Jul 10, 2003 5:05 pm
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()

Posted: Thu Jul 10, 2003 7:19 pm
by Num3
:!: Confirmed :!: the win98 works for 2k !!!

Posted: Thu Jul 10, 2003 7:38 pm
by Inner
Both work on Windows XP

Posted: Thu Jul 10, 2003 7:51 pm
by CoderLaureate
What is the structure for in the Windows NT listing?
I don't see it being used anywhere in the program.

-Jim

Posted: Fri Jul 11, 2003 9:34 am
by Rings
The code for W89 did not work on NT4 machines(its okay while Toolhelp-dll is not available).

Just found this...

Posted: Fri Nov 28, 2003 12:07 am
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?)...

Posted: Fri Nov 28, 2003 12:18 am
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