Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[client\_events.py] Use WinDLL/OleDLL instead of windll/oledll #758

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions comtypes/client/_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,58 @@
import comtypes.automation
import comtypes.typeinfo
import comtypes.connectionpoints
from ctypes import POINTER, WINFUNCTYPE, OleDLL, Structure, WinDLL, HRESULT
from ctypes.wintypes import (
BOOL,
DWORD,
HANDLE,
LPCSTR,
LPVOID,
ULONG,
LPHANDLE,
LPDWORD,
)
from comtypes.client._generate import GetModule
import logging

logger = logging.getLogger(__name__)


class SECURITY_ATTRIBUTES(Structure):
_fields_ = [
("nLength", DWORD),
("lpSecurityDescriptor", LPVOID),
("bInheritHandle", BOOL),
]


_ole32 = OleDLL("ole32")

_CoWaitForMultipleHandles = _ole32.CoWaitForMultipleHandles
_CoWaitForMultipleHandles.argtypes = [DWORD, DWORD, ULONG, LPHANDLE, LPDWORD]
_CoWaitForMultipleHandles.restype = HRESULT


_kernel32 = WinDLL("kernel32")

_CreateEventA = _kernel32.CreateEventA
_CreateEventA.argtypes = [POINTER(SECURITY_ATTRIBUTES), BOOL, BOOL, LPCSTR]
_CreateEventA.restype = HANDLE

_SetEvent = _kernel32.SetEvent
_SetEvent.argtypes = [HANDLE]
_SetEvent.restype = BOOL

PHANDLER_ROUTINE = WINFUNCTYPE(BOOL, DWORD)
_SetConsoleCtrlHandler = _kernel32.SetConsoleCtrlHandler
_SetConsoleCtrlHandler.argtypes = [PHANDLER_ROUTINE, BOOL]
_SetConsoleCtrlHandler.restype = BOOL

_CloseHandle = _kernel32.CloseHandle
_CloseHandle.argtypes = [HANDLE]
_CloseHandle.restype = BOOL


class _AdviseConnection(object):
def __init__(self, source, interface, receiver):
self.cp = None
Expand Down Expand Up @@ -274,24 +320,22 @@ def PumpEvents(timeout):
# CoWaitForMultipleHandles calls the Win32 function
# MsgWaitForMultipleObjects.

hevt = ctypes.windll.kernel32.CreateEventA(None, True, False, None)
hevt = _CreateEventA(None, True, False, None)
handles = _handles_type(hevt)
RPC_S_CALLPENDING = -2147417835

# @ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_uint)
# @ctypes.WINFUNCTYPE(BOOL, DWORD)
def HandlerRoutine(dwCtrlType):
if dwCtrlType == 0: # CTRL+C
ctypes.windll.kernel32.SetEvent(hevt)
_SetEvent(hevt)
return 1
return 0

HandlerRoutine = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_uint)(HandlerRoutine)

ctypes.windll.kernel32.SetConsoleCtrlHandler(HandlerRoutine, 1)
_SetConsoleCtrlHandler(PHANDLER_ROUTINE(HandlerRoutine), 1)

try:
try:
res = ctypes.oledll.ole32.CoWaitForMultipleHandles(
res = _CoWaitForMultipleHandles(
0,
int(timeout * 1000),
len(handles),
Expand All @@ -304,5 +348,5 @@ def HandlerRoutine(dwCtrlType):
else:
raise KeyboardInterrupt
finally:
ctypes.windll.kernel32.CloseHandle(hevt)
ctypes.windll.kernel32.SetConsoleCtrlHandler(HandlerRoutine, 0)
_CloseHandle(hevt)
_SetConsoleCtrlHandler(PHANDLER_ROUTINE(HandlerRoutine), 0)