ctype and functions that return a pointer to a structure

H

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
 
L

Lenard Lindstrom

hg said:
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.
 
H

hg

Lenard said:
hg said:
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.

Many thanks,

hg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top