How do I hook a ctypes.windll.user32.MessageBoxW by using ctypes.windll.user32.SetWindowsHookExW?

1

I wanna make a joke program that at first it opens a message box, after it closes then another message box appear at a random position. It would keep repeating like that until anything kills its task. Using tkinter message boxes then those can't be hooked, I must make another tkinter form (Which is really ugly and different from the Windows message box). So I switched to ctypes, and the problems begins. I created a callback function for the hook then, when I pass the function to the second parameter of the ctypes.windll.user32.SetWindowsHookExA function, then it show the TypeError: wrong type. How can I fix this?

I've tried to cast the function to a c_void_p, but doing that just get more errors like 'Illegal instruction'.

This is my code:

import ctypes, random

def msgBoxHook(nCode, wParam, lParam):
 if nCode == 3:
  hwnd = ctypes.wintypes.HWND
  hwnd = wParam

  msgRekt = ctypes.wintypes.RECT # >:)

  ctypes.windll.user32.GetWindowRect(hwnd, ctypes.byref(msgRekt))
  ctypes.windll.user32.MoveWindow(hwnd, randint(0, ctypes.windll.user32.GetSystemMetrics(0)), randint(0, ctypes.windll.user32.GetSystemMetrics(1)), msgRekt.right - msgRekt.left, msgRekt.bottom - msgRekt.top, True)
 return ctypes.windll.user32.CallNextHookEx(0, nCode, wParam, lParam)

# When I try to call
ctypes.windll.user32.SetWindowsHookExA(5, msgBoxHook, 0, ctypes.windll.kernel32.GetCurrentThreadId())

# It shows:
"""
Traceback (most recent call last):
  File "test.py", line 1, in <module>
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type
"""

Expected: When use ctypes.windll.user32.MessageBoxW(None, 'Hello', 'World', 0x00000010 | 0x00000000) it opens a message box at random position with caption 'World', text 'Hello' and Stop icon with OK button.

Reality: As shown above.

python
ctypes
asked on Stack Overflow Jun 15, 2019 by Triet Vo Nguyen Minh • edited Jun 16, 2019 by Triet Vo Nguyen Minh

1 Answer

0

Listing [Python 3.Docs]: ctypes - A foreign function library for Python.

You can't pass a Python object to a plain C function (I mean you can, but the results won't be the expected ones).

[MS.Docs]: SetWindowsHookExA function expects an [MS.Docs]: CBTProc callback function when its 1st argument is WH_CBT (5).
So, you have to wrap your function:

WH_CBT = 5

HOOKProc = ctypes.WINFUNCTYPE(wintypes.LPVOID, ctypes.c_int, wintypes.WPARAM, wintypes.LPARAM)
CBTProc = HOOKProc

hook = ctypes.windll.user32.SetWindowsHookExA(WH_CBT, CBTProc(msgBoxHook), 0, ctypes.windll.kernel32.GetCurrentThreadId())

This should get you past the current problem. But you'll definitely run into others (which of course can be the subject of other questions).
One that I've already spotted, is that don't define argtypes and restype for any of the functions you use, so that will (almost) certainly get you into trouble (crashes). Some examples:

answered on Stack Overflow Aug 31, 2019 by CristiFati • edited Sep 2, 2019 by CristiFati

User contributions licensed under CC BY-SA 3.0