Re: [ctypes-users] RE Passing a pointer to a pointer to a struct toa function [SEC=PERSONAL]
Brought to you by:
theller
From: dwelch91 <dwe...@gm...> - 2009-09-30 02:05:36
|
Yes, it does. That seems to work properly. Many thanks! On Tue, Sep 29, 2009 at 5:12 PM, Andrew MacIntyre < And...@ac...> wrote: > [ please excuse top posting ] > The POINTER() function returns a type, so you have to create a pointer > instance: > > dests = POINTER(cups_dest_t)() > > You then pass this by reference into your function: > > num_dests = _get_dests(byref(dests)) > > > Hope this helps, > Andrew. > > -------------------------> "These thoughts are mine alone!" <--------- > Andrew MacIntyre National Licensing and Allocations Branch > tel: +61 2 6219 5356 Inputs to Industry Division > fax: +61 2 6253 3277 Australian Communications & Media Authority > email: and...@ac... http://www.acma.gov.au/ > > > -----Original Message----- > > From: dwelch91 [mailto:dwe...@gm...] > > Sent: Wednesday, 30 September 2009 4:24 AM > > To: fra...@bu... > > Cc: ctypes-users > > Subject: Re: [ctypes-users] RE Passing a pointer to a pointer > > to a struct toa function > > > > I Think you have to instanciate an object, try : > > > > dests = cups_dest_t() > > Like I said in my previous post, I'm not sure why this should > > be necessary. The caller doesn't need to create any > > cups_dest_t objects, only a pointer that will receive the > > address of the allocated memory for the cups_dest_t objects > > that the cupsGetDests() API returns. > > > > As an example of this, here's how this API is called from C code: > > > > > > cups_dest_t *dests; > > int num_dests; > > num_dests = cupsGetDests(&dests); > > > > > > // ... do something with dests ... > > cupsFreeDests(num_dests, dests); // free the memory that > > cupsGetDests() allocated > > > > With your changes, I get this TB: > > > > ctypes.ArgumentError: argument 1: <type > > 'exceptions.TypeError'>: expected LP_LP_cups_dest_s instance > > instead of cups_dest_s > > > > Thanks again for your help. > > > > > #C API: int cupsGetDests(cups_dest_t **dests); > > > num_dests = _get_dests(POINTER(dests)) > > and with num_dests = _get_dests( dests ) do you get a traceback ? > > > > I assume that the library routine is allocating the objects. > > How do you free them in a C program ? > > > > > print num_dests > > > > > > > > > 2009/9/29 <fra...@bu...> > > > > > > > > dwelch91 <dwe...@gm...> a écrit sur 29/09/2009 18:58:51 : > > > > > > > > I'm having trouble wrapping a simple CUPS API call > > with ctypes.... > > > > > > The relevant header code is from cups.h is: > > > > > > ~~~~~~~ > > > > > > typedef struct cups_option_s /**** Printer > > Options ****/ > > > { > > > char *name; /* Name of option */ > > > char *value; /* Value of option */ > > > } cups_option_t; > > > > > > typedef struct cups_dest_s /**** Destination ****/ > > > { > > > char *name, /* Printer or class name */ > > > *instance; /* Local instance name or NULL */ > > > int is_default; /* Is this printer > > the default? */ > > > int num_options; /* Number of options */ > > > cups_option_t *options; /* Options */ > > > } cups_dest_t; > > > > > > extern int cupsGetDests(cups_dest_t **dests); > > > > > > > > > STRING = c_char_p > > > > > > class cups_option_s(Structure): > > > _fields_ = [ > > > ('name', STRING), > > > ('value', STRING), > > > ] > > > cups_option_t = cups_option_s > > > > > > class cups_dest_s(Structure): > > > _fields_ = [ > > > ('name', STRING), > > > ('instance', STRING), > > > ('is_default', c_int), > > > ('num_options', c_int), > > > ('options', POINTER(cups_option_t)), > > > ] > > > cups_dest_t = cups_dest_s > > With two names it is a bit more confusing. > > > > > > # code to initialize lib is not shown > > > > > > _get_dests = lib.cupsGetDests > > > _get_dests.argtypes = [POINTER(POINTER(cups_dest_t)) > > > ] > > > _get_dests.restype = c_int > > > > > > def get_dests(): > > > dests = POINTER(cups_dest_t) > > > > I Think you have to instanciate an object, try : > > > > dests = cups_dest_t() > > > > > #C API: int cupsGetDests(cups_dest_t **dests); > > > num_dests = _get_dests(POINTER(dests)) > > and with num_dests = _get_dests( dests ) do you get a > > traceback ? > > > > I assume that the library routine is allocating the > > objects. How do you free them in a C program ? > > > > > print num_dests > > > > > > ~~~~~~~ > > > > > > The error I currently get is: > > > Traceback (most recent call last): > > > ... > > > num_dests = _get_dests(POINTER(dests)) > > > ctypes.ArgumentError: argument 1: <type > > 'exceptions.TypeError'>: > > > expected LP_LP_cups_dest_s instance instead of > > _ctypes.PointerType > > > > > > I've tried all sorts of combinations of cast, byref, > > > pointer/POINTER, etc. Any ideas? > > > > > > Thanks! > > > > > > Regards, > > Mit freundlichen Grüssen, > > ?? ?????? ??????? > > > > F. Petitjean > > > > > > -------------------------------------------------------------- > > ---------------- > > Come build with us! The BlackBerry® Developer > > Conference in SF, CA > > is the only developer event you need to attend this > > year. Jumpstart your > > developing skills, take BlackBerry mobile applications > > to market and stay > > ahead of the curve. Join us from November 9-12, > > 2009. Register now! > > http://p.sf.net/sfu/devconf > > _______________________________________________ > > ctypes-users mailing list > > cty...@li... > > https://lists.sourceforge.net/lists/listinfo/ctypes-users > > > > > > > > > > > > If you have received this email in error, please notify the sender > immediately and erase all copies of the email and any attachments to it. The > information contained in this email and any attachments may be private, > confidential and legally privileged or the subject of copyright. If you are > not the addressee it may be illegal to review, disclose, use, forward, or > distribute this email and/or its contents. > > Unless otherwise specified, the information in the email and any > attachments is intended as a guide only and should not be relied upon as > legal or technical advice or regarded as a substitute for legal or technical > advice in individual cases. Opinions contained in this email or any of its > attachments do not necessarily reflect the opinions of ACMA. > |