Detect if process is running


👉 BlitzCoder will be building a new platform and other plans to preserve and continue the Blitz legacy.

To be able to achieve this goal, we need your support by becoming a Patreon Paid Member 👈

 

Tweet
eugesippe

Hi,

I use BlitzPlus and i need detect if process still running on windows.
For exemple i can detect if "notepad.exe" still running or not

I seems there is a function "ProcessExists ( "process" )" but how to use it in Blitzplus ?

Thanks for your help !

BlitzCoder commented:

I seems there is a function "ProcessExists ( "process" )" but how to use it in Blitzplus ?

The only process I see in the docs is CreateProcess, not sure about and curious where did you find that specific command though..

https://github.com/blitz-research/blitzplus/tree/master/_release/help/commands/2d_commands

perhaps it is an extension or userlib.

RemiD commented:

take a look at this code :
https://archive.blitzcoder.org/forums/code-misc/3212.html

this will not directly help with your problem, but this is an example on how to create a .decls file (that you put in the blitz3d/userlibs/ folder) to be able to call functions from Windows OS.

then you will have to search on the web to know which Windows functions to call and which Windows dll to use (in the decls file).

eugesippe commented:

Hi,

I,ve just found how to do that.
FindWindow search if Window is open using Window Title:
Handle = 0 : Window not open
Handle=other: Window is open

Check this example, i use "calc" to try (Title of window in French is "calculatrice")

; FindWindow% (lpClassName%, lpWindowName$)

title$ = "calculatrice"
AppTitle title
Print "handle of: " + title + " = " + findwindow(title$)
WaitKey
End

Function findwindow(name$)

hwnd% = api_FindWindow% (0,name$)
Return hwnd

End Function

"FindWindow" must be in "user32.decls" like that:

FindWindow% (lpClassName%, lpWindowName$) : "FindWindowA"

druggedbunny983 commented:

I realise the thread is done now, but this is another way to check running processes; needs kernel32 userlib in place with commands listed here. See demo at bottom of file.

The FindProcess () function also demonstrates how to get a current list of processes and iterate through them.


; -----------------------------------------------------------------------------
; ADD TO kernel32.decls IN USERLIBS FOLDER!
; -----------------------------------------------------------------------------

; .lib "kernel32.dll"

; CreateToolhelp32Snapshot% (flags, th32processid)
; Process32First% (snapshot, entry*)
; Process32Next% (snapshot, entry*)
; Module32First% (snapshot, entry*)
; Module32Next% (snapshot, entry*)
; Thread32First% (snapshot, entry*)
; Thread32Next% (snapshot, entry*)
; Heap32First% (snapshot, entry*, th32heapid)
; Heap32Next% (entry*)
; Heap32ListFirst% (snapshot, entry*)
; Heap32ListNext% (snapshot, entry*)
; Toolhelp32ReadProcessMemory% (th32processid, baseaddress, buffer*, ReadBytes, bytesread)
; CloseHandle% (Object)

; -----------------------------------------------------------------------------
; PROCESSENTRY32 structure hack...
; -----------------------------------------------------------------------------

Const SizeOf_PE32 = 296

Type PE32

    Field bank ; contains:

;    dwSize.l
;    cntUsage.l
;    th32ProcessID.l
;    th32DefaultHeapID.l
;    th32ModuleID.l
;    cntThreads.l
;    th32ParentProcessID.l
;    pcPriClassBase.l
;    dwFlags.l
;    szExeFile.b [#MAX_PATH]

End Type

; -----------------------------------------------------------------------------

; -----------------------------------------------------------------------------
; Create a new process list entry...
; -----------------------------------------------------------------------------

Function CreatePE32.PE32 ()
    p.PE32 = New PE32
    p\bank = CreateBank (SizeOf_PE32)
    If p\bank
        PokeInt p\bank, 0, SizeOf_PE32
    Else
        Delete p
        Return Null
    EndIf
    Return p
End Function

; -----------------------------------------------------------------------------
; Free process list entry...
; -----------------------------------------------------------------------------

Function FreePE32 (p.PE32)
    If p\bank
        FreeBank p\bank
    EndIf
    Delete p
End Function

; -----------------------------------------------------------------------------
; Redundant info...
; -----------------------------------------------------------------------------

Function PrintProc (bank)
    Print ""
    Print "Name    : " + ProcessName$ (bank)
    Print "Usage   : " + PeekInt (bank, 4)
    Print "Proc ID : " + PeekInt (bank, 8)
    Print "Heap ID : " + PeekInt (bank, 12)
    Print "Mod  ID : " + PeekInt (bank, 16)
    Print "Threads : " + PeekInt (bank, 20)
    Print "Parent  : " + PeekInt (bank, 24)
    Print "ClasBas : " + PeekInt (bank, 28)
    Print "Flags   : " + PeekInt (bank, 32)
End Function

; -----------------------------------------------------------------------------
; Eeuurrggghhhh... leech process name from bank...
; -----------------------------------------------------------------------------

Function ProcessName$ (bank)
    For s = 36 To BankSize (bank) - 1
        byte = PeekByte (bank, s)
        If byte
            result$ = result$ + Chr (byte)
        Else
            Exit
        EndIf
    Next
    Return result$
End Function

Global PROC_COUNT

; -----------------------------------------------------------------------------
; Constants required by process functions, etc...
; -----------------------------------------------------------------------------

Const TH32CS_SNAPHEAPLIST = $1
Const TH32CS_SNAPPROCESS = $2
Const TH32CS_SNAPTHREAD = $4
Const TH32CS_SNAPMODULE = $8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = $80000000
Const INVALID_HANDLE_VALUE = -1
Const MAX_PATH = 260

; -----------------------------------------------------------------------------
; Take snapshot of running processes...
; -----------------------------------------------------------------------------

Function CreateSnapshot ()
    PROC_COUNT = 0
    Return CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0)
End Function

; -----------------------------------------------------------------------------
; Free list of processes (created via CreateProcessList and GetProcesses)...
; -----------------------------------------------------------------------------

Function FreeProcessList (snap)
    For p.PE32 = Each PE32
        FreePE32 (p)
    Next
    CloseHandle (snap)
    PROC_COUNT = 0
End Function

; -----------------------------------------------------------------------------
; Free list of processes (created via CreateProcessList and GetProcesses)...
; -----------------------------------------------------------------------------

Function GetProcesses (snap)

    PROC_COUNT = 0

    ; Check snapshot is valid...

    If snap <> INVALID_HANDLE_VALUE

        ; Hack up a PE32 (PROCESSENTRY32) structure...

        p.PE32 = CreatePE32 ()

        ; Find the first process, stick info into PE32 bank...

        If Process32First (snap, p\bank)

            ; Increase global process counter...

            PROC_COUNT = PROC_COUNT + 1

            Repeat

                ; Create a new PE32 structure for every following process...

                p.PE32 = CreatePE32 ()

                ; Find the next process, stick into PE32 bank...

                nextproc = Process32Next (snap, p\bank)

                ; Got one? Increase process count. If not, free the last PE32 structure...

                If nextproc         
                    PROC_COUNT = PROC_COUNT + 1
                Else
                    FreePE32 (p)
                EndIf

            ; OK, no more processes...

            Until nextproc = 0

        Else

            ; No first process found, so delete the PE32 structure it used...

            FreePE32 (p)
            Return False

        EndIf

        Return True

    Else

        Return False

    EndIf

End Function

Function FindProcess (process$)

    found_process = False

    ; Follow steps 1-3 each time you need to check processes
    ; to make sure running process list is current...

    ; 1) Get snapshot of running processes...

    snap = CreateSnapshot ()

    ; Valid snapshot?

    If GetProcesses (snap)

        ; 2) Go through list of processes...

        For p.PE32 = Each PE32

            ; Process name to work with:

            proc$ = ProcessName$ (p\bank)

            ; Do what you want with proc$!

            ; In this case, note as found and exit loop:

            If Lower (proc$) = Lower (process$)
                found_process = True
                Exit
            EndIf

        Next

        ; 3) Free the list!

        FreeProcessList (snap)

    Else
        Print "Failed to create process list!"
    EndIf           

    Return found_process

End Function

; -----------------------------------------------------------------------------
; Demo
; -----------------------------------------------------------------------------

proc$ = "notepad.exe"

If FindProcess (proc$)
    Print proc$ + " is running!" Else Print proc$ + " is not running!"
EndIf

Print "Any key to exit..."
WaitKey
End
eugesippe commented:

Thanks druggedbunny983, i will try that soon !

Reply To Topic (minimum 10 characters)

Please log in to reply