Working with ctypes and char** data type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philluminati

    Working with ctypes and char** data type

    I'm a bit of a python newbie and I need to wrap a C library.

    I can initialise the library using CDLL('mcclient. so')

    and I can call functions correctly inside the library but I need to
    invoke one function which has this function definition:

    char ** CAPAPI McSearch(HMCLIN K Handle,
    short nSearchType,
    short nNoSelect,
    char **pSelect,
    short nNoWhere,
    char **pWhere,
    char **pData,
    int iTimeout);

    For **pSelect I want to pass in an array of char points, which in C
    would be declared as

    char *pData[] = { "ADDR", "POSTCODE" };

    Can someone tell me how use pointers + char pointers together in
    python with ctypes please?

    Thank you

    Phill
  • Thomas Heller

    #2
    Re: Working with ctypes and char** data type

    Philluminati schrieb:
    I'm a bit of a python newbie and I need to wrap a C library.
    >
    I can initialise the library using CDLL('mcclient. so')
    >
    and I can call functions correctly inside the library but I need to
    invoke one function which has this function definition:
    >
    char ** CAPAPI McSearch(HMCLIN K Handle,
    short nSearchType,
    short nNoSelect,
    char **pSelect,
    short nNoWhere,
    char **pWhere,
    char **pData,
    int iTimeout);
    >
    For **pSelect I want to pass in an array of char points, which in C
    would be declared as
    >
    char *pData[] = { "ADDR", "POSTCODE" };
    >
    Can someone tell me how use pointers + char pointers together in
    python with ctypes please?
    # create an array that holds two pointers to 'char *', and fill it with data:
    pData = (c_char_p * 2)()
    pData[0] = "ADDR"
    pData[1] = "POSTCODE"

    # Another way:
    pData = (c_char_p * 2)("ADDR", "POSTCODE")

    Thomas

    Comment

    • Philluminati

      #3
      Re: Working with ctypes and char** data type

      On Jul 24, 4:03 pm, Thomas Heller <thel...@python .netwrote:
      Philluminati schrieb:
      >
      >
      >
      I'm a bit of a python newbie and I need to wrap a C library.
      >
      I can initialise the library using CDLL('mcclient. so')
      >
      and I can call functions correctly inside the library but I need to
      invoke one function which has this function definition:
      >
      char ** CAPAPI McSearch(HMCLIN K Handle,
                              short nSearchType,
                              short nNoSelect,
                              char **pSelect,
                              short nNoWhere,
                              char **pWhere,
                              char **pData,
                              int iTimeout);
      >
      For **pSelect I want to pass in an array of char points, which in C
      would be declared as
      >
      char *pData[] = { "ADDR", "POSTCODE" };
      >
      Can someone tell me how use pointers + char pointers together in
      python with ctypes please?
      >
      # create an array that holds two pointers to 'char *', and fill it with data:
      pData = (c_char_p * 2)()
      pData[0] = "ADDR"
      pData[1] = "POSTCODE"
      >
      # Another way:
      pData = (c_char_p * 2)("ADDR", "POSTCODE")
      >
      Thomas
      Thank you Thomas for your reply. I also got it working in a slightly
      less sophisticated manor like this:

      SelectType = c_char_p * 2
      select = SelectType("ADD R", "POSTCODE")
      ptr = pointer(select)

      Anyway, thanks for taking the time to reply!

      Phill

      Comment

      Working...