Kill Running Task

Just starting out? Need help? Post your questions and find answers here.
SoulReaper
Enthusiast
Enthusiast
Posts: 372
Joined: Sun Apr 03, 2005 2:14 am
Location: England

Kill Running Task

Post by SoulReaper »

Hello :)

Is it possible to Kill/End a Process that is running in the Task Manager
from Pure Basic...

Regards
Kevin :wink:
SunSatION
User
User
Posts: 85
Joined: Tue Jun 21, 2005 7:26 pm
Location: Malta

Post by SunSatION »

User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Or simple use "KillPID" from PBOSL Processlib
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Post by eJan »

This is one i'm using:

Code: Select all

; http://www.purearea.net/pb/CodeArchiv/Windows_System/Tasks&Processes/KillProcess.pb

; English forum: http://www.purebasic.fr/english/viewtopic.php?t=8086&start=15
; Author: Hi-Toro
; Date : 30. November 2003
; OS : Windows
; Demo : No

; ------------------------ Get PID
Structure PROCESSENTRY32s
  dwsize.l
  cntusage.l
  th32ProcessID.l
  th32DefaultHeapID.l
  th32ModuleID.l
  cntThreads.l
  th32ParentProcessID.l
  pcPriClassBase.l
  dwFlags.l
  szExeFile.s{1024}
EndStructure
#TH32CS_SNAPPROCESS = $2

Procedure.l FindPid(s.s)
  Process.PROCESSENTRY32s
  ProcSnap.l
  ProcSnap = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
  If ProcSnap <> 0
    Process\dwsize=SizeOf(Process)
    Process32First_(ProcSnap, Process)
    While Process32Next_(ProcSnap, Process) > 0
      If Process\szExeFile = s
        ProcedureReturn Process\th32ProcessID
        Break
      EndIf
    Wend
  EndIf
EndProcedure
; --------------------- Get PID end

; Take a look at ListTaskbarWindows.pb before...
; ... and if a program refuses to close like that, you could instead 'force' it closed
; via this, passing the 'pid' variable from the above code : 

#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...

KillProcess(FindPid("explorer.exe"))
SoulReaper
Enthusiast
Enthusiast
Posts: 372
Joined: Sun Apr 03, 2005 2:14 am
Location: England

Post by SoulReaper »

Thankyou to everyone for the information. :wink:

Regards
Kevin :)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

If you know the window handle, you can just use this:

Code: Select all

Procedure KillProcess(hwnd)
  GetWindowThreadProcessId_(hWnd,@ProcessId)
  hProcess=OpenProcess_(#PROCESS_ALL_ACCESS,#True,ProcessId)
  If hProcess : TerminateProcess_(hProcess,0) : CloseHandle_(hProcess) : EndIf
EndProcedure
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
DTecMeister
User
User
Posts: 42
Joined: Sat Mar 04, 2006 5:19 pm
Location: Rome, NY

Kill Running Task

Post by DTecMeister »

Or just call ntsd -p [pid] -c "q" from PB.
Where are the masses?
Post Reply