zoukankan      html  css  js  c++  java
  • python之win32下,枚举进程,线程和线程对应的窗口的利用ctypes实现

    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]
                        
  • 相关阅读:
    jquery 的 outerWidth() 、width() 、innerWidth()
    图片自动切换 避免 鼠标快速滑过
    Ajax中日历控件的使用
    asp.net如何读取xml文件中的数据
    ASP.NET使用AspNetPager实现简单的分页功能
    XmlDataDocument与DataSet相互转化
    C#中如何过滤掉多余的html代码
    asp.net的几种经典的页面传值方法
    ASP.Net分页方法详解
    ASP.Net中省市级联有关
  • 原文地址:https://www.cnblogs.com/ankier/p/2874027.html
Copyright © 2011-2022 走看看