comtypes icon indicating copy to clipboard operation
comtypes copied to clipboard

Error when trying to use a function that contains reference variables to return data.

Open JimmyBLambert opened this issue 5 years ago • 2 comments

Hi, I'm using comtypes with to try and call a function that uses reference parameters. The method I'm trying is passing all of the parameters with a value and trying to get the return value which should be a tuple with the [out] parameters inside. I've done lots of fiddling and keep getting this same error:

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "c:/Users/jlambert/source/repos/COM/Python/RunFrozPredict.py", line 28, in retTuple = af.RunFrozPredict(Cycles, finalC, finalKc, finalA, finalKa, finalCt, finalKct) File "C:\Program Files\Python38\lib\site-packages\comtypes_init_.py", line 560, in func return self.Invoke(obj, memid, _invkind=1, *args, **kw) # DISPATCH_METHOD File "C:\Program Files\Python38\lib\site-packages\comtypes\automation.py", line 796, in Invoke raise COMError(hresult, text, _ctypes.COMError: (-2147352571, 'Type mismatch.', ('TypeError: Parameter 7', (1.0, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)))

I can't find much in terms of the documentation or from other sources as to how I am supposed to pass these parameters. The documentation says, "Arguments marked [out] or [out, retval] in the IDL are returned from a successful method call, in a tuple if there is more than one. If no [out] or [out, retval] arguments are present, the HRESULT returned by the method call is returned.", Which doesn't specify that you would need to call it any particular way.

Here is what the function looks like copy/pasted from oleview, generated from the IDL:

HRESULT RunFrozPredict(
                        [out] double* Cycles, 
                        [out] double* finalC, 
                        [out] double* finalKc, 
                        [out] double* finalA, 
                        [out] double* finalKa, 
                        [out] double* finalCt, 
                        [out] double* finalKct, 
                        [out, retval] short* retval);

And here is my python code:

from comtypes.client import CreateObject
from comtypes.client import GetEvents
from comtypes.client import PumpEvents
from comtypes import COMError
import inspect

af = CreateObject("Afgrow.Application")
af.Visible = True

af.Model = 2030 # Double Through Crack at Hole, refer to Afgrow Dispatch Interface Manual, or Afgrow F1 Help.
af.CrackLengthC = 0.09
af.SpecimenHoleDia = 0.4
af.ConstAmplitudeSpectrum(0.0)


Cycles = 1.0
finalC = 0.9
finalKc = 0.9
finalA = 0.9
finalKa = 0.9
finalCt = 0.9
finalKct = 0.9
res = 1

retTuple = af.RunFrozPredict(Cycles, finalC, finalKc, finalA, finalKa, finalCt, finalKct)

af.Quit()
print("end")

Thanks for any help, I'm not having much luck online.

JimmyBLambert avatar Oct 28 '20 20:10 JimmyBLambert

Hi @JimmyBLambert what is printed before During handling of the above exception, another exception occurred:?

This is more important, because the second part of traceback you posted is not a root cause, but a handling code.

vasily-v-ryabov avatar Dec 25 '20 16:12 vasily-v-ryabov

Thanks for the reply @vasily-v-ryabov, here is the full traceback including the original exception:

Traceback (most recent call last): File "C:\Program Files\Python38\lib\site-packages\comtypes\automation.py", line 775, in Invoke self.__com_Invoke(dispid, riid_null, _lcid, _invkind, byref(dp), _ctypes.COMError: (-2147352571, 'Type mismatch.', (None, None, None, 0, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "c:/Users/jlambert/source/repos/COM-local/Python/RunFrozPredict.py", line 31, in retTuple = af.RunFrozPredict(Cycles, finalC, finalKc, finalA, finalKa, finalCt, finalKct)
File "C:\Program Files\Python38\lib\site-packages\comtypes_init_.py", line 556, in func
return self.Invoke(obj, memid, _invkind=1, *args, **kw) # DISPATCH_METHOD File "C:\Program Files\Python38\lib\site-packages\comtypes\automation.py", line 797, in Invoke raise COMError(hresult, text, _ctypes.COMError: (-2147352571, 'Type mismatch.', ('TypeError: Parameter 7', (1.0, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9)))

JimmyBLambert avatar Dec 28 '20 14:12 JimmyBLambert