Passing a packed C structure to a c module

P

Philippe Martin

Hi,

Is it possible to define a packed C structure in python and pass it to the c
module, or should the wrapper do that ?

Regards,

Philippe
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Philippe said:
Is it possible to define a packed C structure in python and pass it to the c
module, or should the wrapper do that ?

You can create a packed structure using string concatenation, and with
the help of the struct module. However, this gives you a string object
in the end, which you still need to pass into your C library.

It is better to fill the struct in the wrapper.

Regards,
Martin
 
P

Philippe Martin

Thanks,

It's a pretty big structure: painfull to pass each item as a param.

Regards,

Philippe
 
?

=?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=

Philippe said:
It's a pretty big structure: painfull to pass each item as a param.

So how else would you like to pass them? Define the API you want,
and then just implement it. It still shouldn't require to define
the layout in Python.

Regards,
Martin
 
P

Philippe Martin

Well,

The call actually is an IOCtl: depending on the control code, the structure
has a different format.

Although the number of control codes/structures is finite, it would make the
wrapper function fairly large.

You seem to think that building the structure from python would be a
mistake: why is that ?

PS: the wrapper also has to work under multiple OSs

Regards,

Philippe
 
?

=?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=

Philippe said:
The call actually is an IOCtl: depending on the control code, the structure
has a different format.

Ah. In that case, I recommend to use the ioctl module; you won't need a
C wrapper, then.
Although the number of control codes/structures is finite, it would make the
wrapper function fairly large.

*Some* code to handle this will be very large, regardless of how you
write it.
You seem to think that building the structure from python would be a
mistake: why is that ?

It's very error-prone. You have to match the C structure layout of the
compiler precisely, and you even have to get the alignment right,
something that can't be done reliably in Python.
PS: the wrapper also has to work under multiple OSs

That makes it worse: now you not only have to match the C compiler's
expectation on a single processor, but on different ones.

Perhaps you should be using ctypes.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top