admin管理员组文章数量:1395744
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:\minife3\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.
本文标签:
版权声明:本文标题:windows - How can I match python function to COM DLLs bc when the function passes 'self', the number of argument 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744773178a2624465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论