python文件名排序按windowsp_如何在python中使用ctypes获取Windows窗口名

将进程ID传递给接受窗口句柄的函数。您要做的是枚举顶级窗口的句柄,然后将每个窗口映射到一个进程ID

首先让我们定义ctypes函数原型,以便对函数参数进行正确的类型检查。另外,使用use_last_error=True通过ctypes.get_last_error获得最安全的错误处理。很多Windows函数返回0表示错误,因此在这种情况下使用一个errcheck函数非常方便,例如check_zero。在from __future__ import print_function

import ctypes

from ctypes import wintypes

from collections import namedtuple

user32 = ctypes.WinDLL('user32', use_last_error=True)

def check_zero(result, func, args):

if not result:

err = ctypes.get_last_error()

if err:

raise ctypes.WinError(err)

return args

if not hasattr(wintypes, 'LPDWORD'): # PY2

wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD)

WindowInfo = namedtuple('WindowInfo', 'pid title')

WNDENUMPROC = ctypes.WINFUNCTYPE(

wintypes.BOOL,

wintypes.HWND, # _In_ hWnd

wintypes.LPARAM,) # _In_ lParam

user32.EnumWindows.errcheck = check_zero

user32.EnumWindows.argtypes = (

WNDENUMPROC, # _In_ lpEnumFunc

wintypes.LPARAM,) # _In_ lParam

user32.IsWindowVisible.argtypes = (

wintypes.HWND,) # _In_ hWnd

user32.GetWindowThreadProcessId.restype = wintypes.DWORD

user32.GetWindowThreadProcessId.argtypes = (

wintypes.HWND, # _In_ hWnd

wintypes.LPDWORD,) # _Out_opt_ lpdwProcessId

user32.GetWindowTextLengthW.errcheck = check_zero

user32.GetWindowTextLengthW.argtypes = (

wintypes.HWND,) # _In_ hWnd

user32.GetWindowTextW.errcheck = check_zero

user32.GetWindowTextW.argtypes = (

wintypes.HWND, # _In_ hWnd

wintypes.LPWSTR, # _Out_ lpString

ctypes.c_int,) # _In_ nMaxCount

下面是一个列出可见窗口的函数。它使用的回调是result上的闭包,而不是使用可选的lParam参数。后者需要进行论证。使用闭包更简单。在

^{pr2}$

为了完整起见,这里有一个列出所有进程id的函数。这包括属于其他Windows会话的进程(例如会话0中的服务)。在psapi = ctypes.WinDLL('psapi', use_last_error=True)

psapi.EnumProcesses.errcheck = check_zero

psapi.EnumProcesses.argtypes = (

wintypes.LPDWORD, # _Out_ pProcessIds

wintypes.DWORD, # _In_ cb

wintypes.LPDWORD,) # _Out_ pBytesReturned

def list_pids():

'''Return sorted list of process IDs.'''

length = 4096

PID_SIZE = ctypes.sizeof(wintypes.DWORD)

while True:

pids = (wintypes.DWORD * length)()

cb = ctypes.sizeof(pids)

cbret = wintypes.DWORD()

psapi.EnumProcesses(pids, cb, ctypes.byref(cbret))

if cbret.value < cb:

length = cbret.value // PID_SIZE

return sorted(pids[:length])

length *= 2

例如:if __name__ == '__main__':

print('Process IDs:')

print(*list_pids(), sep='\n')

print('\nWindows:')

print(*list_windows(), sep='\n')

MSDN链接:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值