473,769 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
} IP2LocationReco rd;
Jun 27 '08 #1
6 12923
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;

} IP2LocationReco rd;

First define a struct type IP2LocationReco rd by subclassing from
ctypes.Structur e. Then define a pointer type as
ctypes.POINTER( IP2LocationReco rd) 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...@y ahoo.nowrote:
First define a struct type IP2LocationReco rd by subclassing from
ctypes.Structur e. Then define a pointer type as
ctypes.POINTER( IP2LocationReco rd) and set that as the function's
restype attribute. See the ctypes tutorial or reference for details.
Which is to say:

import ctypes

class IP2LocationReco rd(ctypes.Struc ture):
_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 ),
]

IP2LocationReco rd_Ptr_t = ctypes.POINTER( IP2LocationReco rd)

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

AttributeError: 'LP_IP2Location Record' 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 IP2LocationReco rd(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(IP2Loca tionRecord)
IP2LocationObj = IP2Location_ope n(thisdir + '/IP-COUNTRY-SAMPLE.BIN')
rec = IP2Location_get _all(IP2Locatio nObj, '64.233.167.99' )
print rec.country_sho rt
IP2Location_clo se(IP2LocationO bj)
"sturlamold en" <st**********@y ahoo.nowrote in message
news:4d******** *************** ***********@x35 g2000hsb.google groups.com...
On Apr 25, 5:15 am, sturlamolden <sturlamol...@y ahoo.nowrote:
>First define a struct type IP2LocationReco rd by subclassing from
ctypes.Structu re. Then define a pointer type as
ctypes.POINTER (IP2LocationRec ord) and set that as the function's
restype attribute. See the ctypes tutorial or reference for details.

Which is to say:

import ctypes

class IP2LocationReco rd(ctypes.Struc ture):
_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 ),
]

IP2LocationReco rd_Ptr_t = ctypes.POINTER( IP2LocationReco rd)

function.restyp e = IP2LocationReco rd_Ptr_t


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

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

yourstruct.cont ents.country_sh ort



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

As it says, LP_IP2LocationR ecord has no attribute called
'country_short' . IP2LocationReco rd does.

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

yourstruct.cont ents.country_sh ort

Jun 27 '08 #6
On Apr 25, 5:39 am, "Jack" <nos...@invalid .comwrote:
IP2Location_get _all.restype = POINTER(IP2Loca tionRecord)
IP2LocationObj = IP2Location_ope n(thisdir + '/IP-COUNTRY-SAMPLE.BIN')
rec = IP2Location_get _all(IP2Locatio nObj, '64.233.167.99' )
print rec.country_sho rt
print rec.contents.co untry_short
Jun 27 '08 #7

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

Similar topics

4
2141
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 wrong. If i print everything from getdirlist everything is fine. I just cant pass along the namelist scandir creates. Any help would be great. JR
4
6954
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 I'm defining the functions as void*, and then casting when I use them. This code is going into a general purpose framework, and it would be much nicer if the user didn't need to do any casting. Can someone tell me how to set up those typedefs...
2
4420
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;*/}; class C: public B {public : C(){}/*friend class T;*/}; struct T { A* newA() {return new C();}}; int main() {}
45
899
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 sure there are really good aplications for it I just cannot think of any, so if anyone can tell me why a pointer to a function is nessisary and when it can/should be used I would apreciate that. Thanks. Nori
5
7817
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 EAGAIN/EINTR), I've written a wrapper function (full source code on the bottom) to call those functions and just pass the function pointer to it: do { ...
4
8508
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), c_void_p).value) err = fcntl.ioctl(eos_fd, request, args) Now to do the same with ctypes, I have one problem.
0
1137
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 *value_type; - mydll.foo2(*value_type) same as **value_type; - mydll.foo3(**value_type) How can pass it in python. If i do that: rules=POINTER(value_type) opr=rsl.StrengthOfRules(rules,10)
2
16525
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 and returns a new object which represents a pointer to that instance. Jean-Paul
34
2031
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
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7408
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.