' Enumerate running processes via Toolhelp32 DECLARE
#COMPILE EXE
#DIM ALL

DECLARE FUNCTION CreateToolhelp32Snapshot LIB "KERNEL32.DLL" (BYVAL dwFlags AS LONG, BYVAL th32ProcessID AS LONG) AS LONG
DECLARE FUNCTION Process32First LIB "KERNEL32.DLL" ALIAS "Process32FirstW" (BYVAL hSnapshot AS LONG, BYREF lppe As Byte) AS LONG
DECLARE FUNCTION Process32Next LIB "KERNEL32.DLL" ALIAS "Process32NextW" (BYVAL hSnapshot AS LONG, BYREF lppe As Byte) AS LONG
DECLARE FUNCTION CloseHandle LIB "KERNEL32.DLL" (BYVAL hObject AS LONG) AS LONG
DECLARE FUNCTION OpenProcess LIB "KERNEL32.DLL" (BYVAL dwDesiredAccess AS LONG, BYVAL bInheritHandle AS LONG, BYVAL dwProcessId AS LONG) AS LONG
DECLARE FUNCTION GetExitCodeProcess LIB "KERNEL32.DLL" (BYVAL hProcess AS LONG, BYREF lpExitCode AS LONG) AS LONG
DECLARE FUNCTION TerminateProcess LIB "KERNEL32.DLL" (BYVAL hProcess AS LONG, BYVAL uExitCode AS LONG) AS LONG

%TH32CS_SNAPPROCESS = 2
%PROCESS_QUERY_INFORMATION = &H400
%PROCESS_TERMINATE = 1

FUNCTION EnumProcesses() AS LONG
    LOCAL hSnap AS LONG
    hSnap = CreateToolhelp32Snapshot(%TH32CS_SNAPPROCESS, 0)
    IF hSnap = -1 THEN
        PRINT "Failed to create process snapshot"
        FUNCTION = 0
    END IF
    LOCAL data(0:127) AS BYTE
    LOCAL result AS LONG
    result = Process32First(hSnap, data(0))
    LOCAL count AS LONG
    count = 0
    WHILE result
        count = count + 1
        LOCAL pid  AS LONG
        LOCAL parentPid  AS LONG
        pid = data(12) OR (data(13) * 256) OR (data(14) * 65536) OR (data(15) * 16777216)
        parentPid = data(24) OR (data(25) * 256) OR (data(26) * 65536) OR (data(27) * 16777216)
        LOCAL name AS STRING
        LOCAL i AS LONG
        name = ""
        FOR i = 36 TO 75 STEP 2
            LOCAL b1  AS LONG
            LOCAL b2  AS LONG
            b1 = data(i)
            b2 = data(i + 1)
            IF b1 = 0 AND b2 = 0 THEN EXIT FOR
            name = name + CHR$(b1)
        NEXT i
        PRINT "  PID "; pid; " PPID "; parentPid; " "; name
        result = Process32Next(hSnap, data(0))
    WEND
    CloseHandle(hSnap)
    PRINT "Total processes: "; count
    FUNCTION = count
END FUNCTION

SUB GetProcessInfo()
    PRINT "Process information:"
    PRINT "  Each process has a unique PID"
    PRINT "  Parent PID shows creating process"
    PRINT "  Thread count and priority available"
    PRINT ""
    PRINT "Common system processes:"
    PRINT "  PID 4     System"
    PRINT "  PID 392   smss.exe"
    PRINT "  PID 532   csrss.exe"
    PRINT "  PID 580   wininit.exe"
    PRINT "  PID 620   services.exe"
    PRINT "  PID 632   lsass.exe"
    PRINT "  PID 800   svchost.exe"
    PRINT "  PID 1200  explorer.exe"
END SUB

SUB ProcessMetrics()
    PRINT "Process metrics:"
    DIM pids(0:5) AS LONG
    DIM names(0:5) AS STRING
    pids(0) = 4
    names(0) = "System"
    pids(1) = 392
    names(1) = "smss.exe"
    pids(2) = 620
    names(2) = "services.exe"
    pids(3) = 1200
    names(3) = "explorer.exe"
    pids(4) = 2500
    names(4) = "notepad.exe"
    pids(5) = 3100
    names(5) = "cmd.exe"
    LOCAL i AS LONG
    FOR i = 0 TO 5
        PRINT "  PID "; pids(i); " "; names(i)
        PRINT "    Priority: Normal"
        PRINT "    Threads: "; (i * 3 + 5)
        PRINT "    Memory: "; (i * 12 + 4); " MB"
    NEXT i
END SUB

SUB FindProcess(BYVAL target AS STRING)
    PRINT "Searching for: "; target
    DIM known(0:4) AS STRING
    known(0) = "explorer.exe"
    known(1) = "notepad.exe"
    known(2) = "chrome.exe"
    known(3) = "cmd.exe"
    known(4) = "svchost.exe"
    LOCAL i  AS LONG
    LOCAL found  AS LONG
    found = 0
    FOR i = 0 TO 4
        IF UCASE$(known(i)) = UCASE$(target) THEN
            PRINT "  Found: "; known(i)
            found = 1
        END IF
    NEXT i
    IF found = 0 THEN
        PRINT "  Process not found"
    END IF
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=== Process Enumeration Demo ==="
    PRINT "Win32: Toolhelp32Snapshot, Process32First via DECLARE"
    PRINT ""
    GetProcessInfo()
    PRINT ""
    ProcessMetrics()
    PRINT ""
    FindProcess("notepad.exe")
    FindProcess("chrome.exe")
    FindProcess("unknown.exe")
    PRINT ""
    PRINT "=== End Process Enum ==="
    FUNCTION = 0
END FUNCTION
