Search code examples
pythonctypes

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


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.


Solution

  • Listing [Python.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.Learn]: SetWindowsHookExA function (winuser.h) expects an [MS.Learn]: CBTProc callback function when its 1st argument is WH_CBT (5).
    So, you have to wrap your function:

    import ctypes as cts
    from ctypes import wintypes as wts
    
    
    HOOKProc = cts.WINFUNCTYPE(cts.c_size_t, cts.c_int, wts.WPARAM, wts.LPARAM)
    CBTProc = HOOKProc
    
    SetWindowsHookExA = cts.windll.user32.SetWindowsHookExA
    SetWindowsHookExA.argtypes = (cts.c_int, HOOKProc, wts.HINSTANCE, wts.DWORD)
    SetWindowsHookExA.restype = wts.HHOOK
    
    GetCurrentThreadId = cts.windll.kernel32.GetCurrentThreadId
    GetCurrentThreadId.argtypes = ()
    GetCurrentThreadId.restype = wts.DWORD
    
    WH_CBT = 5
    
    hook = cts.windll.user32.SetWindowsHookExA(WH_CBT, CBTProc(msgBoxHook), 0, 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):

    1. You don't define ArgTypes and ResType for any of the functions you use (already spotted).
      Check [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer) for more details on the topic

    2. You're using HCBT_CREATEWND (3). According to the (listed above) CBTProc URL (emphasis is mine):

      At the time of the HCBT_CREATEWND notification, the window has been created, but its final size and position may not have been determined and its parent window may not have been established.

    Might want to also check:



    Update #0

    Check [SO]: Ctypes method to specify the location of popup window (Python) (@CristiFati's answer) for an alternative.