473,394 Members | 2,052 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,394 software developers and data experts.

ctype and functions that return a pointer to a structure

hg
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

Jul 1 '07 #1
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>
Jul 1 '07 #2
hg
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

Jul 1 '07 #3

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

Similar topics

6
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...
9
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...
9
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...
4
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...
3
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...
11
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...
4
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? ...
19
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...
127
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
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
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...
0
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
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
Oralloy
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,...
0
tracyyun
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...

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.