使用python中的ctypes调用函数时,dll返回一个对象。在
它返回以下值-假设它被命名为ReturnO;print(ReturnO)给出了以下结果:
(63484, <DLLname.ClassName object at 0x09D35670>)
对象应该返回参数;它们的名称是:Paramater_1、Parameter_2,依此类推。我的问题是,如何访问参数_1、参数_2等中的值
如果我按以下方式打印
^{pr2}$我得到以下信息
Field type=c_float_Array_5, ofs=49483, size=20
Field type=c_float_Array_5, ofs=49503, size=20
现在,我如何获得这个数组中的值。dotValue(.value)不起作用。在
感谢你的帮助。谢谢您。在
---------------添加/修改-----------以下---------
以下是代码;感谢您的帮助:
num1=10.1234
int1=10
num11=1.1111
str1=”abcd”
ret=GetOutput_Main(int1,num1,num11,str1)
class ClassName(ctypes.Structure):
_pack_ = 1
_fields_ = [("parameter_1", ctypes.c_float * 5),
("parameter_2", ctypes.c_float * 5)]
def GetOutput_Main (int2,num2,num22,str2):
lib = ctypes.WinDLL("mydllname.dll")
prototype = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ClassName))
paramflags = (1, "int2",), (1, "num2",), (2, "num22",), (2, "str2",),
Getoutput_Sub = prototype(("Getoutput", lib), paramflags))
ret = Getoutput_Sub(int2,num2)
print(ret) #gives the details of the object
print(str2.parameter_1) #gives the details of array
打印(ret)给了我:
(63484, <mydllname.ClassName object at 0x09D35670>)
如果我打印(str2),我会得到以下结果:
<class 'mydllname.ClassName'>
而print(str2.parameter_1)给了我
Field type=c_float_Array_5, ofs=49483, size=20
我在找办法把东西拆开,谢谢。在
如果有,num22是大小
UnpackedST = struct.unpack(str2,num22)
我得到以下错误
Struct() argument 1 must be a str or bytes object, not _ctypes.PyCStructType
如果您有一个cTypesFloat数组,那么有多种方法来获取每个float。在
示例:
我们从一个简单的python float列表开始,只是为了演示:
从列表中创建ctypes浮点数组:
^{pr2}$ctypes数组可订阅:
也可以在它们上使用for循环:
由于ctypes数组是可订阅的,因此可以从中获取python列表:
根据您的描述,您似乎有一个类似于以下内容的C函数:
您的
^{pr2}$paramflags
参数表示两个输入(类型1)和两个返回值(类型2)。只需传递两个必需的输入值,然后索引第二个返回值以访问其成员。使用list()
将数组转换为Python列表:输出:
相关问题 更多 >
编程相关推荐