Can anyone fix this code so i can get all the window processes ?

Started by Hardcoal, December 29, 2024, 14:39:00

Previous topic - Next topic

Hardcoal

Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

fielder

Quote from: Hardcoal on February 26, 2025, 10:32:06compiling what?
compiling this:


SuperStrict

Type PROCESSENTRY32
    Field dwSize:Int
    Field cntUsage:Int
    Field th32ProcessID:Int
    Field th32DefaultHeapID:Byte Ptr
    Field th32ModuleID:Int
    Field cntThreads:Int
    Field th32ParentProcessID:Int
    Field pcPriClassBase:Int
    Field dwFlags:Int

    ' need 260 bytes inline here
    Field szExeFile00:Int, szExeFile01:Int, szExeFile02:Int, szExeFile03:Int
    Field szExeFile04:Int, szExeFile05:Int, szExeFile06:Int, szExeFile07:Int
    Field szExeFile08:Int, szExeFile09:Int, szExeFile10:Int, szExeFile11:Int
    Field szExeFile12:Int, szExeFile13:Int, szExeFile14:Int, szExeFile15:Int
    Field szExeFile16:Int, szExeFile17:Int, szExeFile18:Int, szExeFile19:Int
    Field szExeFile20:Int, szExeFile21:Int, szExeFile22:Int, szExeFile23:Int
    Field szExeFile24:Int, szExeFile25:Int, szExeFile26:Int, szExeFile27:Int
    Field szExeFile28:Int, szExeFile29:Int, szExeFile30:Int, szExeFile31:Int
    Field szExeFile32:Int, szExeFile33:Int, szExeFile34:Int, szExeFile35:Int
    Field szExeFile36:Int, szExeFile37:Int, szExeFile38:Int, szExeFile39:Int
    Field szExeFile40:Int, szExeFile41:Int, szExeFile42:Int, szExeFile43:Int
    Field szExeFile44:Int, szExeFile45:Int, szExeFile46:Int, szExeFile47:Int
    Field szExeFile48:Int, szExeFile49:Int, szExeFile50:Int, szExeFile51:Int
    Field szExeFile52:Int, szExeFile53:Int, szExeFile54:Int, szExeFile55:Int
    Field szExeFile56:Int, szExeFile57:Int, szExeFile58:Int, szExeFile59:Int
    Field szExeFile60:Int, szExeFile61:Int, szExeFile62:Int, szExeFile63:Int
    Field szExeFile64:Int
End Type

Const TH32CS_SNAPPROCESS:Int = $00000002

' Declare Windows API functions
Extern "win32"
    Function CreateToolhelp32Snapshot:Int(flags:Int, processID:Int)
    Function Process32First:Int(handle:Int, lppe:Byte Ptr)
    Function Process32Next:Int(handle:Int, lppe:Byte Ptr)
    Function CloseHandle:Int(handle:Int)
    Function GetLastError:Int()
End Extern

' Function to enumerate all running processes
Function EnumerateProcesses()
    ' Create a snapshot of all processes
    Local hSnapshot:Int = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    If hSnapshot = 0
        Print "Failed to create snapshot! Error code: " + GetLastError()
        Return
    End If

    ' Initialize PROCESSENTRY32
    Local pe32:PROCESSENTRY32 = New PROCESSENTRY32
    pe32.dwSize = SizeOf(PROCESSENTRY32)

    ' Get the first process
    If Process32First(hSnapshot, pe32) = 0
        Print "Failed to retrieve the first process! Error code: " + GetLastError()
        CloseHandle(hSnapshot)
        Return
    End If

    ' Loop through all processes
    Print "Running Processes:"
    Repeat
        ' Extract process information
        Local processID:Int = pe32.th32ProcessID
        Local processName:String = String.FromCString(Varptr pe32.szExeFile00)

        ' Display process details
        Print "Process Name: " + processName + " | PID: " + processID

        ' Break the loop if Process32Next fails (no more processes)
        If Process32Next(hSnapshot, pe32) = 0 Then Exit
    Forever

    ' Close the snapshot handle
    CloseHandle(hSnapshot)

    Print "Enumeration complete."

End Function

' Main program
Print "Enumerating all processes:"
EnumerateProcesses()

64 bit (but also on 32 is doing the same)
 

fielder

are you able to compile this without erros? is my Blitzmax NG installation the issue?

ok... understand.. this code is for standard Bmax and not NG.

fielder

Quote from: GW on December 29, 2024, 20:09:11As an alternate method. You can also do something like this.  Then parse the result.
Import pub.freeprocess

Local Proc:TProcess = CreateProcess("tasklist")
While  Proc.Status()
    If Proc.pipe.ReadAvail() > 0 Then 
        StandardIOStream.WriteString(Proc.pipe.ReadString(Proc.pipe.ReadAvail()).Trim())
        StandardIOStream.Flush()
    End If
   
    If Proc.err.ReadAvail() > 0 Then
        StandardIOStream.WriteString(Proc.err.ReadString(Proc.err.ReadAvail()))
        StandardIOStream.Flush()
    EndIf
Wend

there is a way to have the parent ID of a process? (who launched a process)