473,385 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

[ctypes] convert pointer to string?

In an earlier post, I was interested in passing a pointer to a structure to
fcntl.ioctl.

This works:

c = create_string_buffer (...)
args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value)
err = fcntl.ioctl(eos_fd, request, args)

Now to do the same with ctypes, I have one problem.

class eos_dl_args_t (Structure):
_fields_ = [("length", c_ulong),
("data", c_void_p)]
....
args = eos_dl_args_t()
args_p = cast(pointer(args), c_void_ptr).value
fcntl.ioctl(fd, request, args_p) <<< May fail here

That last may fail, because .value creates a long int, and ioctl needs a
string.

How can I convert my ctypes structure to a string? It _cannot_ be a c-style
0-terminate string, because the structure may contain '0' values.

Jun 27 '08 #1
4 8389
Neal Becker napisał(a):
In an earlier post, I was interested in passing a pointer to a structure to
fcntl.ioctl.

This works:

c = create_string_buffer (...)
args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value)
err = fcntl.ioctl(eos_fd, request, args)

Now to do the same with ctypes, I have one problem.

class eos_dl_args_t (Structure):
_fields_ = [("length", c_ulong),
("data", c_void_p)]
...
args = eos_dl_args_t()
args_p = cast(pointer(args), c_void_ptr).value
fcntl.ioctl(fd, request, args_p) <<< May fail here

That last may fail, because .value creates a long int, and ioctl needs a
string.

How can I convert my ctypes structure to a string? It _cannot_ be a c-style
0-terminate string, because the structure may contain '0' values.
Hello. I'm not completely sure what your problem is ("may fail" is not
a good description. Does it fail or does it not?). If you want a
pointer to a structure as the third argument to ioctl, this would be
imho the easiest way to do it with ctypes:
class eos_dl_args_t(Structure):
_fields_ = [("length", c_ulong), ("data", c_void_p)]
args = eos_dl_args_t()
fcntl.ioctl(fd, request, byref(args))
Regards,
Marek
Jun 27 '08 #2
Neal Becker schrieb:
In an earlier post, I was interested in passing a pointer to a structure to
fcntl.ioctl.

This works:

c = create_string_buffer (...)
args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value)
err = fcntl.ioctl(eos_fd, request, args)

Now to do the same with ctypes, I have one problem.

class eos_dl_args_t (Structure):
_fields_ = [("length", c_ulong),
("data", c_void_p)]
...
args = eos_dl_args_t()
args_p = cast(pointer(args), c_void_ptr).value
fcntl.ioctl(fd, request, args_p) <<< May fail here

That last may fail, because .value creates a long int, and ioctl needs a
string.

How can I convert my ctypes structure to a string? It _cannot_ be a c-style
0-terminate string, because the structure may contain '0' values.
ctypes instances support the buffer interface, and you can convert the buffer
contents into a string:
>>from ctypes import *
c_int(42)
c_long(42)
>>buffer(c_int(42))
<read-only buffer for 0x00AE7260, size -1, offset 0 at 0x00B06040>
>>buffer(c_int(42))[:]
'*\x00\x00\x00'
>>>
Thomas
Jun 27 '08 #3
ma*********@wp.pl wrote:
Neal Becker napisaƂ(a):
>In an earlier post, I was interested in passing a pointer to a structure
to fcntl.ioctl.

This works:

c = create_string_buffer (...)
args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value)
err = fcntl.ioctl(eos_fd, request, args)

Now to do the same with ctypes, I have one problem.

class eos_dl_args_t (Structure):
_fields_ = [("length", c_ulong),
("data", c_void_p)]
...
args = eos_dl_args_t()
args_p = cast(pointer(args), c_void_ptr).value
fcntl.ioctl(fd, request, args_p) <<< May fail here

That last may fail, because .value creates a long int, and ioctl needs a
string.

How can I convert my ctypes structure to a string? It _cannot_ be a
c-style 0-terminate string, because the structure may contain '0' values.

Hello. I'm not completely sure what your problem is ("may fail" is not
a good description. Does it fail or does it not?).
May fail is exactly what I meant. As written, ioctl will accept a 3rd arg
that is either a string or int. In this case, it is int. If the address
fits in an int it works, but if the address (which is a long) doesn't fit
it fails (gives an overflow error). Seems to be random (as well as
platform dependent).
Jun 27 '08 #4
ma*********@wp.pl wrote:
Neal Becker napisaƂ(a):
>In an earlier post, I was interested in passing a pointer to a structure
to fcntl.ioctl.

This works:

c = create_string_buffer (...)
args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value)
err = fcntl.ioctl(eos_fd, request, args)

Now to do the same with ctypes, I have one problem.

class eos_dl_args_t (Structure):
_fields_ = [("length", c_ulong),
("data", c_void_p)]
...
args = eos_dl_args_t()
args_p = cast(pointer(args), c_void_ptr).value
fcntl.ioctl(fd, request, args_p) <<< May fail here

That last may fail, because .value creates a long int, and ioctl needs a
string.

How can I convert my ctypes structure to a string? It _cannot_ be a
c-style 0-terminate string, because the structure may contain '0' values.

Hello. I'm not completely sure what your problem is ("may fail" is not
a good description. Does it fail or does it not?). If you want a
pointer to a structure as the third argument to ioctl, this would be
imho the easiest way to do it with ctypes:
>class eos_dl_args_t(Structure):
_fields_ = [("length", c_ulong), ("data", c_void_p)]
args = eos_dl_args_t()
fcntl.ioctl(fd, request, byref(args))
Seems to be a problem with that:
---59 err = fcntl.ioctl(eos_fd, request, byref(args))
60

TypeError: an integer is required
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
3
by: Lenard Lindstrom | last post by:
Posted in a previous thread was some Python code for accessing Window's Simple MAPI api using the ctypes module. http://groups-beta.google.com/group/comp.lang.python/msg/56fa74cdba9b7be9 This...
0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
12
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv,...
2
by: Eric | last post by:
Hi, I am currently dealing with ctypes, interfacing with winscard libbrary (for smart card access). Several APIs (e.g. SCardListReaderGroupsW ) take a pointer to an unicode string as a...
3
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the...
3
by: waldek | last post by:
Hi, I'm using C dll with py module and wanna read value (buffer of bytes) returned in py callback as parameter passed to dll function. -------------------------------------------------- def ...
3
by: Andrew Lentvorski | last post by:
Basically, I'd like to use the ctypes module as a much more descriptive "struct" module. Is there a way to take a ctypes.Structure-based class and convert it to/from a binary string? Thanks,...
1
by: patelss23 | last post by:
Hello All Experts, I am quite new to Ctypes. I am using one c library and writing python bindings for it. I need to pass a character pointer to one function. I am reading one mp3 file and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.