About Monkey 2 › Forums › Monkey 2 Code Library › Win32 process list/check for running process
Tagged: Process, process list, processes, task list, tasks, win32
This topic contains 0 replies, has 1 voice, and was last updated by DruggedBunny 5 months ago.
-
AuthorPosts
-
September 15, 2018 at 1:08 pm #15438
This is just a couple of functions for reading the list of currently-running processes on Windows as a string array, and for checking if a given process name is running (eg. “notepad.exe”).
Only tested on Windows 7 64-bit, built as a 64-bit executable via MSVC.
Would be interested to hear if it succeeds or fails on 32-bit Windows, or when built as a 32-bit executable!
[EDIT: Quick fix for stoopid.]
Monkey
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179#Import "<std>"#Import "<win32>" ' Some simple Win32 types/structs#Import "<windows.h>" ' Win32 headers#Import "<tlhelp32.h>" ' Toolhelp32 library containing the process iteration functionsUsing std..Using win32..Const MAX_PATH:Int = 260Const TH32CS_SNAPHEAPLIST:Int = $1Const TH32CS_SNAPPROCESS:Int = $2Const TH32CS_SNAPTHREAD:Int = $4Const TH32CS_SNAPMODULE:Int = $8Const TH32CS_SNAPALL:Int = (TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE)Const TH32CS_INHERIT:Int = $80000000ExternAlias LPPROCESSENTRY32:PROCESSENTRY32 PtrStruct PROCESSENTRY32Field dwSize:DWORDField cntUsage:DWORDField th32ProcessID:DWORDField th32DefaultHeapID:ULong PtrField th32ModuleID:DWORDField cntThreads:DWORDField th32ParentProcessID:DWORDField pcPriClassBase:LongField dwFlags:DWORDField szExeFile:WCHAR [] ' Should be WCHAR [MAX_PATH], Win32 seems to allocate the area, though, per libc.sizeof()...EndFunction CreateToolhelp32Snapshot: HANDLE (dwFlags:DWORD, th32ProcessID:DWORD)Function CloseHandle: Bool (hObject:HANDLE)Function Process32First: Bool (hSnapshot:HANDLE, lppe:LPPROCESSENTRY32)Function Process32Next: Bool (hSnapshot:HANDLE, lppe:LPPROCESSENTRY32)PublicFunction ProcessRunning:Bool (process:String)process = process.ToLower ()Local processes:String [] = GetProcessList ()For Local proc:String = Eachin processesIf proc.ToLower () = process Then Return TrueNextReturn FalseEndFunction GetProcessList:String [] (sorted:Bool = True)Local snap:HANDLE = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0)If snapLocal processes:Stack <String> = New Stack <String>Local proc:PROCESSENTRY32 = New PROCESSENTRY32proc.dwSize = libc.sizeof (proc)' Ouchie! Pointer to process entry structure...Local pe32ptr:LPPROCESSENTRY32 = Cast <LPPROCESSENTRY32> (Varptr proc)Local exe:StringIf Process32First (snap, pe32ptr)For Local loop:Int = 0 Until MAX_PATHLocal w_char:WCHAR = proc.szExeFile[loop]If w_charexe = exe + String.FromChar (w_char)ElseExitEndifNextprocesses.Add (exe)While Process32Next (snap, pe32ptr)exe = ""For Local loop:Int = 0 Until MAX_PATHLocal w_char:WCHAR = proc.szExeFile[loop]If w_charexe = exe + String.FromChar (w_char)ElseExitEndifNextprocesses.Add (exe)WendEndifCloseHandle (snap)If sorted Then processes.Sort (' Modified from Stack module's Sort method...' Compares lower-case versions of process names, otherwise' the list is made up of sorted capitalised names first, then' sorted non-capitalised names (so A-Z followed by a-z)...Lambda:Int (x:String, y:String)Return x.ToLower () <=> y.ToLower ()End)Return processes.ToArray ()EndifReturn NullEnd' Demo...Function Main:Void ()Local process:String' Get list of processes:Local processes:String [] = GetProcessList () ' Pass False for default order returned by WindowsPrint "Running processes:"Print ""For process = Eachin processesPrint processNextPrint ""' Check a few specific processes:process = "Ted2.exe"If ProcessRunning (process)Print "Process " + process + " is running"ElsePrint "Process " + process + " is not running"Endifprocess = "notepad.exe"If ProcessRunning (process)Print "Process " + process + " is running"ElsePrint "Process " + process + " is not running"Endifprocess = "explorer.exe"If ProcessRunning (process)Print "Process " + process + " is running"ElsePrint "Process " + process + " is not running"EndifEnd -
AuthorPosts
You must be logged in to reply to this topic.