python - How to handle array of strings (char **) in ctypes in a 64-bit environment? -


i'm using ctypes work libgphoto2 in python. following code succeeds on 32-bit machine, fails segmentation fault on 64-bit machine (linux, ubuntu):

import ctypes  gp = ctypes.cdll('libgphoto2.so') = gp.gp_library_version(0) x = ctypes.c_char_p.from_address(a) x.value 

libphoto2, ctypes , python repositories, assume problem in code; need consider pointers in more general way or that. gp_library_version() returns const char **, array of string constants. other gp_* functions work fine.

the problem that, default, ctypes assumes functions return int, 32 bits. but, on 64-bit system, pointer 64 bits, you're dropping upper 32 bits pointer return value, segfault when try dereference invalid pointer.

you can change expected return type of function assigning type restype attribute so:

gp = ctypes.cdll('libgphoto2.so') gp.gp_library_version.restype = ctypes.pointer(ctypes.c_char_p) = gp.gp_library_version(0) print a[0].value 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -