0

I am trying to Automate a Tool via win32com.client module which expects the Input to be in the following format,The format shown below if specific to MATLAB.

HRESULT StaticStokesParameters([in] SAFEARRAY(double) newVal)

I have no clue what does SAFEARRAY type represent. I have tried to create an 2D array in python, but i keep receiving the following error,

pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 1)

I can read the values out without any problem, but when i assign the same value back as SET argument, then it fails to do so,

EngineMgr = win32com.client.Dispatch("EngineMgr")
Engine = EngineMgr.OpenEngine(0)
d_array = Engine.StaticStokesParameters
print(d_array)
**(-1.0, 0.0, 0.0) # Output of Print Statement**
Engine.StaticStokesParameters = d_array
**Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
Engine.StaticStokesParameters = d_array
File "C:\Users\ashes\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 549, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 1)**
Ashesh Nair
  • 317
  • 5
  • 21
  • SAFEARRAY are a sort of COM array. win32com should support SAFEARRAYs natively, google on "SAFEARRAY win32com python" – Simon Mourier Dec 16 '19 at 09:05
  • @SimonMourier I tried that (for a similar issue). Does not seem to work. Do SAFEARRAYS have to be built up using CTYPES? – Jiminion Mar 19 '23 at 16:21
  • The answer below constructs a SAFEARRAY using pywin32, if it doesn't suit your need, you should ask another question – Simon Mourier Mar 19 '23 at 16:51
  • @SimonMourier I did post a similar question. https://stackoverflow.com/questions/75769241/how-do-you-format-a-list-of-python-values-to-be-compatible-with-the-com-safearra – Jiminion Mar 19 '23 at 17:08

1 Answers1

0

Got this working by importing below shown modules,

from win32com.client import VARIANT
import pythoncom

sop= Engine.StaticStokesParameters
Engine.StaticStokesParameters = VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8,sop)
Ashesh Nair
  • 317
  • 5
  • 21