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.