Hello!
I wanted to get the full contents of a character array stored in a
struct, i.e.
_fields_ = [...("array", c_char * 12)...]
however, ctypes seems to try to return struct.array as a Python string
rather than a character array, and stops as soon as it encounters a
null within the character array.
I ended up having to define a dummy struct
class dummystruct(Str ucture):
_fields_ = []
and declare array as:
("array", dummystruct)
then use string_at(byref (struct.array), 12).
Is this really the best way of doing it? Is there no better way to
work around ctypes 'guess what you want' behaviour?
Thanks in advance,
Rodrigo 2 10168
On Sep 12, 6:38*pm, overdrig...@gma il.com wrote:
Hello!
I wanted to get the full contents of a character array stored in a
struct, i.e.
_fields_ = [...("array", c_char * 12)...]
however, ctypes seems to try to return struct.array as a Python string
rather than a character array, and stops as soon as it encounters a
null within the character array.
I ended up having to define a dummy struct
class dummystruct(Str ucture):
* * _fields_ = []
and declare array as:
("array", dummystruct)
then use string_at(byref (struct.array), 12).
Is this really the best way of doing it? Is there no better way to
work around ctypes 'guess what you want' behaviour?
Thanks in advance,
Rodrigo
Rodrigo,
If you have the option to change your declaration to c_byte* 12, you
have more options. This example prints the null character you wanted.
from ctypes import *
import struct
class S( Structure ):
_fields_= [
( 'array', c_byte* 12 )
]
s= S()
#initialize
struct.pack_int o( '7s', s.array, 0, 'abc\x00def' )
#prototype and call PyString_FromSt ringAndSize
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ), c_size_t )
PyString_FromSt ringAndSize= prototype( ( "PyString_FromS tringAndSize",
pythonapi ) )
x= PyString_FromSt ringAndSize( s.array, 12 )
print repr( x )
#prototype and call PyString_FromSt ring for contrast
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ) )
PyString_FromSt ring= prototype( ( "PyString_FromS tring", pythonapi ) )
x= PyString_FromSt ring( s.array )
print repr( x )
/Output:
'abc\x00def\x00 \x00\x00\x00\x0 0'
'abc'
On Sep 13, 6:45*am, "Aaron \"Castironpi \" Brady"
<castiro...@gma il.comwrote:
On Sep 12, 6:38*pm, overdrig...@gma il.com wrote:
Hello!
I wanted to get the full contents of a character array stored in a
struct, i.e.
_fields_ = [...("array", c_char * 12)...]
however,ctypess eems to try to return struct.array as a Python string
rather than a character array, and stops as soon as it encounters a
null within the character array.
I ended up having to define a dummy struct
class dummystruct(Str ucture):
* * _fields_ = []
and declare array as:
("array", dummystruct)
then use string_at(byref (struct.array), 12).
Is this really the best way of doing it? Is there no better way to
work aroundctypes'gu ess what you want' behaviour?
Thanks in advance,
Rodrigo
Rodrigo,
If you have the option to change your declaration to c_byte* 12, you
have more options. *This example prints the null character you wanted.
fromctypesimpor t *
import struct
class S( Structure ):
* * _fields_= [
* * * * ( 'array', c_byte* 12 )
* * ]
s= S()
#initialize
struct.pack_int o( '7s', s.array, 0, 'abc\x00def' )
#prototype and call PyString_FromSt ringAndSize
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ), c_size_t )
PyString_FromSt ringAndSize= prototype( ( "PyString_FromS tringAndSize",
pythonapi ) )
x= PyString_FromSt ringAndSize( s.array, 12 )
print repr( x )
#prototype and call PyString_FromSt ring for contrast
prototype= PYFUNCTYPE( py_object, POINTER( c_byte ) )
PyString_FromSt ring= prototype( ( "PyString_FromS tring", pythonapi ) )
x= PyString_FromSt ring( s.array )
print repr( x )
/Output:
'abc\x00def\x00 \x00\x00\x00\x0 0'
'abc'
Great - and thank you for replying so fast! I found that [chr(x) for x
in struct.array] with array as a c_byte * 12 did what I needed. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 functions exposed from dlls/shared libraries
and has extensive facilities to create, access and manipulate
|
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 SharedMemAccess_Mutex_ctypes.py
or SharedMemAccess_Mutex_win32all.py
|
by: Sanghyeon Seo |
last post by:
ctypes is a popular CPython extension planned for inclusion in Python
2.5. It is a foreign function interface library.
Homepage: http://starship.python.net/crew/theller/ctypes/
Documentation: http://docs.python.org/dev/lib/module-ctypes.html
I would like to announce a preliminary implementation of ctypes for
IronPython using .NET's P/Invoke machinery.
Code is here: http://sparcs.kaist.ac.kr/~tinuviel/fepy/lib/ctypes.py
|
by: chris.atlee |
last post by:
Hello,
I've been trying to write a PAM module using ctypes. In the
conversation
function (my_conv in the script below), you're passed in a
pam_response**
pointer. You're supposed to allocate an array of pam_response's and
set
the pointer's value to the new array. Then you fill in the array with
appropriate data.
|
by: luis |
last post by:
I'm using ctypes to call a fortran dll from python. I have no problems
passing integer and double arryas, but I have an error with str arrys.
For example:
.....
StringVector = c_char_p * len(id) # id is a list of strings
Id_dat=StringVector()
for i in range(len(Id)):
....Id_dat=id
| |
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 script below), you're passed in a
pam_response**
pointer. You're supposed to allocate an array of pam_response's and
set
|
by: moreati |
last post by:
Recently I discovered the re module doesn't support POSIX character
classes:
Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
None
So I thought I'd try out pcre through ctypes, to recreate pcredemo.c
in python. The c code is at:
|
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,
-a
|
by: dudeja.rajat |
last post by:
Hi,
I'm using the CTYPES module of python to load a dll. My dll contains a
Get_Version function as:
long __stdcall af1xEvdoRDll_GetVersion(long version);
This function accepts a long array of 4 elements.
Following is the python code I've written:
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |