diff --git a/comtypes/messageloop.py b/comtypes/messageloop.py index 27b67068..fc545452 100644 --- a/comtypes/messageloop.py +++ b/comtypes/messageloop.py @@ -1,6 +1,7 @@ import ctypes -from ctypes import WinDLL, WinError, byref -from ctypes.wintypes import MSG +from ctypes import POINTER, WinDLL, WinError, byref +from ctypes.wintypes import BOOL, HWND, MSG +from ctypes.wintypes import LPLONG as LRESULT from typing import TYPE_CHECKING, SupportsIndex if TYPE_CHECKING: @@ -10,12 +11,29 @@ _FilterCallable = Callable[["_CArgObject"], Iterable[Any]] # type: ignore +# PeekMessage options +PM_NOREMOVE = 0x0000 +PM_REMOVE = 0x0001 +PM_NOYIELD = 0x0002 + _user32 = WinDLL("user32") GetMessage = _user32.GetMessageA -GetMessage.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint, ctypes.c_uint] +GetMessage.argtypes = [POINTER(MSG), HWND, ctypes.c_uint, ctypes.c_uint] +GetMessage.restype = BOOL + TranslateMessage = _user32.TranslateMessage +TranslateMessage.argtypes = [POINTER(MSG)] +TranslateMessage.restype = BOOL + DispatchMessage = _user32.DispatchMessageA +DispatchMessage.argtypes = [POINTER(MSG)] +DispatchMessage.restype = LRESULT + +# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagea +PeekMessage = _user32.PeekMessageA +PeekMessage.argtypes = [POINTER(MSG), HWND, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint] +PeekMessage.restype = BOOL class _MessageLoop: diff --git a/comtypes/test/test_eventinterface.py b/comtypes/test/test_eventinterface.py index f2b9d303..9bfca80d 100644 --- a/comtypes/test/test_eventinterface.py +++ b/comtypes/test/test_eventinterface.py @@ -1,8 +1,14 @@ import unittest as ut -from ctypes import POINTER, Structure, WinDLL, byref, c_long, c_uint, c_ulong -from ctypes.wintypes import BOOL, HWND, LPLONG, UINT +from ctypes import byref +from ctypes.wintypes import MSG from comtypes.client import CreateObject, GetEvents +from comtypes.messageloop import ( + PM_REMOVE, + DispatchMessage, + PeekMessage, + TranslateMessage, +) # FIXME: External test dependencies like this seem bad. Find a different # built-in win32 API to use. @@ -42,42 +48,11 @@ def DocumentComplete(self, this, *args): self._events.append("DocumentComplete") -class POINT(Structure): - _fields_ = [("x", c_long), ("y", c_long)] - - -class MSG(Structure): - _fields_ = [ - ("hWnd", c_ulong), - ("message", c_uint), - ("wParam", c_ulong), - ("lParam", c_ulong), - ("time", c_ulong), - ("pt", POINT), - ] - - def PumpWaitingMessages(): - _user32 = WinDLL("user32") - - _PeekMessageA = _user32.PeekMessageA - _PeekMessageA.argtypes = [POINTER(MSG), HWND, UINT, UINT, UINT] - _PeekMessageA.restype = BOOL - - _TranslateMessage = _user32.TranslateMessage - _TranslateMessage.argtypes = [POINTER(MSG)] - _TranslateMessage.restype = BOOL - - LRESULT = LPLONG - _DispatchMessageA = _user32.DispatchMessageA - _DispatchMessageA.argtypes = [POINTER(MSG)] - _DispatchMessageA.restype = LRESULT - msg = MSG() - PM_REMOVE = 0x0001 - while _PeekMessageA(byref(msg), 0, 0, 0, PM_REMOVE): - _TranslateMessage(byref(msg)) - _DispatchMessageA(byref(msg)) + while PeekMessage(byref(msg), 0, 0, 0, PM_REMOVE): + TranslateMessage(byref(msg)) + DispatchMessage(byref(msg)) class Test(ut.TestCase): diff --git a/comtypes/test/test_git.py b/comtypes/test/test_git.py index 90978579..990627ec 100644 --- a/comtypes/test/test_git.py +++ b/comtypes/test/test_git.py @@ -7,7 +7,7 @@ from _ctypes import COMError from collections.abc import Iterator from ctypes import HRESULT, POINTER, OleDLL, WinDLL, byref -from ctypes.wintypes import BOOL, DWORD, HANDLE, HWND, MSG, UINT +from ctypes.wintypes import BOOL, DWORD, HANDLE, MSG from pathlib import Path from queue import Queue @@ -18,16 +18,16 @@ RegisterInterfaceInGlobal, RevokeInterfaceFromGlobal, ) -from comtypes.messageloop import DispatchMessage, TranslateMessage +from comtypes.messageloop import ( + PM_REMOVE, + DispatchMessage, + PeekMessage, + TranslateMessage, +) from comtypes.persist import STGM_READ, IPersistFile _user32 = WinDLL("user32") -# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagea -PeekMessage = _user32.PeekMessageA -PeekMessage.argtypes = [POINTER(MSG), HWND, UINT, UINT, UINT] -PeekMessage.restype = BOOL - # https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-msgwaitformultipleobjects MsgWaitForMultipleObjects = _user32.MsgWaitForMultipleObjects MsgWaitForMultipleObjects.restype = DWORD @@ -49,8 +49,6 @@ QS_ALLINPUT = 0x04FF # All message types including SendMessage -PM_REMOVE = 0x0001 # Remove message from queue after Peek - APTTYPE_MAINSTA = 3 RPC_E_WRONG_THREAD = -2147417842 # 0x8001010E