Skip to content

Commit

Permalink
Provide a local definition of user32.SetTimer, user32.KillTimer and o…
Browse files Browse the repository at this point in the history
…le32.CoGetApartmentType on Windows

By overriding the type definition of SetTimer, we prevent None being passed as the callback parameter. This causes problems when integrating Bleak in larger Windows applications that call SetTimer in this way.
By defining the function type definition locally, we don't override the global definition so existing code that uses None as a callback keeps working.

To prevent other problems, we also make the definitions of KillTimer and CoGetApartmentType local.
  • Loading branch information
bramd committed Jun 30, 2024
1 parent 5d6d145 commit f31c69d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Contributors
* Robbe Gaeremynck <[email protected]>
* David Johansen <[email protected]>
* JP Hutchins <[email protected]>
* Bram Duvigneau <[email protected]>

Sponsors
--------
Expand Down
50 changes: 39 additions & 11 deletions bleak/backends/winrt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,52 @@ def _check_hresult(result, func, args):
)

# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-settimer
_SetTimer = ctypes.windll.user32.SetTimer
_SetTimer.restype = _UINT_PTR
_SetTimer.argtypes = [wintypes.HWND, _UINT_PTR, wintypes.UINT, _TIMERPROC]
_SET_TIMER_PROTOTYPE = ctypes.WINFUNCTYPE(
_UINT_PTR, wintypes.HWND, _UINT_PTR, wintypes.UINT, _TIMERPROC
)
_SET_TIMER_PARAM_FLAGS = (
(1, "hwnd", None),
(
1,
"nidevent",
),
(
1,
"uelapse",
),
(1, "lptimerfunc", None),
)
_SetTimer = _SET_TIMER_PROTOTYPE(
("SetTimer", ctypes.windll.user32), _SET_TIMER_PARAM_FLAGS
)
_SetTimer.errcheck = _check_result

# https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-killtimer
_KillTimer = ctypes.windll.user32.KillTimer
_KillTimer.restype = wintypes.BOOL
_KillTimer.argtypes = [wintypes.HWND, wintypes.UINT]

_KILL_TIMER_PROTOTYPE = ctypes.WINFUNCTYPE(
wintypes.BOOL, wintypes.HWND, _UINT_PTR
)
_KILL_TIMER_PARAM_FLAGS = (
(1, "hwnd", None),
(
1,
"uidevent",
),
)
_KillTimer = _KILL_TIMER_PROTOTYPE(("KillTimer", ctypes.windll.user32), _KILL_TIMER_PARAM_FLAGS)

# https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cogetapartmenttype
_CoGetApartmentType = ctypes.windll.ole32.CoGetApartmentType
_CoGetApartmentType.restype = ctypes.c_int
_CoGetApartmentType.argtypes = [
_CO_GET_APARTMENT_TYPE_PROTOTYPE = ctypes.WINFUNCTYPE(
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int),
]
)
_CO_GET_APARTMENT_TYPE_PARAM_FLAGS = (
(1, "papttype", None),
(1, "paptqualifier", None),
)
_CoGetApartmentType = _CO_GET_APARTMENT_TYPE_PROTOTYPE(
("CoGetApartmentType", ctypes.windll.ole32), _CO_GET_APARTMENT_TYPE_PARAM_FLAGS
)
_CoGetApartmentType.errcheck = _check_hresult

_CO_E_NOTINITIALIZED = -2147221008
Expand Down

0 comments on commit f31c69d

Please sign in to comment.