471,348 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 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 2802
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Gibby Koldenhof | last post: by
9 posts views Thread by Mikhail Teterin | last post: by
4 posts views Thread by Bilgehan.Balban | last post: by
3 posts views Thread by Fernando Barsoba | last post: by
4 posts views Thread by sofeng | last post: by
19 posts views Thread by Taras_96 | last post: by
127 posts views Thread by sanjay.vasudevan | 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.