pywinauto: ctypes.ArgumentError @ click_input

Windows 10.0.15063 x64 Python 3.6.2 pywinauto 0.6.3

 
Setup (functional):

hwnd = my_kivy_app.get_hwnd()

app = pywinauto.Application()
app.connect(handle=hwnd)
window = app.window(handle=hwnd).wrapper_object()

 
Exception @:

window.click_input(button='left', pressed='', coords=(100, 100), double=False, absolute=False)
Traceback (most recent call last):
  File "Test_Window_Commands.py", line 114, in <module>
    app.run()
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\kivy\app.py", line 828, in run
    runTouchApp()
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\kivy\base.py", line 504, in runTouchApp
    EventLoop.window.mainloop()
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
    self._mainloop()
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\kivy\core\window\window_sdl2.py", line 405, in _mainloop
    EventLoop.idle()
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\kivy\base.py", line 339, in idle
    Clock.tick()
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\kivy\clock.py", line 581, in tick
    self._process_events()
  File "kivy\_clock.pyx", line 367, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7700)
  File "kivy\_clock.pyx", line 397, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7577)
  File "kivy\_clock.pyx", line 395, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7498)
  File "kivy\_clock.pyx", line 167, in kivy._clock.ClockEvent.tick (kivy\_clock.c:3483)
  File "C:\OneDrive\_Projects\Python\dev\kivy\basic\_app.py", line 140, in _wrapped
    function(*args, **kwargs)
  File "Test_Window_Commands.py", line 104, in ctrl_shift_alt
    app._window.click_input(button='left', pressed='', coords=(100, 100), double=False, absolute=False)
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\pywinauto\base_wrapper.py", line 667, in click_input
    coords = self.client_to_screen(coords)
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\pywinauto\base_wrapper.py", line 333, in client_to_screen
    rect = self.element_info.rectangle
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\pywinauto\win32_element_info.py", line 142, in rectangle
    return handleprops.rectangle(self.handle)
  File "C:\OneDrive\_Frameworks\Python\3\lib\site-packages\pywinauto\handleprops.py", line 200, in rectangle
    win32functions.GetWindowRect(handle, ctypes.byref(rect))
ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected LP_RECT instance instead of pointer to RECT

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you both (@airelil, @vasily-v-ryabov ) for clarifying things. I suppose you won’t reject any potential (ctypes related) PRs, that I might submit when I’ll have the time.

  •  ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected LP_RECT instance instead of pointer to RECT
    

    (worked around by win32gui.GetWindowRect) was because both modules had their own RECT (in terms of) definition, and prototype specified one but the other was passed. I think if both classes would have extended wintypes.RECT (without redefining _fields_, of course), and windll.user32.GetWindowRect’s last argtype would have been POINTER(wintypes.RECT), both could have worked with ctypes

  •   ctypes.ArgumentError: argument 3: <class 'TypeError'>: wrong type
    

    is a bug on Kivy’s side: [GitHub]: kivy/kivy - SetWindowLongPtrW ctypes prototype bug

I’m getting back to the old issues and I could reproduce the original issue with GetWindowRect. The fix is here (in master branch already, just verified it): https://github.com/pywinauto/pywinauto/commit/3a24b360220515f0e84b69af8962096bb28bccac#diff-a0da689e9deac595ceaf85dd5d6e8fa1

@Enteleform if it’s still relevant, do you have any more pywinauto issues related to this project?

Can you also try to import pywinauto after kivy?

Tried it, and the same issue occurs.

 

I’m working to fix the first issue using win32gui.GetWindowRect function instead of ctypes.windll.user32.GetWindowRect.

That seems like it should work. I managed to get the same functionality working in Kivy without any issues using:

  • win32gui.GetWindowRect and/or the GetWindowRect method of pywin32’s PyCWnd object
  • the mouse library, which uses
    user32 = ctypes.WinDLL('user32', use_last_error=True)
    user32.mouse_event

Maybe creating a user32 instance with ctypes.WinDLL instead of using ctypes.windll.user32 will help?

OK, Kivy is installed on the same Python version as pywinauto?

Yes.

 

I’d like to try it on my side. Are you using pywinauto inside your Kivy app?

Yes, here’s an MVCE:

import random
import pywinauto
from kivy.app           import App
from kivy.lang          import Builder
from kivy.core.window   import Window
from kivy.uix.boxlayout import BoxLayout


class DemoLayout(BoxLayout): pass
Builder.load_string("""
#: import datetime  datetime.datetime
<DemoLayout>:
  padding: 75

  Button:
    on_press: print(f"PRESSED @ {datetime.now()}")
""")


class Demo(App):
	
  def build(self):
    self.root = DemoLayout()

  def on_start(self):
    title = f"__KIVY_APP__{random.getrandbits(128)}"
    Window.set_title(title)
    hwnd = pywinauto.findwindows.find_window(title=title)
    app = pywinauto.Application()
    app.connect(handle=hwnd)
    window = app.window(handle=hwnd).wrapper_object()
    window.click_input(button="left", pressed="", coords=(100, 100), double=False, absolute=False)


Demo().run()