473,241 Members | 1,619 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,241 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 3147
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.