472,122 Members | 1,406 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 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 8060
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thomas Heller | last post: by
reply views Thread by Srijit Kumar Bhadra | last post: by
12 posts views Thread by p.lavarre | last post: by
2 posts views Thread by Eric | last post: by
3 posts views Thread by Chris AtLee | last post: by
3 posts views Thread by Andrew Lentvorski | last post: by

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.