如何通过名称查找进程并使用ctypes杀死进程?

如何通过名称查找进程并使用ctypes杀死进程?,第1张

如何通过名称查找进程并使用ctypes杀死进程?

尝试这个:

import sys, os.path, ctypes, ctypes.wintypesPsapi = ctypes.WinDLL('Psapi.dll')EnumProcesses = Psapi.EnumProcessesEnumProcesses.restype = ctypes.wintypes.BOOLGetProcessImageFileName = Psapi.GetProcessImageFileNameAGetProcessImageFileName.restype = ctypes.wintypes.DWORDKernel32 = ctypes.WinDLL('kernel32.dll')OpenProcess = Kernel32.OpenProcessOpenProcess.restype = ctypes.wintypes.HANDLETerminateProcess = Kernel32.TerminateProcessTerminateProcess.restype = ctypes.wintypes.BOOLCloseHandle = Kernel32.CloseHandleMAX_PATH = 260PROCESS_TERMINATE = 0x0001PROCESS_QUERY_INFORMATION = 0x0400count = 32while True:    ProcessIds = (ctypes.wintypes.DWORD*count)()    cb = ctypes.sizeof(ProcessIds)    BytesReturned = ctypes.wintypes.DWORd()    if EnumProcesses(ctypes.byref(ProcessIds), cb, ctypes.byref(BytesReturned)):        if BytesReturned.value<cb: break        else: count *= 2    else:        sys.exit("Call to EnumProcesses failed")for index in range(BytesReturned.value / ctypes.sizeof(ctypes.wintypes.DWORD)):    ProcessId = ProcessIds[index]    hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, False, ProcessId)    if hProcess:        ImageFileName = (ctypes.c_char*MAX_PATH)()        if GetProcessImageFileName(hProcess, ImageFileName, MAX_PATH)>0: filename = os.path.basename(ImageFileName.value) if filename == 'notepad.exe':     TerminateProcess(hProcess, 1)        CloseHandle(hProcess)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5654840.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存