Python 3.4.3 ctypes: Trying to pass a structure to a function as an output parameter fails -
here's given vendor describe function.
typedef struct { float distance_sm; float distance_nm; short fuel_stops; char flight_time[8]; char block_time[8]; char departure_time[8]; char departure_date[12]; char arrival_time[8]; char arrival_date[12]; char timezone_change[8]; char messages[fosapi_msg_len]; } fos_leg; fos_extern_c bool dll_exp_util fos_calc_flight( char *leg_date, // in: leg date ( char *leg_time, // in: leg time ( char *airport_from, // in: airport icao(4) or iata(3) (kokc) char *airport_to, // in: airport icao(4) or iata(3) char *aircraft_type, // in: character aircraft type (max len = 4) fos_leg *fos_leg, // in: allocated space fos_leg char errors[fosapi_msg_len], // in: 256 bytes out: error messages bool arrival_date_time=false, // in: true = leg_date , leg_time arrival, calculate departure char *airport_from_prefix=null, // in: airport icao code(2) char *airport_to_prefix=null); // in: airport icao code(2)
so here's code i'm trying access function with. seems problem way i'm trying pass structure output parameter function.
import ctypes mydll = ctypes.windll.loadlibrary("c:\\fos\\fosutil.dll") class fos_leg(ctypes.structure): _fields_ = [ ("distance_sm", ctypes.c_float), ("distance_nm", ctypes.c_float), ("fuel_stops", ctypes.c_short), ("flight_time", ctypes.c_char), ("block_time", ctypes.c_char), ("departure_time", ctypes.c_char), ("departure_date", ctypes.c_char), ("arrival_time", ctypes.c_char), ("arrival_date", ctypes.c_char), ("timezone_change", ctypes.c_char), ("messages", ctypes.c_char)] fos_leg = ctypes.pointer(fos_leg) error_out = ctypes.c_char mydll._fos_calc_flight("04042014","0800","kokc","katl","h25b",fos_leg,error_out,1,"ap","ap")
here's error returned.
traceback (most recent call last): file "test.py", line 28, in mydll._fos_calc_flight("04042014","0800","kokc","katl","h25b",fos_leg,er ror_out,1,"ap","ap") ctypes.argumenterror: argument 6: : don't know how convert parameter 6
thanks assistance
untested, structure should more like:
class fos_leg(ctypes.structure): _fields_ = [ ("distance_sm", ctypes.c_float), ("distance_nm", ctypes.c_float), ("fuel_stops", ctypes.c_short), ("flight_time", ctypes.c_char * 8), ("block_time", ctypes.c_char * 8), ("departure_time", ctypes.c_char * 8), ("departure_date", ctypes.c_char * 12), ("arrival_time", ctypes.c_char * 8), ("arrival_date", ctypes.c_char * 12), ("timezone_change", ctypes.c_char * 8), ("messages", ctypes.c_char * fosapi_msg_len)]
next should declare arguments , return type of function:
mydll._fos_calc_flight.argtypes = [ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.pointer(fos_leg),ctypes.c_char_p,ctypes.c_bool,ctypes.c_char_p,ctypes.c_char_p] mydll._fos_calc_flight.restype = ctypes.c_bool
finally, output parameters need instances of types required. pointer
declares type, want create instance of structure:
fos_leg = fos_leg()
error_out
needs writable string:
error_out = ctypes.create_string_buffer(fosapi_msg_len)
then call function reference structure:
mydll._fos_calc_flight(b"04042014",b"0800",b"kokc",b"katl",b"h25b",ctypes.byref(fos_leg),error_out,1,b"ap",b"ap")
Comments
Post a Comment