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

Strict

Import brl.System

Extern "Win32"
    Function EnumProcesses(pProcessIds:Int Ptr, cb:Int, pBytesReturned:Int Ptr)
End Extern

Function ListAllProcesses()
    Local processIds:Int[1024] ' Allocate space for up to 1024 process IDs
    Local bytesReturned:Int
   
    ' Call EnumProcesses
    If EnumProcesses(Int ptr(processIds), SizeOf(processIds), VarPtr(bytesReturned)) Then
        Local processCount:Int = bytesReturned / 4 ' Each process ID is 4 bytes
        Print "Number of Processes: " + processCount
        For Local i:Int = 0 Until processCount
            Print "Process ID: " + processIds[i]
        Next
    Else
        Print "Failed to enumerate processes."
    End If
End Function

ListAllProcesses()

I get Error from compiler that say  
Quotewin32.x86.o:(code+0x101): undefined reference to `EnumProcesses@12'

i need to include some Psapi.dll or something 
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]


Hardcoal

i get error
QuoteBlitzMax/bin/ld.exe: cannot find -lpsapi
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Midimaster

...back from North Pole.

Henri

Hi,

try to rename winAPI function to K32EnumProcesses .

-Henri
- Got 01100011 problems, but the bit ain't 00000001

dawlane

Quote from: Hardcoal on December 29, 2024, 15:32:58i get error
QuoteBlitzMax/bin/ld.exe: cannot find -lpsapi

Strange. 
Works as expected on my BlitzMax NG setup.

GW

As 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



Hardcoal

Gw Method works, And also other method I use also worked, but the weird thing is that I made this following Exe

Strict

Global NetHost:TGNetHost = CreateGNetHost()

Global GNObject:TGNetObject

Repeat

If GNetListen(NetHost, 80) > 0 Then Print "Working"
Delay (5)

Until KeyDown(KEY_ESCAPE)

And when i run it, it will appear in the task manager , but it wont appear on the lists when I read processes lists.. not even on GW code version
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Hardcoal

this is my last attempt, but it results in zero PID detection

and with Error at end 

it does show list of processes but does not list the PID

Strict
Import brl.standardio

' Define required structures and constants
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
    Field szExeFile:Byte[260]
End Type

Const TH32CS_SNAPPROCESS = $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

' Helper function to convert null-terminated byte array to string
Function ByteArrayToString:String(arr:Byte[], maxLen:Int)
    Local str:String = ""
    For Local i:Int = 0 Until maxLen
        If arr[i] = 0 Then Exit ' Null terminator
        str :+ Chr(arr[i])
    Next
    Return str
End Function

' 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, Varptr 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 = ByteArrayToString(pe32.szExeFile, 260)

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

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

    ' Close the snapshot handle
    CloseHandle(hSnapshot)

    Print "Enumeration complete."

End Function

' Main program
Print "Enumerating all processes:"
EnumerateProcesses()
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Baggey

Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

Baggey

@Hardcoal

Im having a look at your code ???

Where did you learn to code in BlitzmaxNG like that!?  It's certainly wasnt from the tutorials! :-X

I can only say it will be something you've not defined with your Superstrict / strict ? That's my guess at the mo :)
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

Hardcoal

Lol firstly im still stuck with blitzmax vanilla..
everything works.. besides that i cant see processes without window..

i will take a look and maybe try to transfer it to blitzmaxng..

the only thing that stop me from using NG is that each time i want to switch . i need to set up environment on my editor to fit the language.


This topic not even interesting to me, but since i made this process terminator i did.. i cant just leave it without recognizing all processes ..
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

col

Hey,
Happy New Year and all that :)

You were almost there. The PROCESSENTRY32 expects 260 bytes 'inline' as part of the type for the exe name, and creating a bmax array is not sufficient in these cases so you need to pad these out using Ints. Also you were taking a pointer to the pe instance via VarPtr which creates a pointer to a pointer (in this example) whereas blitzmax will automatically convert an instance of an 'Object' to 'a pointer to its data members' when you pass an instance of the Object to a function whose parameter is a Byte Ptr.

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()



https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Hardcoal

Great. it Works Col! 
I dont understand much about pointer I must say..
But since I released a TaskTerminator App, it would be inappropriate not to recognize all Tasks, so I had to address that.

Happy New year to you too And All those who helped 
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]