我在Python中使用DLL函数时遇到了问题。我需要为我的应用程序使用一个用C编写的特定DLL。我不能修改DLL,但我有结构的定义。C结构是这样的:
typedef struct {
int length;
int* val;
//to access value at a row => val[row]
} Array1dInt, **Array1dIntHandle;
该函数的原型是:
void __declspec(dllexport) modifywithHandle(Array1dIntHandle);
我用Python定义了我的类,如下所示:
class Array1dInt(Structure):
_fields_=[("length",c_int),("val",c_int)]
def handle(self):
"""Return a pointer. Use this function when you need the Handle of the array structure"""
return addressof(pointer(self))
def __init__(self,vec):
"""init initialize an object of the class Array1dInt. Takes a python array as input"""
self.length=c_long(len(vec))
self.vector=np.ctypeslib.as_ctypes(np.array(vec))
self.val=c_long(addressof(self.vector))
当我想使用DLL函数时,我是这样做的:
Python_array=[0,1,2,3,4,5]
MyObject=Array1dInt(Python_array)
modstruct=DllLib.modifywithHandle
modstruct.argtypes=[c_int]
modstruct(MyObject.handle())
在其他东西中,我应该看到控制台中的长度和元素。DLL函数正确检索长度,但无法读取向量。我用指针或addressof尝试了不同的解决方案,但似乎没有任何效果。我注意到length和val没有转换为ctypes对象,而vector是。 假设DLL函数编码正确,我在Python中做错了什么? 谢谢你的帮助
目前没有回答
相关问题 更多 >
编程相关推荐