question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When pywinauto uses “ctypes” to call the same system function with other modules ,it will cause the ctypes.ArgumentError

See original GitHub issue

Steps to Reproduce the Problem

  1. import pywinauto
  2. import other module which using ctypes to call the same system function

Actual Behavior

return bool(ctypes.windll.user32.IsWindowVisible(ctypes.c_void_p(handle)))
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type

Expected Behavior

If I delete the the argtypes and restype in win32functions.py, for example

IsWindowVisible.argtypes = [win32structures.HWND]
IsWindowVisible.restype = win32structures.BOOL ,

everything seems to go well.

Short Example of Code to Demonstrate the Problem

from pywinauto.application import Application
import uiautomation as auto
from data.setting import Setting

window = auto.WindowControl(AutomationId="mywindowautoid",Name="mywindowname",searchDepth =1)
window.SetActive()
app = Application(backend="uia").connect(path=Setting.exepath)
print(app.is_process_running())

Specifications

  • Pywinauto version: the lastest version 0.6.6
  • Python version and bitness: 3.7.2 and 32bit
  • Platform and OS: Win10

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
airelilcommented, Sep 9, 2019

Pywinauto sets up the return type of GetWindow to wintypes.HWND as per https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindow The xlwings problem seems to be due to this line in _xlwings.py: https://github.com/xlwings/xlwings/blob/4a51d73b87af3820fc686b402e65e0c843e9d63c/xlwings/_xlwindows.py#L300 because the c_void_p is casted to Python long type instead of integer returned by ctypes restype by default. Limiting restype to integer is dangerous, especially on 64-bit OS, because HWND can exceed the integer type max size, which I believe is 32-bits for Python 2.7 (see: sys.maxint)

0reactions
shovalcommented, Sep 8, 2019

I encountered the same problem using the xlwings package.

How to reproduce

  1. Open some Excel workbook (one or more, it doesn’t matter)
  2. Run the code below once - it should print out the names of the open workbooks
  3. Uncomment the first row and run again - now it should raise an attribute error
# import pywinauto
import xlwings as xw
print xw.books

Insights

I managed to track the problem to the following lines in win32functions.py:

GetWindow = windll.user32.GetWindow
GetWindow.restype = wintypes.HWND

Overriding GetWindow.restype, which is global, is what causes the problem.

Specifications

  • Pywinauto version: 0.6.7
  • Python version: 2.7.15 (64bit)
  • Platform and OS: Win7
Read more comments on GitHub >

github_iconTop Results From Across the Web

ctypes.ArgumentError when using kivy with pywinauto
Both modules call this function via ctypes: ... Kivy tried to call the function using Pywinauto's prototype, otherwise it would have worked.
Read more >
Issues · pywinauto/pywinauto · GitHub
When pywinauto uses “ctypes” to call the same system function with other modules ,it will cause the ctypes.ArgumentError need investigation.
Read more >
pywinauto wrapper_object | The Search Engine You Control
An HwndWrapper object can be passed directly to a ctypes wrapped C function - and it will get converted to a Long with...
Read more >
2009-October.txt - Python mailing list
I started this for the first few methods of the IRealTimeStylus interface: """ # module realtimestylus.py from ctypes import * from ctypes.wintypes import ......
Read more >
PyInstaller Documentation - Read the Docs
When you install PyInstaller using pip, the setup will attempt to build a ... Ctypes is a foreign function library for Python, that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found