struct of struct as return value
Nyirő Gergő <[email protected]> Thu, 8 Aug 2013 15:31:38 +0200
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CAOch2UesF+BDtiTSJwaDB-DMb=CX4ih9h9kMwqn1_oea+DQF4g@mail.gmail.com> |
Hello,
I have a problem with structure as a return value, structure of structure
to be correct.
Here is c snippet that demonstrate the interface
```c
#ifndef foo_h
#define foo_h
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#define C_CALL __cdecl
#else
#define DECLDIR __declspec(dllimport)
#define C_CALL
#endif
#pragma pack(1)
typedef struct in{
float a;
float b;
} In;
typedef struct data{
float c;
float d;
} Data;
typedef struct out{
Data front;
Data rear;
} Out;
#pragma pack(0)
DECLDIR Out C_CALL calc(In in);
#endif
```
I compile it with mingw32-gcc:
$ i686-pc-mingw32-gcc-4.5.2 -c foo.c -D DLL_EXPORT -Wall
$ i686-pc-mingw32-gcc-4.5.2 -shared -o foo.dll foo.o
-Wl,--out-implib,libfoo.a,--output-def,foo.def
That should be the python wrapper:
```python
from ctypes import Structure, c_float, CDLL
class In(Structure):
_fields_ = [
('a', c_float),
('b', c_float),
]
_pack_ = 1
class Data(Structure):
_fields_ = [
('c', c_float),
('d', c_float),
]
_pack_ = 1
class Out(Structure):
_fields_ = [
('front', Data),
('rear', Data),
]
_pack_ = 1
lib = CDLL('foo.dll')
lib.calc.argtypes = In,
lib.calc.restype = Out
data = In(1.0, 2.0)
result = lib.calc(data)
```
The wrapper worked until, I used only a plain structure as a return value,
and it is working if I use pointer the get back the data, but unfortunatelly
I cannot change on the interface.
How should I handle this output?
thanks for your advice in advance,
gergo
------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead.
Download for free and get started troubleshooting in minutes.
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users