dll在使用python中的ctype调用函数时返回一个对象。
它返回以下内容--假设它被命名为ReturnO;print(ReturnO)给出了以下内容:
(63484, <DLLname.ClassName object at 0x09D35670>)
对象应该返回参数;它们的名称是: Paramater_1、Parameter_2等等。我的问题是,如何访问Parameter_1、Parameter_2等中的值。
如果我按以下方式打印
print(ClassName.Parameter_1)
print(ClassName.Parameter_2)
我得到以下信息
Field type=c_float_Array_5, ofs=49483, size=20
Field type=c_float_Array_5, ofs=49503, size=20
现在,我如何获得这个数组中的值。dotValue (.value)不工作。
感谢你的帮助。谢谢。
----------------ADDED/MODIFIED----------BELOW
下面是代码,感谢您的帮助:
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'>
打印(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
发布于 2018-11-20 16:19:37
根据您的描述,您的C函数类似于以下内容:
test.c
#include <inttypes.h>
#ifdef _WIN32
# define API __declspec(dllexport)
#else
# define API
#endif
struct ClassName {
float parameter_1[5];
float parameter_2[5];
};
API int __stdcall Getoutput(int a, uint32_t b, uint32_t* pc, struct ClassName* pd) {
int i;
*pc = a + b;
for(i = 0; i < 5; ++i) {
pd->parameter_1[i] = i * .5f;
pd->parameter_2[i] = i * .25f;
}
return a * b;
}
paramflags
参数指示两个输入(类型1)和两个返回值(类型2)。只需传递两个所需的输入值,并将结果存储在一个元组中。您可以在数组上调用list()
或切片来访问它们的元素。我提供了一个__repr__
函数,这样类在打印时就可以显示自己了。
test.py
import ctypes as ct
class ClassName(ct.Structure):
_fields_ = (('parameter_1', ct.c_float * 5),
('parameter_2', ct.c_float * 5))
def __repr__(self):
'''Return string describing how to print ClassName object.
'''
# Note that slicing an array produces a list of its elements.
return f'ClassName({self.parameter_1[:]}, {self.parameter_2[:]})'
lib = ct.WinDLL('./test')
prototype = ct.WINFUNCTYPE(ct.c_int, ct.c_int, ct.c_uint32, ct.POINTER(ct.c_uint32), ct.POINTER(ClassName))
paramflags = (1, 'int2'), (1, 'num2'), (2, 'num22'), (2, 'str2')
Getoutput = prototype(('Getoutput', lib), paramflags)
pc, pd = Getoutput(10, 11)
print(f'{pc=} {pd=}')
输出:
pc=21 pd=ClassName([0.0, 0.5, 1.0, 1.5, 2.0], [0.0, 0.25, 0.5, 0.75, 1.0])
发布于 2018-11-19 22:58:54
如果您有一个ctype浮点数数组,则有各种方法来获取每个浮点数。
示例:
我们从一个简单的python浮动列表开始,只是为了演示:
>>> python_float_list = [1.5, 2.5, 3.5, 4.5, 5.5]
从列表中创建一个ctype浮点数数组:
>>> import ctypes
>>> c_float_array = (ctypes.c_float * 5)(*python_float_list)
>>> c_float_array
<__main__.c_float_Array_5 object at 0x000001D6D9A66A48>
ctypes数组是可订阅的:
>>> c_float_array[0]
1.5
>>> c_float_array[1]
2.5
您也可以对它们使用for循环:
>>> for f in c_float_array:
print(f)
1.5
2.5
3.5
4.5
5.5
由于ctype数组是可订阅的,您可以从它们获得一个python列表:
>>> list(c_float_array)
[1.5, 2.5, 3.5, 4.5, 5.5]
https://stackoverflow.com/questions/53367453
复制