The dll returns an object on calling a function using ctypes in python.
It returns the following - say it is named as ReturnO; print(ReturnO) gives the following:
(63484, <DLLname.ClassName object at 0x09D35670>)
The object should return the parameters; their names are: Paramater_1, Parameter_2 and so on. My question is, how do i access the values in Parameter_1, Parameter_2 etc.
if i do a print as follows
print(ClassName.Parameter_1)
print(ClassName.Parameter_2)
i get the following
Field type=c_float_Array_5, ofs=49483, size=20
Field type=c_float_Array_5, ofs=49503, size=20
Now, how do I get the value in this array. dotValue (.value) does not work.
Appreciate you help. Thank you.
----------------ADDED/MODIFIED----------BELOW------------
below is the code; appreciate your help:
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
the print(ret) gives me:
(63484, <mydllname.ClassName object at 0x09D35670>)
if i do print(str2), I get the following:
<class 'mydllname.ClassName'>
and print(str2.parameter_1) gives me
Field type=c_float_Array_5, ofs=49483, size=20
i am looking for ways to unpack the object, thanks.
if I do, where num22 is the size
UnpackedST = struct.unpack(str2,num22)
i get the following error
Struct() argument 1 must be a str or bytes object, not _ctypes.PyCStructType
If you have a ctypes float array, there are various means to get each of the float.
Example:
We start with a simple python float list, just for the sake of the demo:
Create a ctypes float array from the list:
ctypes arrays are subscriptable:
You can use a for loop on them too:
As the ctypes arrays are subscriptable, you can get a python list back from them:
Given your description it looks like you have a C function similar to the following:
Your
paramflags
argument indicates two inputs (type 1) and two return values (type 2). Simply pass the two required input values, then index the second return value to access its members. Uselist()
to convert the array to Python lists:Output: