0

I am writing a python program and need to interact with existing com dll objects written in C++. I can access the Dispatch interface of the dll using the comtypes module and get the correct function signature:

HRESULT FindFeatures([in, out] int* pint, [in] BSTR strDatabase, [in] BSTR strRules, [in] VARIANT Y, [in] VARIANT X, [in] double Param1, [in] double Param2, [in] double Param3, [in] int Param4,[out] VARIANT* FeaturesFound);

If I try to acccess the function by rescode = cls.FindFeatures(pointer(int(a)), "PathToDatabase", "PathToRules", np.ndarray(Y), np.ndarray(X), float(param1), float(param2), float(param3), int(1), np.zeros((20,))) I get an error code that 10 arguments expected, but 11 passed. And in the debugger it gets caught on the code in comtypes rescode = func(self, *args, **kw)

I thought maybe the comtypes wrapper is expecting it to put the output args as actual output: 'features_found = cls.FindFeatures(pointer(int(a)), "PathToDatabase", "PathToRules", np.ndarray(Y), np.ndarray(X), float(param1), float(param2), float(param3), int(1))'

it raises the following comtypes error

File "C:\miniforge3\envs\pywin\Lib\site-packages\comtypes\_memberspec.py", line 295, in call_with_inout
    rescode = func(self, *args, **kw)
              ^^^^^^^^^^^^^^^^^^^^^^^
_ctypes.COMError: (-2147024809, 'The parameter is incorrect.', (None, None, None, 0, None))

I am way out of my depth on this and could really use some guidance to help point me in the right direction. I'll post more of the code that I'm trying below in case I'm doing something wildly crazy.

import comtypes.client as cc
import comtypes
from comtypes.safearray import safearray_as_ndarray
from CustomLoaders import loadData
import ctypes
import numpy as np

#Weird work around to get the pointer to int into the function call and past type checking
int_type = ctypes.c_int
int_value = int_type(10)
ptr = ctypes.pointer(int_value)

#Get access to the DLL
find_featureslib = comtypes.GUID("{98B95959-2FF5-11D4-9F36-0000D11D8091}")
cc.GetModule((find_featureslib,1,0))
import comtypes.gen.COFINDFEATURESLib as FindFeaturesLib
feature_id = cc.CreateObject("COFINDFEATURES.FeatureId",None, None, FindFeaturesLib.IFeatureId)


#Get the data, x and y are 1D numpy arrays
x,y= loadData(r"PathToData.data")

#make buffer array for return data:
peaks_found = np.zeros(shape=(20,), dtype=np.int16)

with safearray_as_ndarray:
    rescode = feature_id.FindFeatures( ptr, strDataBasePath, strRulesPath,float(param1), float(param2), float(param3), int(1), peaks_found)

which I thought should match the function signature but it says I have the wrong number of arguments

or

with safearray_as_ndarray:
    peaks_found= feature_id.FindFeatures( ptr, strDataBasePath, strRulesPath,float(param1), float(param2), float(param3), int(1))

Which yields the comtypes error code mentioned above.

I'm using comtypes=1.4.10, numpy=1.26.4, python=3.11.11 I've tried a number of different things after googling my issue, reading the open issues on the comtypes github project, looking through the comtypes source, and combing StackOverflow, but it feels like I'm still fumbling in the dark.

2
  • Ideally the last parameter is annotated as [out, retval]. The error matches however, so try features = cls.FindFeatures(...10 parameters...) Commented Mar 11 at 20:17
  • @HansPassant Thanks! I did just try what you suggested. But it turns out I had already solved the problem(s). There was a conflict with the numpy version and using as safe array. I had tried various versions of numpy, but ended up converting the numpy array to a python array. Dumb but fine. But it still didn't work but it didn't error out either. Apparently the underlying dll's function had an undocumented ordering requirement on the x data array. In the end something like: int, peaks_found = feaureid.FindFeatures(...) did work. I had to read more into COM to understand the mapping Commented Apr 7 at 15:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.