Trying to pass sys.argv as (int argc, char **argv) using ctypes

Mark Summerfield list at qtrac.plus.com
Mon Jun 6 05:50:20 EDT 2016


Hi,

I have a setup roughly like this:

import ctypes
import sys

Lib = ctypes.cdll.LoadLibrary("libthing")

c_char_pp = ctypes.POINTER(ctypes.c_char_p)

LibOpen = Lib.Open
LibOpen.argtypes = (ctypes.c_int,       # argc
                    c_char_pp)          # argv
LibOpen.restype = ctypes.c_int


argc = ctypes.c_int(len(sys.argv))
Args = ctypes.c_char_p * len(sys.argv)
args = Args(*[ctypes.c_char_p(arg.encode("utf-8"))
              for arg in sys.argv])
argv = ctypes.pointer(args)
LibOpen(argc, ctypes.byref(argv))

But when run it produces an error:

ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected LP_c_char_p instance instead of pointer to LP_c_char_p_Array_1

or this if I use LibOpen(argc, argv):

ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected LP_c_char_p instance instead of LP_c_char_p_Array_1


I understand what it is telling me; but I don't know or understand ctypes well enough to fix it. I guess I need to convert the array pointer to a char ** pointer? Can anyone tell me, or point me to info that would help?

Thanks!


More information about the Python-list mailing list