Re: [ctypes-users] RE Passing a pointer to a pointer to a struct to a function
Brought to you by:
theller
From: dwelch91 <dwe...@gm...> - 2009-09-29 18:24:49
|
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 > > |