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

ctypes: return a pointer to a struct

I'm not able to build IP2Location's Python interface so I'm
trying to use ctypes to call its C interface. The functions
return a pointer to the struct below. I haven't been able to
figure out how I should declare the return type of the functions
and read the fields. Any hint is appreciated.

typedef struct
{
char *country_short;
char *country_long;
char *region;
char *city;
char *isp;
float latitude;
float longitude;
char *domain;
char *zipcode;
char *timezone;
char *netspeed;
} IP2LocationRecord;
Jun 27 '08 #1
6 12676
On Apr 25, 5:09 am, "Jack" <nos...@invalid.comwrote:
typedef struct
{
char *country_short;
char *country_long;
char *region;
char *city;
char *isp;
float latitude;
float longitude;
char *domain;
char *zipcode;
char *timezone;
char *netspeed;

} IP2LocationRecord;

First define a struct type IP2LocationRecord by subclassing from
ctypes.Structure. Then define a pointer type as
ctypes.POINTER(IP2LocationRecord) and set that as the function's
restype attribute. See the ctypes tutorial or reference for details.


Jun 27 '08 #2
On Apr 25, 5:15 am, sturlamolden <sturlamol...@yahoo.nowrote:
First define a struct type IP2LocationRecord by subclassing from
ctypes.Structure. Then define a pointer type as
ctypes.POINTER(IP2LocationRecord) and set that as the function's
restype attribute. See the ctypes tutorial or reference for details.
Which is to say:

import ctypes

class IP2LocationRecord(ctypes.Structure):
_fields_ = [
('country_short', ctypes.c_char_p),
('country_long', ctypes.c_char_p),
('region', ctypes.c_char_p),
('city', ctypes.c_char_p),
('isp', ctypes.c_char_p),
('latitude', ctypes.c_float),
('longitude', ctypes.c_float),
('domain', ctypes.c_char_p),
('zipcode', ctypes.c_char_p),
('timezone', ctypes.c_char_p),
('netspeed', ctypes.c_char_p),
]

IP2LocationRecord_Ptr_t = ctypes.POINTER(IP2LocationRecord)

function.restype = IP2LocationRecord_Ptr_t
Jun 27 '08 #3
Thanks for the prompt and detailed reply. I tried that but was getting this
error:

AttributeError: 'LP_IP2LocationRecord' object has no attribute
'country_short'

Here's my version, which I think is equivalent to yours:
(as a matter of fact, I also tried yours and got the same error.)

class IP2LocationRecord(Structure):
_fields_ = [("country_short", c_char_p),
("country_long", c_char_p),
("region", c_char_p),
("city", c_char_p),
("isp", c_char_p),
("latitude", c_float),
("longitude", c_float),
("domain", c_char_p),
("zipcode", c_char_p),
("timezone", c_char_p),
("netspeed", c_char_p)]

IP2Location_get_all.restype = POINTER(IP2LocationRecord)
IP2LocationObj = IP2Location_open(thisdir + '/IP-COUNTRY-SAMPLE.BIN')
rec = IP2Location_get_all(IP2LocationObj, '64.233.167.99')
print rec.country_short
IP2Location_close(IP2LocationObj)
"sturlamolden" <st**********@yahoo.nowrote in message
news:4d**********************************@x35g2000 hsb.googlegroups.com...
On Apr 25, 5:15 am, sturlamolden <sturlamol...@yahoo.nowrote:
>First define a struct type IP2LocationRecord by subclassing from
ctypes.Structure. Then define a pointer type as
ctypes.POINTER(IP2LocationRecord) and set that as the function's
restype attribute. See the ctypes tutorial or reference for details.

Which is to say:

import ctypes

class IP2LocationRecord(ctypes.Structure):
_fields_ = [
('country_short', ctypes.c_char_p),
('country_long', ctypes.c_char_p),
('region', ctypes.c_char_p),
('city', ctypes.c_char_p),
('isp', ctypes.c_char_p),
('latitude', ctypes.c_float),
('longitude', ctypes.c_float),
('domain', ctypes.c_char_p),
('zipcode', ctypes.c_char_p),
('timezone', ctypes.c_char_p),
('netspeed', ctypes.c_char_p),
]

IP2LocationRecord_Ptr_t = ctypes.POINTER(IP2LocationRecord)

function.restype = IP2LocationRecord_Ptr_t


Jun 27 '08 #4
On Apr 25, 5:39 am, "Jack" <nos...@invalid.comwrote:
AttributeError: 'LP_IP2LocationRecord' object has no attribute
'country_short'
As it says, LP_IP2LocationRecord has no attribute called
'country_short'. IP2LocationRecord does.

Use the 'contents' attribute to dereference the pointer. That is:

yourstruct.contents.country_short



Jun 27 '08 #5
That worked. Thank you!
>AttributeError: 'LP_IP2LocationRecord' object has no attribute
'country_short'

As it says, LP_IP2LocationRecord has no attribute called
'country_short'. IP2LocationRecord does.

Use the 'contents' attribute to dereference the pointer. That is:

yourstruct.contents.country_short

Jun 27 '08 #6
On Apr 25, 5:39 am, "Jack" <nos...@invalid.comwrote:
IP2Location_get_all.restype = POINTER(IP2LocationRecord)
IP2LocationObj = IP2Location_open(thisdir + '/IP-COUNTRY-SAMPLE.BIN')
rec = IP2Location_get_all(IP2LocationObj, '64.233.167.99')
print rec.country_short
print rec.contents.country_short
Jun 27 '08 #7

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

Similar topics

4
by: JR | last post by:
I need some help. I am trying to return a dirent struct location so i can access what the function found in main(). I dont understand pointers very well and think that is were i am getting it...
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
2
by: Steven T. Hatton | last post by:
Can somebody explain why making T a friend of either B or C will permit the code to compile? class A{ protected: A(){} }; class T; class B: protected A {protected: B(){}/*friend class T;*/};...
45
by: noridotjabi | last post by:
What is the purpose of the function pointer? Why do you need a pointer to a function. I cannot really think of any application where this is the only or even easiest solution to a problem. I'm...
5
by: A. Farber | last post by:
Hello, I call readv() and writev() in several spots of a program which I run under Linux, OpenBSD and Cygwin. Since it always the same way (check the return value; then check errno and retry if...
4
by: Neal Becker | last post by:
In an earlier post, I was interested in passing a pointer to a structure to fcntl.ioctl. This works: c = create_string_buffer (...) args = struct.pack("iP", len(c), cast (pointer (c),...
0
by: gianluca | last post by:
I've a problem with dll function colled with python/ctypes. My functions (C) requred a typedef int "value_type" in tree different way: same as value_type; - mydll.foo1(value_type) same as...
2
by: Jean-Paul Calderone | last post by:
On Mon, 30 Jun 2008 09:13:42 -0700 (PDT), gianluca <geonomica@gmail.comwrote: POINTER takes a class and returns a new class which represents a pointer to input class. pointer takes an instance...
34
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.