MessageBox hook using constructorΒΆ

 1import ctypes
 2import ctypes.wintypes
 3import cyminhook
 4import win32api
 5import win32con
 6
 7signature = ctypes.WINFUNCTYPE(
 8    ctypes.c_int,
 9    ctypes.wintypes.HWND,
10    ctypes.wintypes.LPCWSTR,
11    ctypes.wintypes.LPCWSTR,
12    ctypes.wintypes.UINT,
13    ctypes.wintypes.WORD,
14    use_last_error=True,
15)
16
17target = ctypes.windll.user32.MessageBoxExW
18
19def detour(hWnd, lpText, lpCaption, uType, langId):
20    return hook.original(hWnd, "Hooked", "Hooked", uType, langId)
21
22with cyminhook.MinHook(signature=signature, target=target, detour=detour) as hook:
23    hook.enable()
24
25    win32api.MessageBox(None, "Hello, World!", "Python", win32con.MB_OK)