[Python]:ctypes - Python 的外部函数库没有提供完整的示例。
code.py:
#!/usr/bin/env python3
import sys
import ctypes
from ctypes import wintypes
def errcheck(result, func, args):
if not result:
raise ctypes.WinError()
rc = args[1]
return rc.left, rc.top, rc.bottom, rc.right
def test_get_window_rect(desktop_wnd_handle):
print("\n{:s}\n".format(test_get_window_rect.__name__))
prototype = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, ctypes.POINTER(wintypes.RECT))
paramflags = (1, "hwnd"), (2, "lprect")
GetWindowRect = prototype(("GetWindowRect", ctypes.windll.user32), paramflags)
print("Without errcheck:\n")
result = GetWindowRect(ctypes.windll.user32.GetDesktopWindow())
print("Left: {:d}, Top: {:}, Right: {:d}, Bottom: {:}".format(result.left, result.top, result.right, result.bottom))
result = GetWindowRect(0)
print("Left: {:d}, Top: {:}, Right: {:d}, Bottom: {:}".format(result.left, result.top, result.right, result.bottom))
GetWindowRect.errcheck = errcheck
print("\nWith errcheck:\n")
result = GetWindowRect(desktop_wnd_handle)
print("Left: {:d}, Top: {:}, Right: {:d}, Bottom: {:}".format(*result))
result = GetWindowRect(0)
print("Left: {:d}, Top: {:}, Right: {:d}, Bottom: {:}".format(*result))
def test_get_window_rect_old_style(desktop_wnd_handle):
print("\n{:s}\n".format(test_get_window_rect_old_style.__name__))
user32_dll = ctypes.WinDLL("user32")
get_windows_rect_func = user32_dll.GetWindowRect
get_windows_rect_func.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.RECT)]
get_windows_rect_func.restype = wintypes.BOOL
rect = wintypes.RECT()
result = get_windows_rect_func(desktop_wnd_handle, ctypes.byref(rect))
print("Result: {:d}\nLeft: {:d}, Top: {:}, Right: {:d}, Bottom: {:}".format(result, rect.left, rect.top, rect.right, rect.bottom))
rect = wintypes.RECT()
result = get_windows_rect_func(0, ctypes.byref(rect))
print("Result: {:d}\nLeft: {:d}, Top: {:}, Right: {:d}, Bottom: {:}".format(result, rect.left, rect.top, rect.right, rect.bottom))
def main():
hwnd = ctypes.windll.user32.GetDesktopWindow()
test_get_window_rect_old_style(hwnd)
test_get_window_rect(hwnd)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
Notes:
- 函数调用正常发生(没有参数处理 - 将它们放在列表中),并且仅input指定参数
-
errcheck“魔法”图解
- 还包括调用函数的老式方法
Output:
(py35x64_test) e:\Work\Dev\StackOverflow\q050669907>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
test_get_window_rect_old_style
Result: 1
Left: 0, Top: 0, Right: 1920, Bottom: 1080
Result: 0
Left: 0, Top: 0, Right: 0, Bottom: 0
test_get_window_rect
Without errcheck:
Left: 0, Top: 0, Right: 1920, Bottom: 1080
Left: 0, Top: 0, Right: 0, Bottom: 0
With errcheck:
Left: 0, Top: 0, Right: 1080, Bottom: 1920
Traceback (most recent call last):
File "code.py", line 58, in <module>
main()
File "code.py", line 53, in main
test_get_window_rect(hwnd)
File "code.py", line 30, in test_get_window_rect
result = GetWindowRect(0)
File "code.py", line 9, in errcheck
raise ctypes.WinError()
OSError: [WinError 1400] Invalid window handle.