python之win32下,枚举进程,线程和线程对应的窗口的利用ctypes实现
2013-01-23 22:48
866 查看
import ctypes import win32com.client import win32gui, win32api, pywintypes WMI = win32com.client.GetObject('winmgmts:') kernel32 = ctypes.windll.kernel32 TH32CS_SNAPTHREAD = 0x00000004 class THREADENTRY32(ctypes.Structure): _fields_ = [ ("dwSize", ctypes.c_ulong), ("cntUsage", ctypes.c_ulong), ("th32ThreadID", ctypes.c_ulong), ("th32OwnerProcessID", ctypes.c_ulong), ("tpBasePri", ctypes.c_long), ("tpDeltaPri", ctypes.c_long), ("dwFlags", ctypes.c_ulong)] def EnumerateThreads(processId): threadEntry = THREADENTRY32() threadList = [] threadSnap = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, processId) if threadSnap is not None: threadEntry.dwSize = ctypes.sizeof(threadEntry) success = kernel32.Thread32First(threadSnap, ctypes.byref(threadEntry)) if not success: print 'Failed getting first process.' while success: if threadEntry.th32OwnerProcessID == processId: threadList.append(threadEntry.th32ThreadID) success = kernel32.Thread32Next(threadSnap, ctypes.byref(threadEntry)) kernel32.CloseHandle(threadSnap) return threadList def EnumerateProcesses(processName): processList = WMI.ExecQuery("SELECT * FROM Win32_Process where name = '%s'"%processName) return processList def SearchWindowHandleIdIdByCaption(processName ,caption): processList = EnumerateProcesses(processName) for process in processList: for thread in EnumerateThreads(long(process.Handle)): windows = [] win32gui.EnumThreadWindows(thread, lambda hwnd, resultList: resultList.append(hwnd), windows) for windowIdi in windows: text = win32gui.GetWindowText(windowIdi) text = text.__str__() caption = caption.__str__() if caption in text: return [windowIdi, process.Handle] return [None, None]
相关文章推荐
- 突破python缺陷,实现几种自定义线程池 以及进程、线程、协程的介绍
- Python下的多进程实现——利用multiprocessing库实现并发爬虫
- 如何用 Win32 APIs 枚举应用程序窗口和进程
- 使用Python WSGI编写Web服务器前端,并利用Boost.Python在同一进程内连接到C++服务后端实现
- 用Win32 API枚举应用程序窗口和进程
- MFC(7) 利用CWinThread实现跨线程父子MFC窗口
- 利用主线程与子线程间的消息通讯,实现任务处理队列.子线程中创建不会阻塞执行的窗口
- Python 进程线程之间通信方式(2,共享数据实现进程之间通信)
- 利用Hyperic调用Python实现进程守护
- Python 进程线程之间通信方式(2.1,共享数据实现进程之间通信)
- 利用CWinThread实现跨线程父子MFC窗口
- python 线程简单使用----1利用threading 实现多线程
- python实现守护进程、守护线程、守护非守护并行
- 利用win32 API GetWindow 枚举窗口
- python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过autopy实现右键菜单和另存为操作
- 如何用 Win32 APIs 枚举应用程序窗口和进程
- 突破python缺陷,实现几种自定义线程池 以及进程、线程、协程的介绍
- 线程共享的环境包括:进程代码段、进程的公有数据(利用这些共享的数据,线程很容易的实现相互之间的通讯)、进程打开的文件描述符、信号的处理器、进程的当前目录和进程用户ID与进程组ID。 进程拥有这
- 突破python缺陷,实现几种自定义线程池 以及进程、线程、协程的介绍
- 14-08-07 关于程序、进程、线程,以及python中实现多线程的办法