Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pywin32的GetGuitThreadInfo()_Python_Python 3.x_Windows_Ctypes_Pywin32 - Fatal编程技术网

Python 使用pywin32的GetGuitThreadInfo()

Python 使用pywin32的GetGuitThreadInfo(),python,python-3.x,windows,ctypes,pywin32,Python,Python 3.x,Windows,Ctypes,Pywin32,我正在努力跟进,我已经到了应该打电话的地步 GetGUIThreadInfo() 但我在我使用的电脑中找不到 到目前为止,我所做的是 import win32api import win32gui import win32process test1 = win32gui.FindWindowEx(0, 0, 0, "notepad") (test1tid, test1pid) = win32process.GetWindowThreadProcessId(test1) test1hwndFo

我正在努力跟进,我已经到了应该打电话的地步

GetGUIThreadInfo()
但我在我使用的电脑中找不到

到目前为止,我所做的是

import win32api
import win32gui
import win32process

test1 = win32gui.FindWindowEx(0, 0, 0, "notepad")
(test1tid, test1pid) = win32process.GetWindowThreadProcessId(test1)
test1hwndFocus = win32process.GetGUIThreadInfo(test1tid)
但最后一行是完整的,因为我找不到调用函数的正确方法

更新1:

我想我取得了一些进展,但现在我的结构只返回0,当我期望一些hwnd。。。所以可能我的结构没有写入,我想这可能是因为我的结构中的类型,但是我如何找到正确的类型呢

import win32api
import win32gui
import win32process
import ctypes

class RECT(ctypes.Structure):
    _fields_ = [
    ("left", ctypes.c_ulong),
    ("top", ctypes.c_ulong),
    ("right", ctypes.c_ulong),
    ("bottom", ctypes.c_ulong)
    ]


class GUITHREADINFO(ctypes.Structure):
    _fields_ = [
    ("cbSize", ctypes.c_ulong),
    ("flags", ctypes.c_ulong),
    ("hwndActive", ctypes.c_ulong),
    ("hwndFocus", ctypes.c_ulong),
    ("hwndCapture", ctypes.c_ulong),
    ("hwndMenuOwner", ctypes.c_ulong),
    ("hwndMoveSize", ctypes.c_ulong),
    ("hwndCaret", ctypes.c_ulong),
    ("rcCaret", RECT)
    ]

guiThreadInfoStruct = GUITHREADINFO()


ctypes.sizeof(gtitest)

test1 = win32gui.FindWindowEx(0, 0, 0, "notepad")
(test1tid, test1pid) = win32process.GetWindowThreadProcessId(test1)
ctypes.windll.user32.GetGUIThreadInfo(test1tid, guiThreadInfoStruct)
print (guiThreadInfoStruct.hwndFocus)
更新2:

我找到了类型

更新3:

如果有人想看看我用它做什么,那就去看看吧。显然,PyWin32并没有包装它,所以必须使用其他方法。其中之一是通过调用它(包括编写大量额外代码)

代码00.py:

#/usr/bin/env python
导入系统
将win32gui导入为wgui
将win32process作为wproc导入
将win32con作为wcon导入
将ctypes导入为ct
从ctypes将wintypes作为wt导入
类信息(ct.Structure):
_字段=[
(“cbSize”,wt.DWORD),
(“旗帜”,wt.DWORD),
(“hwndActive”,wt.HWND),
(“hwndFocus”,wt.HWND),
(“hwndCapture”,wt.HWND),
(“hwndMenuOwner”,wt.HWND),
(“hwndMoveSize”,wt.HWND),
(“hwndCaret”,wt.HWND),
(“rcCaret”,wt.RECT),
]
定义(自我):
ret=“\n”+自我报告
start_format=“\n{0:s}:”
对于字段_name,u in self_字段\[:-1]:
字段值=getattr(自身,字段名称)
field_format=start_format+(“0x{1:016X}”如果field_值为其他“{1:}”)
ret+=字段\格式。格式(字段\名称、字段\值)
rc\u caret=getattr(self,self.\u字段\uu[-1][0])
ret+=(start_format+“({1:d},{2:d},{3:d},{4:d})”)。格式(self._字段{u[-1][0],rc_caret.top,rc_caret.left,rc_caret.right,rc_caret.bottom)
回程网
def主(*argv):
window\u name=“无标题-记事本”
hwnd=wgui.FindWindowEx(wcon.NULL,0,wcon.NULL,窗口名称)
打印(“{0:s}”窗口句柄:0x{1:016X}”。格式(窗口名称,hwnd))
tid,pid=wproc.GetWindowThreadProcessId(hwnd)
打印(“PId:{0:d},TId:{1:d}”。格式(PId,TId))
user32_dll=ct.windell(“user32.dll”)
GetGuitThreadInfo=getattr(user32_dll,“GetGuitThreadInfo”)
GetGUIThreadInfo.argtypes=[wt.DWORD,ct.POINTER(GUITHREADINFO)]
GetGUIThreadInfo.restype=wt.BOOL
gti=GUITHREADINFO()
gti.cbSize=ct.sizeof(GUITHREADINFO)
res=GetGUIThreadInfo(tid,ct.byref(gti))
打印(“{0:s}返回:{1:d}”。格式(getGuitThreadInfo.\uuuu name\uuuuuu,res))
如果有的话:
打印(gti)
如果名称=“\uuuuu main\uuuuuuuu”:
打印(“Python{0:s}{1:d}位在{2:s}\n.format(“.join(sys.version.split(“\n”)中的项的item.strip()),如果sys.maxsize>0x100000000,则为64,否则为32,sys.platform))
main(*sys.argv[1:])
打印(“\n完成”)
输出

e:\Work\Dev\StackOverflow\q059884688>“e:\Work\Dev\VEnvs\py\u pc064\u 03.07.06\u test0\Scripts\python.exe”code00.py
win32上的Python 3.7.6(tags/v3.7.6:43364a7ae0,2019年12月19日,00:42:30)[MSC v.1916 64位(AMD64)]64位
“无标题-记事本”窗口句柄:0x00000000042B20D8
PId:37192,TId:53072
返回的GetGuitThreadInfo:1
cbSize:0x0000000000000048
旗帜:0
hwndActive:没有
hwndFocus:无
hwndCapture:无
hwndMenuOwner:无
hwndMoveSize:无
hwndCaret:无
rcCaret:(0,0,0,0)
完成。
注释

  • 打印您正在使用的数据,因为它可能与您期望的不同。例如,记事本窗口标题不是代码期望的“记事本”,在这种情况下,win32gui.FindWindowEx将返回NULL(0)
  • 我也使用(它既旧又过时,但在大多数情况下非常有用)

@sth0r:这回答了你的问题吗?如果是,请接受答案,以便其他人也能够承认()。如果没有,请让我知道如何改进它,这样它就回答了问题。我可以两者都做吗?它确实解决了问题,但我不理解部分解决方案,我想把它变成一个函数,将hwnd作为输入并返回hwndFocus,但我不想得到答案,但可能需要一些澄清。我真的不明白这是怎么回事吗?GetGuitThreadInfo=getattr(user32_dll,“GetGuitThreadInfo”),当某个对象将self作为输入时,这意味着什么?回答第一条注释:它在加载的.dll实例中搜索具有给定名称的函数。但是它包含在cTypesURL中(答案中)。至于第二条评论:检查。