how to kill a process?

Just starting out? Need help? Post your questions and find answers here.
muab256
User
User
Posts: 43
Joined: Mon Apr 28, 2003 4:57 pm

how to kill a process?

Post by muab256 »

heyas =)

how could i "kill" a running process (not my own app, but some
other app running)?

thx

muab
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Open the Windows Calculator then run this:

Code: Select all

hWnd = FindWindow_(0, "Calculator")
Debug hWnd
If hWnd <> 0
    Debug "Calculator found"
    Debug "Closing..."
    SendMessage_(hWnd, #WM_CLOSE, 0, 0)
    Debug "Done"
EndIf
Is this what you meant?
--Kale

Image
muab256
User
User
Posts: 43
Joined: Mon Apr 28, 2003 4:57 pm

Post by muab256 »

Kale wrote:Is this what you meant?
nearly =)

i need to kill "window-less" applications too...
so i need to kill a process by its "real name" (calculator.exe)

muab
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

Terminate process

Post by Hi-Toro »

This terminates a process, but you need its 'pid' (process ID). This can usually be retrieved from the Task Manager, or you can iterate through the process list using Process32First/Next, etc.

See the thread below for an example of listing processes and their IDs:
viewtopic.php?p=37534#37534

Code: Select all


#PROCESS_TERMINATE = $1
#PROCESS_CREATE_THREAD = $2
#PROCESS_VM_OPERATION = $8
#PROCESS_VM_READ = $10
#PROCESS_VM_WRITE = $20
#PROCESS_DUP_HANDLE = $40
#PROCESS_CREATE_PROCESS = $80
#PROCESS_SET_QUOTA = $100
#PROCESS_SET_INFORMATION = $200
#PROCESS_QUERY_INFORMATION = $400
#PROCESS_ALL_ACCESS = #STANDARD_RIGHTS_REQUIRED | #SYNCHRONIZE | $FFF

; This appears to be pretty much how Windows kills a program if you 'End Process'
; from the Task Manager. Note that this is 'unfriendly'!

Procedure KillProcess (pid)
    phandle = OpenProcess_ (#PROCESS_TERMINATE, #FALSE, pid)
    If phandle <> #NULL
        If TerminateProcess_ (phandle, 1)
            result = #TRUE
        EndIf
        CloseHandle_ (phandle)
    EndIf
    ProcedureReturn result
EndProcedure

; Enter process ID here! I suggest going to Task Manager,
; making sure PIDs are shown (try View menu -> Select columns if
; they are not listed), then run a program and enter its number here...

Debug KillProcess ( x )

James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Post Reply