您正在将进程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
@H_419_15@这是列出可见窗口的功能。它使用一个闭包的回调,
result
而不是使用可选lParam
参数。后者将需要转换参数。使用闭包比较简单。def list_windows(): '''Return a sorted list of visible windows.''' result = [] @WNDENUMPROC def enum_proc(hWnd, lParam): if user32.IsWindowVisible(hWnd): pid = wintypes.DWORD() tid = user32.GetWindowThreadProcessId( hWnd, ctypes.byref(pid)) length = user32.GetWindowTextLengthW(hWnd) + 1 title = ctypes.create_unicode_buffer(length) user32.GetWindowTextW(hWnd, title, length) result.append(WindowInfo(pid.value, title.value)) return True user32.EnumWindows(enum_proc, 0) return sorted(result)
@H_419_15@为了完整起见,这是一个列出所有进程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
@H_419_15@例如:
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
def list_windows(): '''Return a sorted list of visible windows.''' result = [] @WNDENUMPROC def enum_proc(hWnd, lParam): if user32.IsWindowVisible(hWnd): pid = wintypes.DWORD() tid = user32.GetWindowThreadProcessId( hWnd, ctypes.byref(pid)) length = user32.GetWindowTextLengthW(hWnd) + 1 title = ctypes.create_unicode_buffer(length) user32.GetWindowTextW(hWnd, title, length) result.append(WindowInfo(pid.value, title.value)) return True user32.EnumWindows(enum_proc, 0) return sorted(result)
@H_419_15@为了完整起见,这是一个列出所有进程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
@H_419_15@例如:
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
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
@H_419_15@例如:
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
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)
这是列出可见窗口的功能。它使用一个闭包的回调,result
而不是使用可选lParam
参数。后者将需要转换参数。使用闭包比较简单。
为了完整起见,这是一个列出所有进程ID的函数。这包括属于其他Windows会话的进程(例如,会话0中的服务)。
例如:
MSDN链接:
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的ctypes)。
这是列出可见窗口的功能。它使用一个闭包的回调,result
而不是使用可选lParam
参数。后者将需要转换参数。使用闭包比较简单。
def list_windows(): '''Return a sorted list of visible windows.''' result = [] @WNDENUMPROC def enum_proc(hWnd, lParam): if user32.IsWindowVisible(hWnd): pid = wintypes.DWORD() tid = user32.GetWindowThreadProcessId( hWnd, ctypes.byref(pid)) length = user32.GetWindowTextLengthW(hWnd) + 1 title = ctypes.create_unicode_buffer(length) user32.GetWindowTextW(hWnd, title, length) result.append(WindowInfo(pid.value, title.value)) return True user32.EnumWindows(enum_proc, 0) return sorted(result)
@H_419_15@为了完整起见,这是一个列出所有进程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
@H_419_15@例如:
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
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
@H_419_15@例如:
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
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)
为了完整起见,这是一个列出所有进程ID的函数。这包括属于其他Windows会话的进程(例如,会话0中的服务)。
例如:
MSDN链接:
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的ctypes)。
为了完整起见,这是一个列出所有进程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
@H_419_15@例如:
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
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)
例如:
MSDN链接:
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的ctypes)。
例如:
if __name__ == '__main__': print('Process IDs:') print(*list_pids(), sep='\n') print('\nWindows:') print(*list_windows(), sep='\n')
@H_419_15@MSDN链接:
EnumWindows
IsWindowVisible
GetWindowThreadProcessId
GetWindowTextLength
GetWindowText
EnumProcesses
解决方法
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的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)
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)
MSDN链接:
我试图通过带有长对象的句柄获取Windows窗口标题名称和pid。我的代码有效,但是有问题。当我应该获得10个或更多时,我只有4个窗口标题。谁能帮忙告诉我如何解决此代码?我认为问题出在我如何转换长对象(我不太了解它们以及一般的ctypes)。
MSDN链接: