Page 1 of 2
List of running processes...
Posted: Tue Oct 28, 2003 1:08 am
by Hi-Toro
Code updated for 5.20+
FIXED: This post and next one -- now works on '98!
Code: Select all
; MAKE SURE DEBUGGER IS ON!!!
#TH32CS_SNAPHEAPLIST = $1
#TH32CS_SNAPPROCESS = $2
#TH32CS_SNAPTHREAD = $4
#TH32CS_SNAPMODULE = $8
#TH32CS_SNAPALL = #TH32CS_SNAPHEAPLIST | #TH32CS_SNAPPROCESS | #TH32CS_SNAPTHREAD | #TH32CS_SNAPMODULE
#TH32CS_INHERIT = $80000000
#INVALID_HANDLE_VALUE = -1
#PROCESS32LIB = 9999
; NOTE: I've chosen to add processes to this list so that it can be played with as necessary...
NewList Process32.PROCESSENTRY32 ()
; Add processes to Process32 () list...
If OpenLibrary (#PROCESS32LIB, "kernel32.dll")
snap = CallFunction (#PROCESS32LIB, "CreateToolhelp32Snapshot", #TH32CS_SNAPPROCESS, 0)
If snap
Define.PROCESSENTRY32 Proc32
Proc32\dwSize = SizeOf (PROCESSENTRY32)
If CallFunction (#PROCESS32LIB, "Process32First", snap, @Proc32)
AddElement (Process32 ())
CopyMemory (@Proc32, @Process32 (), SizeOf (PROCESSENTRY32))
While CallFunction (#PROCESS32LIB, "Process32Next", snap, @Proc32)
AddElement (Process32 ())
CopyMemory (@Proc32, @Process32 (), SizeOf (PROCESSENTRY32))
Wend
EndIf
CloseHandle_ (snap)
EndIf
CloseLibrary (#PROCESS32LIB)
EndIf
; List processes...
ResetList (Process32 ())
While NextElement (Process32 ())
Debug PeekS (@Process32 ()\szExeFile)
Wend
... and even if no-one cares, there's more!
Posted: Wed Oct 29, 2003 1:25 am
by Hi-Toro
Code updated for 5.20+
Code: Select all
; -----------------------------------------------------------------------------
; Constants required by process functions, etc...
; -----------------------------------------------------------------------------
#TH32CS_SNAPHEAPLIST = $1
#TH32CS_SNAPPROCESS = $2
#TH32CS_SNAPTHREAD = $4
#TH32CS_SNAPMODULE = $8
#TH32CS_SNAPALL = #TH32CS_SNAPHEAPLIST | #TH32CS_SNAPPROCESS | #TH32CS_SNAPTHREAD | #TH32CS_SNAPMODULE
#TH32CS_INHERIT = $80000000
#INVALID_HANDLE_VALUE = -1
#MAX_PATH = 260
#PROCESS32LIB = 9999
; -----------------------------------------------------------------------------
; GLOBAL PROCESS LIST! Used to retrieve information after getting process list...
; -----------------------------------------------------------------------------
Global NewList Process32.PROCESSENTRY32 ()
; This function sorts processes into TreeGadget list, so that child processes branch off from parents...
Procedure CompareProcs (gadget, currentid, currentname$)
Debug "Comparing " + currentname$ + " [" + Str (currentid) + "]"
ResetList (Process32 ())
While NextElement (Process32 ())
; Skip if checking against 'currentid', ie. same process...
If Process32 ()\th32ProcessID <> currentid
; Check currentid against this one...
againstid = Process32 ()\th32ProcessID
againstparent = Process32 ()\th32ParentProcessID
; If 'currentid' is parent of this process...
If currentid = againstparent
proc$ = PeekS (@Process32 ()\szExeFile)
Debug "--------> " + proc$ + " [" + Str (Process32 ()\th32ProcessID) + "]" + " / " + " [" + Str (Process32 ()\th32ParentProcessID) + "]"
AddGadgetItem (gadget, -1, PeekS (@Process32 ()\szExeFile))
current = ListIndex (Process32 ())
CompareProcs (gadget, againstid, proc$)
SelectElement (Process32 (), current)
DeleteElement (Process32 ())
EndIf
EndIf
Wend
EndProcedure
; Add processes to Process32 () list...
If OpenLibrary (#PROCESS32LIB, "kernel32.dll")
snap = CallFunction (#PROCESS32LIB, "CreateToolhelp32Snapshot", #TH32CS_SNAPPROCESS, 0)
If snap
Define.PROCESSENTRY32 Proc32
Proc32\dwSize = SizeOf (PROCESSENTRY32)
If CallFunction (#PROCESS32LIB, "Process32First", snap, @Proc32)
AddElement (Process32 ())
CopyMemory (@Proc32, @Process32 (), SizeOf (PROCESSENTRY32))
While CallFunction (#PROCESS32LIB, "Process32Next", snap, @Proc32)
AddElement (Process32 ())
CopyMemory (@Proc32, @Process32 (), SizeOf (PROCESSENTRY32))
Wend
EndIf
CloseHandle_ (snap)
EndIf
CloseLibrary (#PROCESS32LIB)
EndIf
; Window hook, used to resize/redraw TreeGadget when window is resized...
Procedure WinHook (WindowID, Message, wParam, lParam)
If Message = #WM_PAINT
ResizeGadget (0, 0, 0, WindowWidth (0), WindowHeight (0))
RedrawWindow_ (GadgetID (0), #Null, #Null, #RDW_INVALIDATE)
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
; GUI...
OpenWindow (0, 320, 200, 320, 400, "Process list...", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
TreeGadget (0, 0, 0, WindowWidth (0), WindowHeight (0))
SetWindowCallback (@WinHook ())
; Add processes to TreeGadget...
ResetList (Process32 ())
While NextElement (Process32 ())
AddGadgetItem (0, -1, PeekS (@Process32 ()\szExeFile))
current = ListIndex (Process32 ())
CompareProcs (0, Process32 ()\th32ProcessID, PeekS (@Process32 ()\szExeFile))
SelectElement (Process32 (), current)
Wend
Repeat
Until WaitWindowEvent () = #PB_Event_CloseWindow
End
Posted: Wed Oct 29, 2003 9:07 am
by Rings
... and even if no-one cares, there's more!
it is very usefull for my next project, thx for this snippet .
forgotten to say, cannot run under NT4 and Win95 (because of missing 'CreateToolhelp32Snapshot') but who cares ?
Windows 9x...
Posted: Wed Oct 29, 2003 9:17 am
by Hi-Toro
Hi Rings,
MSDN says it's in 95 and 98 at least...
Client: Included in Windows XP, Windows 2000 Professional, Windows Me, Windows 98, and Windows 95.
Posted: Wed Oct 29, 2003 11:20 am
by dmoc
Hi-de-hi Hi-Toro!
Both versions only produce one line (kernel) here on w98

Posted: Wed Oct 29, 2003 1:38 pm
by Rings
who cares on the old OS's .
another reason to drop all those Win89 into trashcan !
Posted: Wed Oct 29, 2003 2:22 pm
by blueznl
sorry rings, some of my stuff is running on 95, 98, NT, 2K, as well as XP
you should have seen me go ballistic with gfabasic when microsoft decided to remove (part of) thunking support in xp!!! all was fine under 2k, then xp came and all my long file names routines i could throw out of the window... oh yeah, i *could* rewrite them as xp appearently suddenly supported irq (!) calls again... in fact, i did that on a few cases, but thinking about it still p*ss*s me of, so please don't utterly ignore old systems
but you are right for most newer, bigger stuff... it's just a choice: should your code run on older machines yes or no? i prefer it to run on 98 / 2k and xp, don't care much about nt
Posted: Wed Oct 29, 2003 3:41 pm
by benny
@Hi-Toro:
Damn ... good work. Thanks for sharing !

Posted: Wed Oct 29, 2003 7:03 pm
by Seldon
Thanks for sharing.
I only get 1 process running (under Win98) , that is : kernel32.dll
Is that normal ?
Weird...
Posted: Wed Oct 29, 2003 7:58 pm
by Hi-Toro
Weird. The MSDN site states this is supported on 9x, but I seem to vaguely remember reading that it might have to be called a different way, ie. by opening the library directly; however, that's what I'm doing! I might try it with the Windows equivalent of OpenLibrary/CallFunction, etc...
Gah!
Posted: Wed Oct 29, 2003 8:57 pm
by Hi-Toro
Gah! It makes no sense!
http://support.microsoft.com/default.as ... bContent=1
This page claims these functions are *only* available in 95/98/Me, where it's apparently not working properly, yet this code works on 2000 and XP, for which they suggest you must use the PSAPI functions!
Those on 9x -- do you get only one process for the code in the very first post here?
Posted: Wed Oct 29, 2003 9:03 pm
by Berikco
Cool, thanks Hi-Toro
Works fine under XP an Windows 2003 Server
Forget windows 9.... bhaa, i forgot the name of that old OS

Re: Gah!
Posted: Wed Oct 29, 2003 9:07 pm
by Henrik
Hi-Toro wrote:
Those on 9x -- do you get only one process for the code in the very first post here?
Hi Hi-Toro
Yep only one.
kernel32.dll
Best regards
Henrik
Posted: Wed Oct 29, 2003 9:33 pm
by dmoc
Yep, only one.
Re "old" os: give me ONE good reason to upgrade and I'll give several against. How's it feel to be one of Billy Boy's slobbering masses?

Posted: Wed Oct 29, 2003 10:43 pm
by blueznl
i'll dig into the archive, see if i can find an old visualbasic source that worked on all os'es and listed all processes and tasks including 'hidden' ones... must have it somewhere...