OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Triet Vo Nguyen Minh
  • Start date Start date
T

Triet Vo Nguyen Minh

Guest
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:

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.
<p>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?</p>

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

<p>This is my code:</p>

<pre class="lang-py prettyprint-override"><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
"""
</code></pre>

<p>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.</p>

<p>Reality: As shown above.</p>
 

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top