我正在尝试通过 python 和 ctypes 设置 Win32 窗口的所有者(不是父级)。
当我打电话时:
GWL_HWNDPARENT = -8
ctypes.windll.user32.SetWindowLongW(hWndChild, GWL_HWNDPARENT, hWndParent)
失败,ctypes.GetLastError() 返回 1413 - ERROR_INVALID_INDEX (link)。
hWndChild 和 hWndParent 似乎都有效,因为我可以成功使用 ctypes.windll.user32.SetParent。
我很困惑。无效索引指的是什么?
根据评论更新。
这需要两个记事本实例分别编辑OwnerWindow.txt和NonOwnerWindow.txt
import ctypes
GWL_HWNDPARENT = -8
_FindWindow = ctypes.windll.user32.FindWindowW
_FindWindow.argtypes = [ctypes.c_wchar_p,ctypes.c_wchar_p]
_FindWindow.restype = ctypes.c_void_p
_SetWindowLong = ctypes.windll.user32.SetWindowLongW
_SetWindowLong.argtypes = [ctypes.c_long, ctypes.c_long, ctypes.c_long]
_SetWindowLong.restype = ctypes.c_void_p
oHWnd = _FindWindow('Notepad', 'OwnerWindow.txt - Notepad')
nHWnd = _FindWindow('Notepad', 'NonOwnerWindow.txt - Notepad')
print "Last Error: {0}".format(str(ctypes.GetLastError()))
_SetWindowLong(ctypes.c_int(nHWnd), ctypes.c_int(GWL_HWNDPARENT), ctypes.c_int(oHWnd))
print "Last Error: {0}".format(str(ctypes.GetLastError()))
请您参考如下方法:
编辑:我做了一些测试并(重新)阅读了文档,同时考虑了@eryksun 的评论。
了解 SetWindowLong 是否存在的唯一方法失败或成功是使用SetLastError(0)就在调用之前,然后使用 GetLastError调用后,如果调用返回0。
If the function succeeds, the return value is the previous value of the specified 32-bit integer.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
If the previous value of the specified 32-bit integer is zero, and the function succeeds, the return value is zero, but the function does not clear the last error information. This makes it difficult to determine success or failure. To deal with this, you should clear the last error information by calling SetLastError with 0 before calling SetWindowLong. Then, function failure will be indicated by a return value of zero and a GetLastError result that is nonzero.
如果来自 ctypes 的 C/C++ 代码在您的 SetLastError 之间插入 Win32 API 调用吗?通话和决赛 SetWindowLong调用,不知道成败。
ctypes use_last_error示例:
from ctypes import *
from ctypes.wintypes import *
from ctypes import _SimpleCData
user32le = WinDLL('user32', use_last_error=True)
user32le.SetWindowLongW.restype = LONG
user32le.SetWindowLongW.argtypes = [HWND, c_int, LONG]
LONG_PTR = HWND # should be an integer, but a pointer is simpler
if sizeof(HWND) == sizeof(c_long): # WIN32
# WIN32: this is a macro in winuser.h
user32le.SetWindowLongPtrW = user32le.SetWindowLongW
elif sizeof(HWND) == sizeof(c_longlong): # WIN64
user32le.SetWindowLongPtrW.restype = LONG_PTR
user32le.SetWindowLongPtrW.argtypes = [HWND, c_int, LONG_PTR]
def SetWindowLongPtr(hWnd, nIndex, dwNewLong):
if isinstance(dwNewLong, _SimpleCData):
# avoid ArgumentError with LONG, etc
dwNewLong = dwNewLong.value
# set last_error to 0, and save the previous value
last_error = set_last_error(0)
try:
# ctypes calls GetLastError / SetLastError to swap
# LastError and last_error
result = user32le.SetWindowLongPtrW(hWnd, nIndex, dwNewLong)
# ctypes again swaps LastError and last_error
finally:
last_error = set_last_error(last_error)
if not result and last_error:
raise WinError(last_error)
return result
