In Windows, it is very troublesome to kill some processes and use the task manager for complicated operations. It is also troublesome to enter taskkill/f/im xxx in cmd with many characters.
I wrote a python tool to kill the process. Please use the latest python3.2.2,
Python3.2 has a bug. If you use 3.2, go
Cmd = input ('> ')
Add a row below
Cmd = cmd. Replace ('\ R ')
Usage: run the script
> Process name (only a few keywords are allowed) or PID Number
> Look refreshes the list and displays
The script has two files.
------------------------- Winproc. py -------------------------------
Import ctypes
Import sys
Th32cs_snapprocess = 0x00000002
Class processentry32 (ctypes. structure ):
_ Fields _ = [("dwsize", ctypes. c_ulong ),
("Cntusage", ctypes. c_ulong ),
("Th32processid", ctypes. c_ulong ),
("Th32defaultheapid", ctypes. c_ulong ),
("Th32moduleid", ctypes. c_ulong ),
("Cntthreads", ctypes. c_ulong ),
("Th32parentprocessid", ctypes. c_ulong ),
("Pcpriclassbase", ctypes. c_ulong ),
("Dwflags", ctypes. c_ulong ),
("Szexefile", ctypes. c_char * 260)]
Def getproclist ():
Createconlhelp32snapshot = ctypes. Windll. kernel32.createconlhelp32snapshot
Process32first = ctypes. Windll. kernel32.process32first
Process32next = ctypes. Windll. kernel32.process32next
Closehandle = ctypes. Windll. kernel32.closehandle
Hprocesssnap = createconlhelp32snapshot (th32cs_snapprocess, 0)
Pe32 = processentry32 ()
Pe32.dwsize = ctypes. sizeof (processentry32)
If process32first (hprocesssnap, ctypes. byref (pe32) = false:
Print ("failed getting first process.", file = SYS. stderr)
Return
While true:
Yield pe32
If process32next (hprocesssnap, ctypes. byref (pe32) = false:
Break
Closehandle (hprocesssnap)
Def getchildpid (PID ):
Proclist = getproclist ()
For proc in proclist:
If Proc. th32parentprocessid = PID:
Yield Proc. th32processid
Def killpid (PID ):
Childlist = getchildpid (PID)
For childpid in childlist:
Killpid (childpid)
Handle = ctypes. Windll. kernel32.openprocess (1, false, pid)
Ctypes. Windll. kernel32.terminateprocess (handle, 0)
If _ name _ = '_ main __':
ARGs = SYS. argv
If Len (ARGs)> 1:
PID = int (ARGs [1])
Killpid (PID)
Else:
Proclist = getproclist ()
For proc in proclist:
Print (STR (Proc. szexefile) + ''+ STR (Proc. th32parentprocessid) +'' + STR (Proc. th32processid ))
----------------------------- Main program killtask. py ------------------------------------------------
Import getopt
Import winproc # winproc is the winproc. py under the same level directory
Proclist2 = []
Def lookproc ():
Proclist = winproc. getproclist ()
For proc in proclist:
# Print (Proc. szexefile. Decode ('gbk') + ''+ STR (Proc. th32parentprocessid) +'' + STR (Proc. th32processid ))
Print (Proc. szexefile. Decode ('gbk') + ''+ STR (Proc. th32processid ))
Proclist2.append ([Proc. szexefile. Decode ('gbk'), Proc. th32processid])
If _ name _ = '_ main __':
Lookproc ()
While true:
Cmd = input ('> ')
If cmd = '':
Continue
If cmd = 'exit ':
Break
Elif cmd = 'look ':
Print ('view Process ')
Proclist2 = []
Lookproc ()
Elif cmd. isnumeric ():
PID = int (CMD)
Winproc. killpid (INT (CMD ))
PID = 0
For proc in proclist2:
If pid = proc [1]:
Print ('process terminated: "'+ proc [0] +'" \ t' + STR (PID ))
If pid = 0:
Print ('the process with PID = '+ cmd +' not found ')
Else:
PID = 0
For proc in proclist2:
# Print ('location', Proc [0], type (Proc [0])
If proc [0]. Find (CMD )! =-1:
PID = proc [1]
Winproc. killpid (PID)
Print ('process terminated: "'+ proc [0] +'" \ t' + STR (PID ))
If pid = 0:
Print ('not found "' + cmd + '"')
# ARGs = SYS. argv
# If Len (ARGs)> 1:
# Pid = int (ARGs [1])
# Killpid (PID)
# Else:
# Proclist = getproclist ()
# For proc in proclist:
# Print (STR (Proc. szexefile) + ''+ STR (Proc. th32parentprocessid) +'' + STR (Proc. th32processid ))
# Print (STR (Proc.
----------------------------------------------------------------------------------
Put the two files in one directory and double-click the main program killtask. py.