473,287 Members | 3,319 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,287 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 8364
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.