moses-palmer / pystray

GNU General Public License v3.0
463 stars 57 forks source link

ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected LP_POINT instance instead of pointer to POINT when using both pyautogui and pystray #93

Closed TomsonBoylett closed 3 years ago

TomsonBoylett commented 3 years ago
import pyautogui
import pystray
pyautogui.moveTo(10,10)

The code above causes the following error

File "\venv\lib\site-packages\pyautogui\_pyautogui_win.py", line 351, in _position
    ctypes.windll.user32.GetCursorPos(ctypes.byref(cursor))
ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected LP_POINT instance instead of pointer to POINT

Based on the contents of a similar pywinauto issue here, I think the issue is that pystray is setting attributes on the global windll instance.

I have confirmed this by removing the following lines from pystray_util\win32.py

GetCursorPos = windll.user32.GetCursorPos
GetCursorPos.argtypes = (
    LPPOINT,)
GetCursorPos.restype = wintypes.BOOL
GetCursorPos.errcheck = _err

This prevents the error but stops pystray detecting mouse events properly.

I'm guessing the fix is either to create a new instance using windll = ctypes.LibraryLoader(ctypes.WinDLL). or to call the global instance without setting any attributes.

See related issue for pyautogui here

moses-palmer commented 3 years ago

Thank you for your report, and apologies for the late reply.

I think your suggestion to instantiate a new windll is what this library has needed for a very long time! I have made a new release and pushed it to Github; unfortunately it appears that my upload script is no longer compatible with PyPi: I get the following message:

Upload failed (400): Invalid value for blake2_256_digest. Error: Use a valid, hex-encoded, BLAKE2 message digest.

I will look into this in the following days, but in the meantime, you may use Github as a mirror.

TomsonBoylett commented 3 years ago

I switched to the new version and I can confirm it fixed the issue. Thanks for the update!