Jun-26-2024, 01:27 PM
Working on Microsoft Windows [Version 10.0.19045.4412], Python 3.8.10 (I need to compile my script for Windows 7 as well...), comtypes 1.4.4
I am interacting with a Windows application through comtypes
Now, I have a method that asks for String array as an argument, defined as follows (inside its owner's class _methods_ list, of course)
I am interacting with a Windows application through comtypes
import comtypes.client import numpy as np from comtypes.automation import VARIANT comtypes.client.GetModule('path\to\application.exe') from comtypes.gen import Application as App comtypes.npsupport.enable() MyApp = comtypes.client.CreateObject('CLSID', interface=App.IDualApplication)and everything is working smooth: I found methods that asks for Variant (two-element array of double with array lower bound = 0 and array upper bound = 1) as an argument, defined as follows (inside its owner's class _methods_ list, of course)
COMMETHOD( [dispid(id), helpstring('Perform this action')], HRESULT, 'ThisMethod', (['in'], VARIANT, 'dValues'), )I managed to deal with them with
MyApp.ThisMethod(VARIANT(np.array([x, y], dtype='float')))Sweet!
Now, I have a method that asks for String array as an argument, defined as follows (inside its owner's class _methods_ list, of course)
COMMETHOD( [dispid(id), helpstring('Perform that action.')], HRESULT, 'ThatMethod', (['in', 'optional'], VARIANT, 'strArrayItems'), )Here's how I tried to deal with it, with the results I got:
MyApp.ThatMethod(['x', 'y'])
Error:Traceback (most recent call last):
MyApp.ThatMethod(['x', 'y'])
File "\venv\lib\site-packages\comtypes\automation.py", line 568, in __ctypes_from_outparam__
result = self.value
File "\venv\lib\site-packages\comtypes\automation.py", line 520, in _get_value
typ = _vartype_to_ctype[self.vt & ~VT_ARRAY]
KeyError: 9
MyApp.ThatMethod(VARIANT(np.array(['x', 'y'], dtype='str')))
Error:Traceback (most recent call last):
MyApp.ThatMethod(['x', 'y'])
File "\venv\lib\site-packages\comtypes\automation.py", line 215, in __init__
self.value = args[0]
File "\venv\lib\site-packages\comtypes\automation.py", line 344, in _set_value
obj = _midlSAFEARRAY(VARIANT).create(value)
File "\venv\lib\site-packages\comtypes\safearray.py", line 122, in create
return cls.create_from_ndarray(value, extra)
File "\venv\lib\site-packages\comtypes\safearray.py", line 165, in create_from_ndarray
value = _ndarray_to_variant_array(value)
File "\venv\lib\site-packages\comtypes\safearray.py", line 386, in _ndarray_to_variant_array
if comtypes.npsupport.interop.VARIANT_dtype is None:
AttributeError: 'Interop' object has no attribute 'interop'
variant = VARIANT(VT_ARRAY | VT_BSTR) variant.value = ['x', 'y'] MyApp.ThatMethod(variant)
Error:Traceback (most recent call last):
MyApp.ThatMethod(variant)
File "\venv\lib\site-packages\comtypes\automation.py", line 568, in __ctypes_from_outparam__
result = self.value
File "\venv\lib\site-packages\comtypes\automation.py", line 520, in _get_value
typ = _vartype_to_ctype[self.vt & ~VT_ARRAY]
KeyError: 9
variant = VARIANT(VT_ARRAY | VT_BSTR) variant.value = np.array(['x', 'y'], dtype='str') MyApp.ThatMethod(variant)
Error:Traceback (most recent call last):
variant.value = np.array(['x', 'y'], dtype='str')
File "\venv\lib\site-packages\comtypes\automation.py", line 344, in _set_value
obj = _midlSAFEARRAY(VARIANT).create(value)
File "\venv\lib\site-packages\comtypes\safearray.py", line 122, in create
return cls.create_from_ndarray(value, extra)
File "\venv\lib\site-packages\comtypes\safearray.py", line 165, in create_from_ndarray
value = _ndarray_to_variant_array(value)
File "\venv\lib\site-packages\comtypes\safearray.py", line 386, in _ndarray_to_variant_array
if comtypes.npsupport.interop.VARIANT_dtype is None:
AttributeError: 'Interop' object has no attribute 'interop'
How can I provide this COM method with a String array?