Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

killProcName.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Kills a process by process name
  2. #
  3. # Uses the Performance Data Helper to locate the PID, then kills it.
  4. # Will only kill the process if there is only one process of that name
  5. # (eg, attempting to kill "Python.exe" will only work if there is only
  6. # one Python.exe running. (Note that the current process does not
  7. # count - ie, if Python.exe is hosting this script, you can still kill
  8. # another Python.exe (as long as there is only one other Python.exe)
  9. # Really just a demo for the win32pdh(util) module, which allows you
  10. # to get all sorts of information about a running process and many
  11. # other aspects of your system.
  12. import sys
  13. import win32api
  14. import win32con
  15. import win32pdhutil
  16. def killProcName(procname):
  17. # Change suggested by Dan Knierim, who found that this performed a
  18. # "refresh", allowing us to kill processes created since this was run
  19. # for the first time.
  20. try:
  21. win32pdhutil.GetPerformanceAttributes("Process", "ID Process", procname)
  22. except:
  23. pass
  24. pids = win32pdhutil.FindPerformanceAttributesByName(procname)
  25. # If _my_ pid in there, remove it!
  26. try:
  27. pids.remove(win32api.GetCurrentProcessId())
  28. except ValueError:
  29. pass
  30. if len(pids) == 0:
  31. result = "Can't find %s" % procname
  32. elif len(pids) > 1:
  33. result = "Found too many %s's - pids=`%s`" % (procname, pids)
  34. else:
  35. handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pids[0])
  36. win32api.TerminateProcess(handle, 0)
  37. win32api.CloseHandle(handle)
  38. result = ""
  39. return result
  40. if __name__ == "__main__":
  41. if len(sys.argv) > 1:
  42. for procname in sys.argv[1:]:
  43. result = killProcName(procname)
  44. if result:
  45. print(result)
  46. print("Dumping all processes...")
  47. win32pdhutil.ShowAllProcesses()
  48. else:
  49. print("Killed %s" % procname)
  50. else:
  51. print("Usage: killProcName.py procname ...")