MessageBox hook using subclassΒΆ

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