我尝试通过长对象的句柄来获取Windows窗口标题名称和pid。我的代码可以工作,但是有些问题。我只得到了4个窗口标题,而实际上应该得到10个或更多。有人能帮忙告诉我如何修复这个代码吗?我认为问题在于我如何转换长对象(我对它们不太了解,以及ctypes通用的情况)。
from __future__ import print_function
from ctypes import *
psapi = windll.psapi
titles = []
# get window title from pid
def gwtfp():
max_array = c_ulong * 4096
pProcessIds = max_array()
pBytesReturned = c_ulong()
psapi.EnumProcesses(byref(pProcessIds),
sizeof(pProcessIds),
byref(pBytesReturned))
# get the number of returned processes
nReturned = pBytesReturned.value/sizeof(c_ulong())
pidProcessArray = [i for i in pProcessIds][:nReturned]
print(pidProcessArray)
#
EnumWindows = windll.user32.EnumWindows
EnumWindowsProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int))
GetWindowText = windll.user32.GetWindowTextW
GetWindowTextLength = windll.user32.GetWindowTextLengthW
IsWindowVisible = windll.user32.IsWindowVisible
for process in pidProcessArray:
#print("Process PID %d" % process)
if IsWindowVisible(process):
length = GetWindowTextLength(process)
buff = create_unicode_buffer(length + 1)
GetWindowText(process, buff, length + 1)
titles.append(buff.value)
gwtfp()
print(titles)