Python Forum
comtypes: how to provinde a list of string to a COM method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comtypes: how to provinde a list of string to a COM method
#1
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
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question about List.reverse() method tomliuwhite 1 1,485 Dec-07-2021, 08:20 AM
Last Post: ndc85430
  Unable to use random.choice(list) in async method spacedog 4 3,641 Apr-29-2021, 04:08 PM
Last Post: spacedog
  About list and method .... Fernando_7obink 3 2,425 Dec-22-2020, 09:15 AM
Last Post: Fernando_7obink
  How to call COM-method using comtypes jespersahner 0 2,547 Nov-15-2019, 12:54 PM
Last Post: jespersahner
  print all method and property of list object engmoh 4 3,038 Oct-26-2019, 05:33 PM
Last Post: engmoh
  How to run a method on an argument in a formatted string Exsul 1 1,754 Aug-30-2019, 01:57 AM
Last Post: Exsul
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,393 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  How to use a string method on user input Exsul 2 2,765 Mar-17-2019, 08:12 PM
Last Post: Exsul
  String Method 'find(...)'. ClassicalSoul 3 2,435 Feb-27-2019, 12:24 PM
Last Post: buran
  why my method doesn't find my List in the same class? Scorpio 2 2,468 Jan-31-2019, 05:21 PM
Last Post: Scorpio

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020