Hi,
I have the following code:
******************************************
from ctypes import *
g_lib = cdll.LoadLibrary("libc.so.6")
class Struct_Password(Structure):
"""
"""
_fields_ = [ ('name', c_char_p),
('code', c_char_p),
('date', c_long),
('min', c_long),
('max', c_long),
('warn', c_long),
('inact', c_long),
('expire', c_long),
('flag', c_ulong)
]
l_res = g_lib.getspnam('john')
l_struct = cast(l_res, POINTER( Struct_Password() ) )
print l_struct
******************************************
The exception I get is "unhashable type" - apparently cast only handles
simple types.
Is there a way to convert l_res to Struct_Password ?
Thanks,
hg 2 3168
hg wrote:
Hi,
I have the following code:
******************************************
from ctypes import *
g_lib = cdll.LoadLibrary("libc.so.6")
class Struct_Password(Structure):
"""
"""
_fields_ = [ ('name', c_char_p),
('code', c_char_p),
('date', c_long),
('min', c_long),
('max', c_long),
('warn', c_long),
('inact', c_long),
('expire', c_long),
('flag', c_ulong)
]
l_res = g_lib.getspnam('john')
l_struct = cast(l_res, POINTER( Struct_Password() ) )
l_struct = cast(l_res, POINTER( Struct_Password ) )
POINTER wants a ctypes type as an argument. Struct_Password() is a
Structure instance.
A better way to do it is define the return type for getspnam:
g_lib.getspnam.restype = POINTER( Struct_Password )
Now the function returns a structure pointer so the cast is unnecessary.
---
Lenard Lindstrom
<le***@telus.net>
Lenard Lindstrom wrote:
hg wrote:
>Hi,
I have the following code: ****************************************** from ctypes import * g_lib = cdll.LoadLibrary("libc.so.6") class Struct_Password(Structure): """
""" _fields_ = [ ('name', c_char_p), ('code', c_char_p), ('date', c_long), ('min', c_long), ('max', c_long), ('warn', c_long), ('inact', c_long), ('expire', c_long), ('flag', c_ulong) ] l_res = g_lib.getspnam('john') l_struct = cast(l_res, POINTER( Struct_Password() ) )
l_struct = cast(l_res, POINTER( Struct_Password ) )
POINTER wants a ctypes type as an argument. Struct_Password() is a
Structure instance.
A better way to do it is define the return type for getspnam:
g_lib.getspnam.restype = POINTER( Struct_Password )
Now the function returns a structure pointer so the cast is unnecessary.
---
Lenard Lindstrom
<le***@telus.net>
Many thanks,
hg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Melkor Ainur |
last post by:
Hello,
I'm attempting to build an interpreter for a pascal-like language.
Currently, I don't generate any assembly. Instead, I just build an
abstract syntax tree representing what I've parsed...
|
by: Gibby Koldenhof |
last post by:
Hiya,
Terrible subject but I haven't got a better term at the moment.
I've been building up my own library of functionality (all nice conforming
ISO C) for over 6 years and decided to adopt a...
|
by: Mikhail Teterin |
last post by:
Hello!
I'd like to have a variable of a pointer-to-function type. The two possible
values are of type (*)(FILE *) and (*)(void *). For example:
getter = straight ? fgetc : gzgetc;
nextchar...
|
by: Bilgehan.Balban |
last post by:
Hi,
Generally in my C code, I use the following convention to return
success or failure:
a) For functions that return a pointer, (allocation, filling-in a
structure etc.) return Null for all...
|
by: Fernando Barsoba |
last post by:
Hi all,
I'm trying to reassign the address of a pointer inside a function and
return it once the function has done its things... However, the address
returned is not the address I wanted to...
|
by: Macca |
last post by:
Hi,
I'm writing an application that will pass a large amount of data between
classes/functions.
In C++ it was more efficient to send a pointer to the object, e.g structure
rather than passing...
|
by: sofeng |
last post by:
The following link shows a chart I created about passing structures
among functions. Would you review it and tell me if it requires any
corrections?
...
|
by: Taras_96 |
last post by:
Hi all,
A poster at http://bytes.com/forum/thread60652.html implies that using
strtoupper in transform doesn't work because ctype.h may define
strtoupper as a macro:
"The problem is that most...
|
by: sanjay.vasudevan |
last post by:
Why are the following declarations invalid in C?
int f();
int f();
It would be great if anyone could also explain the design decision
for such a language restricton.
Regards,
Sanjay
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| |