Accessing contents of an object returned by DLL using ctypes in Python
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
python object dll ctypes
add a comment |
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
python object dll ctypes
You'll have to provide more than that (code, Python, and probably from library), basically, everything described in [SO]: How to create a Minimal, Complete, and Verifiable example (mcve).
– CristiFati
Nov 19 '18 at 18:16
After reading the code and researching online, I see that the object is packed and I have to unpack it. I struggling how to unpack it. Will keep you posted. Thank you.
– PK is using Python
Nov 19 '18 at 22:06
Please provide the C function prototype and structure definition as well.
– Mark Tolonen
Nov 20 '18 at 21:36
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you
– PK is using Python
Nov 21 '18 at 9:13
add a comment |
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
python object dll ctypes
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
python object dll ctypes
python object dll ctypes
edited Nov 20 '18 at 3:27
PK is using Python
asked Nov 19 '18 at 2:16
PK is using PythonPK is using Python
84
84
You'll have to provide more than that (code, Python, and probably from library), basically, everything described in [SO]: How to create a Minimal, Complete, and Verifiable example (mcve).
– CristiFati
Nov 19 '18 at 18:16
After reading the code and researching online, I see that the object is packed and I have to unpack it. I struggling how to unpack it. Will keep you posted. Thank you.
– PK is using Python
Nov 19 '18 at 22:06
Please provide the C function prototype and structure definition as well.
– Mark Tolonen
Nov 20 '18 at 21:36
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you
– PK is using Python
Nov 21 '18 at 9:13
add a comment |
You'll have to provide more than that (code, Python, and probably from library), basically, everything described in [SO]: How to create a Minimal, Complete, and Verifiable example (mcve).
– CristiFati
Nov 19 '18 at 18:16
After reading the code and researching online, I see that the object is packed and I have to unpack it. I struggling how to unpack it. Will keep you posted. Thank you.
– PK is using Python
Nov 19 '18 at 22:06
Please provide the C function prototype and structure definition as well.
– Mark Tolonen
Nov 20 '18 at 21:36
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you
– PK is using Python
Nov 21 '18 at 9:13
You'll have to provide more than that (code, Python, and probably from library), basically, everything described in [SO]: How to create a Minimal, Complete, and Verifiable example (mcve).
– CristiFati
Nov 19 '18 at 18:16
You'll have to provide more than that (code, Python, and probably from library), basically, everything described in [SO]: How to create a Minimal, Complete, and Verifiable example (mcve).
– CristiFati
Nov 19 '18 at 18:16
After reading the code and researching online, I see that the object is packed and I have to unpack it. I struggling how to unpack it. Will keep you posted. Thank you.
– PK is using Python
Nov 19 '18 at 22:06
After reading the code and researching online, I see that the object is packed and I have to unpack it. I struggling how to unpack it. Will keep you posted. Thank you.
– PK is using Python
Nov 19 '18 at 22:06
Please provide the C function prototype and structure definition as well.
– Mark Tolonen
Nov 20 '18 at 21:36
Please provide the C function prototype and structure definition as well.
– Mark Tolonen
Nov 20 '18 at 21:36
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you
– PK is using Python
Nov 21 '18 at 9:13
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you
– PK is using Python
Nov 21 '18 at 9:13
add a comment |
2 Answers
2
active
oldest
votes
Given your description it looks like you have a C function similar to the following:
#include <inttypes.h>
#define API __declspec(dllexport)
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;
}
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. Use list()
to convert the array to Python lists:
from ctypes import *
class ClassName(Structure):
_fields_ = [('parameter_1',c_float * 5),
('parameter_2',c_float * 5)]
lib = WinDLL('test')
prototype = WINFUNCTYPE(c_int,c_int,c_uint32,POINTER(c_uint32),POINTER(ClassName))
paramflags = (1,'int2'),(1,'num2'),(2,'num22'),(2,'str2')
Getoutput = prototype(('Getoutput',lib),paramflags)
ret = Getoutput(10,11)
print(ret)
print(ret[1].parameter_1)
print(ret[1].parameter_2)
print(list(ret[1].parameter_1))
print(list(ret[1].parameter_2))
Output:
(21, <__main__.ClassName object at 0x000001DA3A9139C8>)
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
[0.0, 0.5, 1.0, 1.5, 2.0]
[0.0, 0.25, 0.5, 0.75, 1.0]
add a comment |
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:
>>> python_float_list = [1.5, 2.5, 3.5, 4.5, 5.5]
Create a ctypes float array from the list:
>>> import ctypes
>>> c_float_array = (ctypes.c_float * 5)(*python_float_list)
>>> c_float_array
<__main__.c_float_Array_5 object at 0x000001D6D9A66A48>
ctypes arrays are subscriptable:
>>> c_float_array[0]
1.5
>>> c_float_array[1]
2.5
You can use a for loop on them too:
>>> for f in c_float_array:
print(f)
1.5
2.5
3.5
4.5
5.5
As the ctypes arrays are subscriptable, you can get a python list back from them:
>>> list(c_float_array)
[1.5, 2.5, 3.5, 4.5, 5.5]
its a field (Field type=c_float_Array_5, ofs=49483, size=20) and not a direct array. If I just try print(str2.parameter_1[0]), it says that the Field is not subscript-able. The error is:TypeError: '_ctypes.CField' object is not subscriptable
– PK is using Python
Nov 20 '18 at 7:12
@PK79: if it's a field, then what you have is a type, not an instance of the type. TryClassName.parameter_1
in a shell and you have the same result. I suspect this is because the way you prototyped the function is kinda strange. you should useargtypes
andrestype
on the function prototype.
– Neitsa
Nov 20 '18 at 7:30
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you.
– PK is using Python
Nov 20 '18 at 7:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367453%2faccessing-contents-of-an-object-returned-by-dll-using-ctypes-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Given your description it looks like you have a C function similar to the following:
#include <inttypes.h>
#define API __declspec(dllexport)
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;
}
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. Use list()
to convert the array to Python lists:
from ctypes import *
class ClassName(Structure):
_fields_ = [('parameter_1',c_float * 5),
('parameter_2',c_float * 5)]
lib = WinDLL('test')
prototype = WINFUNCTYPE(c_int,c_int,c_uint32,POINTER(c_uint32),POINTER(ClassName))
paramflags = (1,'int2'),(1,'num2'),(2,'num22'),(2,'str2')
Getoutput = prototype(('Getoutput',lib),paramflags)
ret = Getoutput(10,11)
print(ret)
print(ret[1].parameter_1)
print(ret[1].parameter_2)
print(list(ret[1].parameter_1))
print(list(ret[1].parameter_2))
Output:
(21, <__main__.ClassName object at 0x000001DA3A9139C8>)
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
[0.0, 0.5, 1.0, 1.5, 2.0]
[0.0, 0.25, 0.5, 0.75, 1.0]
add a comment |
Given your description it looks like you have a C function similar to the following:
#include <inttypes.h>
#define API __declspec(dllexport)
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;
}
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. Use list()
to convert the array to Python lists:
from ctypes import *
class ClassName(Structure):
_fields_ = [('parameter_1',c_float * 5),
('parameter_2',c_float * 5)]
lib = WinDLL('test')
prototype = WINFUNCTYPE(c_int,c_int,c_uint32,POINTER(c_uint32),POINTER(ClassName))
paramflags = (1,'int2'),(1,'num2'),(2,'num22'),(2,'str2')
Getoutput = prototype(('Getoutput',lib),paramflags)
ret = Getoutput(10,11)
print(ret)
print(ret[1].parameter_1)
print(ret[1].parameter_2)
print(list(ret[1].parameter_1))
print(list(ret[1].parameter_2))
Output:
(21, <__main__.ClassName object at 0x000001DA3A9139C8>)
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
[0.0, 0.5, 1.0, 1.5, 2.0]
[0.0, 0.25, 0.5, 0.75, 1.0]
add a comment |
Given your description it looks like you have a C function similar to the following:
#include <inttypes.h>
#define API __declspec(dllexport)
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;
}
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. Use list()
to convert the array to Python lists:
from ctypes import *
class ClassName(Structure):
_fields_ = [('parameter_1',c_float * 5),
('parameter_2',c_float * 5)]
lib = WinDLL('test')
prototype = WINFUNCTYPE(c_int,c_int,c_uint32,POINTER(c_uint32),POINTER(ClassName))
paramflags = (1,'int2'),(1,'num2'),(2,'num22'),(2,'str2')
Getoutput = prototype(('Getoutput',lib),paramflags)
ret = Getoutput(10,11)
print(ret)
print(ret[1].parameter_1)
print(ret[1].parameter_2)
print(list(ret[1].parameter_1))
print(list(ret[1].parameter_2))
Output:
(21, <__main__.ClassName object at 0x000001DA3A9139C8>)
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
[0.0, 0.5, 1.0, 1.5, 2.0]
[0.0, 0.25, 0.5, 0.75, 1.0]
Given your description it looks like you have a C function similar to the following:
#include <inttypes.h>
#define API __declspec(dllexport)
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;
}
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. Use list()
to convert the array to Python lists:
from ctypes import *
class ClassName(Structure):
_fields_ = [('parameter_1',c_float * 5),
('parameter_2',c_float * 5)]
lib = WinDLL('test')
prototype = WINFUNCTYPE(c_int,c_int,c_uint32,POINTER(c_uint32),POINTER(ClassName))
paramflags = (1,'int2'),(1,'num2'),(2,'num22'),(2,'str2')
Getoutput = prototype(('Getoutput',lib),paramflags)
ret = Getoutput(10,11)
print(ret)
print(ret[1].parameter_1)
print(ret[1].parameter_2)
print(list(ret[1].parameter_1))
print(list(ret[1].parameter_2))
Output:
(21, <__main__.ClassName object at 0x000001DA3A9139C8>)
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
<__main__.c_float_Array_5 object at 0x000001DA3A790EC8>
[0.0, 0.5, 1.0, 1.5, 2.0]
[0.0, 0.25, 0.5, 0.75, 1.0]
answered Nov 21 '18 at 0:19
Mark TolonenMark Tolonen
91.9k12109176
91.9k12109176
add a comment |
add a comment |
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:
>>> python_float_list = [1.5, 2.5, 3.5, 4.5, 5.5]
Create a ctypes float array from the list:
>>> import ctypes
>>> c_float_array = (ctypes.c_float * 5)(*python_float_list)
>>> c_float_array
<__main__.c_float_Array_5 object at 0x000001D6D9A66A48>
ctypes arrays are subscriptable:
>>> c_float_array[0]
1.5
>>> c_float_array[1]
2.5
You can use a for loop on them too:
>>> for f in c_float_array:
print(f)
1.5
2.5
3.5
4.5
5.5
As the ctypes arrays are subscriptable, you can get a python list back from them:
>>> list(c_float_array)
[1.5, 2.5, 3.5, 4.5, 5.5]
its a field (Field type=c_float_Array_5, ofs=49483, size=20) and not a direct array. If I just try print(str2.parameter_1[0]), it says that the Field is not subscript-able. The error is:TypeError: '_ctypes.CField' object is not subscriptable
– PK is using Python
Nov 20 '18 at 7:12
@PK79: if it's a field, then what you have is a type, not an instance of the type. TryClassName.parameter_1
in a shell and you have the same result. I suspect this is because the way you prototyped the function is kinda strange. you should useargtypes
andrestype
on the function prototype.
– Neitsa
Nov 20 '18 at 7:30
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you.
– PK is using Python
Nov 20 '18 at 7:51
add a comment |
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:
>>> python_float_list = [1.5, 2.5, 3.5, 4.5, 5.5]
Create a ctypes float array from the list:
>>> import ctypes
>>> c_float_array = (ctypes.c_float * 5)(*python_float_list)
>>> c_float_array
<__main__.c_float_Array_5 object at 0x000001D6D9A66A48>
ctypes arrays are subscriptable:
>>> c_float_array[0]
1.5
>>> c_float_array[1]
2.5
You can use a for loop on them too:
>>> for f in c_float_array:
print(f)
1.5
2.5
3.5
4.5
5.5
As the ctypes arrays are subscriptable, you can get a python list back from them:
>>> list(c_float_array)
[1.5, 2.5, 3.5, 4.5, 5.5]
its a field (Field type=c_float_Array_5, ofs=49483, size=20) and not a direct array. If I just try print(str2.parameter_1[0]), it says that the Field is not subscript-able. The error is:TypeError: '_ctypes.CField' object is not subscriptable
– PK is using Python
Nov 20 '18 at 7:12
@PK79: if it's a field, then what you have is a type, not an instance of the type. TryClassName.parameter_1
in a shell and you have the same result. I suspect this is because the way you prototyped the function is kinda strange. you should useargtypes
andrestype
on the function prototype.
– Neitsa
Nov 20 '18 at 7:30
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you.
– PK is using Python
Nov 20 '18 at 7:51
add a comment |
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:
>>> python_float_list = [1.5, 2.5, 3.5, 4.5, 5.5]
Create a ctypes float array from the list:
>>> import ctypes
>>> c_float_array = (ctypes.c_float * 5)(*python_float_list)
>>> c_float_array
<__main__.c_float_Array_5 object at 0x000001D6D9A66A48>
ctypes arrays are subscriptable:
>>> c_float_array[0]
1.5
>>> c_float_array[1]
2.5
You can use a for loop on them too:
>>> for f in c_float_array:
print(f)
1.5
2.5
3.5
4.5
5.5
As the ctypes arrays are subscriptable, you can get a python list back from them:
>>> list(c_float_array)
[1.5, 2.5, 3.5, 4.5, 5.5]
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:
>>> python_float_list = [1.5, 2.5, 3.5, 4.5, 5.5]
Create a ctypes float array from the list:
>>> import ctypes
>>> c_float_array = (ctypes.c_float * 5)(*python_float_list)
>>> c_float_array
<__main__.c_float_Array_5 object at 0x000001D6D9A66A48>
ctypes arrays are subscriptable:
>>> c_float_array[0]
1.5
>>> c_float_array[1]
2.5
You can use a for loop on them too:
>>> for f in c_float_array:
print(f)
1.5
2.5
3.5
4.5
5.5
As the ctypes arrays are subscriptable, you can get a python list back from them:
>>> list(c_float_array)
[1.5, 2.5, 3.5, 4.5, 5.5]
answered Nov 20 '18 at 6:58
NeitsaNeitsa
4,76311631
4,76311631
its a field (Field type=c_float_Array_5, ofs=49483, size=20) and not a direct array. If I just try print(str2.parameter_1[0]), it says that the Field is not subscript-able. The error is:TypeError: '_ctypes.CField' object is not subscriptable
– PK is using Python
Nov 20 '18 at 7:12
@PK79: if it's a field, then what you have is a type, not an instance of the type. TryClassName.parameter_1
in a shell and you have the same result. I suspect this is because the way you prototyped the function is kinda strange. you should useargtypes
andrestype
on the function prototype.
– Neitsa
Nov 20 '18 at 7:30
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you.
– PK is using Python
Nov 20 '18 at 7:51
add a comment |
its a field (Field type=c_float_Array_5, ofs=49483, size=20) and not a direct array. If I just try print(str2.parameter_1[0]), it says that the Field is not subscript-able. The error is:TypeError: '_ctypes.CField' object is not subscriptable
– PK is using Python
Nov 20 '18 at 7:12
@PK79: if it's a field, then what you have is a type, not an instance of the type. TryClassName.parameter_1
in a shell and you have the same result. I suspect this is because the way you prototyped the function is kinda strange. you should useargtypes
andrestype
on the function prototype.
– Neitsa
Nov 20 '18 at 7:30
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you.
– PK is using Python
Nov 20 '18 at 7:51
its a field (Field type=c_float_Array_5, ofs=49483, size=20) and not a direct array. If I just try print(str2.parameter_1[0]), it says that the Field is not subscript-able. The error is:TypeError: '_ctypes.CField' object is not subscriptable
– PK is using Python
Nov 20 '18 at 7:12
its a field (Field type=c_float_Array_5, ofs=49483, size=20) and not a direct array. If I just try print(str2.parameter_1[0]), it says that the Field is not subscript-able. The error is:TypeError: '_ctypes.CField' object is not subscriptable
– PK is using Python
Nov 20 '18 at 7:12
@PK79: if it's a field, then what you have is a type, not an instance of the type. Try
ClassName.parameter_1
in a shell and you have the same result. I suspect this is because the way you prototyped the function is kinda strange. you should use argtypes
and restype
on the function prototype.– Neitsa
Nov 20 '18 at 7:30
@PK79: if it's a field, then what you have is a type, not an instance of the type. Try
ClassName.parameter_1
in a shell and you have the same result. I suspect this is because the way you prototyped the function is kinda strange. you should use argtypes
and restype
on the function prototype.– Neitsa
Nov 20 '18 at 7:30
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you.
– PK is using Python
Nov 20 '18 at 7:51
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you.
– PK is using Python
Nov 20 '18 at 7:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367453%2faccessing-contents-of-an-object-returned-by-dll-using-ctypes-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You'll have to provide more than that (code, Python, and probably from library), basically, everything described in [SO]: How to create a Minimal, Complete, and Verifiable example (mcve).
– CristiFati
Nov 19 '18 at 18:16
After reading the code and researching online, I see that the object is packed and I have to unpack it. I struggling how to unpack it. Will keep you posted. Thank you.
– PK is using Python
Nov 19 '18 at 22:06
Please provide the C function prototype and structure definition as well.
– Mark Tolonen
Nov 20 '18 at 21:36
T1=ret[1].parameter_1 and then accessing T1[0], T1[1]...T1[4] is working . Thank you
– PK is using Python
Nov 21 '18 at 9:13