File: n_processlist.sru
Size: 3382
Date: Thu, 29 May 2025 21:03:38 +0200
$PBExportHeader$n_processlist.sru
forward
global type n_processlist from nonvisualobject
end type
type processentry32 from structure within n_processlist
end type
end forward

type processentry32 from structure
   unsignedlong      dwsize
   unsignedlong      cntusage
   unsignedlong      th32processid
   long     th32defaultheapid
   unsignedlong      th32moduleid
   unsignedlong      cntthreads
   unsignedlong      th32parentprocessid
   long     pcpriclassbase
   unsignedlong      dwflags
   character      szexefile[260]
end type

global type n_processlist from nonvisualobject autoinstantiate
end type

type prototypes
Function long CreateToolhelp32Snapshot ( &
   ulong dwFlags, &
   ulong th32ProcessID &
   ) Library "kernel32.dll"

Function boolean CloseHandle ( &
   long hObject &
   ) Library "kernel32.dll"

Function boolean Process32First ( &
   long hSnapshot, &
   Ref PROCESSENTRY32 lppe &
   ) Library "kernel32.dll" Alias For "Process32FirstW"

Function boolean Process32Next ( &
   long hSnapshot, &
   Ref PROCESSENTRY32 lppe &
   ) Library "kernel32.dll" Alias For "Process32NextW"

Function long OpenProcess ( &
   ulong dwDesiredAccess, &
   boolean bInheritHandle, &
   ulong dwProcessId &
   ) Library "kernel32.dll"

Function boolean QueryFullProcessImageName ( &
   long hProcess, &
   ulong dwFlags, &
   Ref string lpExeName, &
   Ref ulong lpdwSize &
   ) Library "kernel32.dll" Alias For "QueryFullProcessImageNameW"

end prototypes

forward prototypes
public function long processlist (ref unsignedlong aul_processid[], ref string as_exefilename[], ref string as_fullexefile[])
end prototypes

public function long processlist (ref unsignedlong aul_processid[], ref string as_exefilename[], ref string as_fullexefile[]);// --------------------------------------------------------------
// ProcessList - Return a list of processes
// --------------------------------------------------------------

PROCESSENTRY32 lstr_pe32
Constant ULong TH32CS_SNAPPROCESS = 2
Constant ULong PROCESS_QUERY_INFORMATION = 1024    // 0x0400
Constant ULong PROCESS_NAME_NATIVE = 1

Long ll_next
Long lptr_SnapShot, lptr_Process
String ls_empty[], ls_FullName
ULong lul_empty[], lul_dwSize

aul_ProcessId  = lul_empty
as_ExeFileName = ls_empty
as_FullExeFile = ls_empty

lstr_pe32.dwSize = 556

lptr_SnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If lptr_SnapShot > 0 Then
   If Process32First(lptr_SnapShot, lstr_pe32) Then
      Do
         If lstr_pe32.th32ProcessID > 0 Then
            lptr_Process = OpenProcess(PROCESS_QUERY_INFORMATION, &
                                 False, lstr_pe32.th32ProcessID)
            If lptr_Process > 0 Then
               ll_next ++

               aul_ProcessId[ll_next]  = lstr_pe32.th32ProcessID
               as_ExeFileName[ll_next] = String(lstr_pe32.szExeFile)
               as_FullExeFile[ll_next] = ""

               lul_dwSize = 260
               ls_FullName = Space(lul_dwSize)

               If QueryFullProcessImageName(lptr_Process, &
                        0, ls_FullName, lul_dwSize) Then
                  as_FullExeFile[ll_next] = ls_FullName
               End If

               CloseHandle(lptr_Process)
            End If
         End If
      Loop While Process32Next(lptr_SnapShot, lstr_pe32)
   End If
   CloseHandle(lptr_SnapShot)
End If

Return ll_next

end function

on n_processlist.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_processlist.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on