Python ctypes: Passing an array of strings

gt6989b

I have an array of strings in Python 2.7, which I would like to pass to a C function via ctypes:

unsigned int SetParams(unsigned int count, const char **params)

So I can define the parameters in python:

import ctypes as ct
lib = ct.cdll.LoadLibrary('...')
lib.SetParams.restype  = ct.c_uint
lib.SetParams.argtypes = [ct.c_uint, ct.POINTER(ct.c_char_p)]

but now when I am given a set of params in a Python function, which I would like to use in the above library call, how do I actually call it? I am thinking something like this:

def setParameters(strParamList):
    numParams    = len(strParamList)
    strArrayType = ct.c_char_p * numParams
    strArray     = strArrayType()
    for i, param in enumerate(strParamList):
        strArray[i] = param
    lib.SetParams(numParams, strArray)

I am just starting with ctypes and wondering if there is a more automated way to do this? Also is the assignment strArray[i] = param actually reallocating? If so, this seems quite expensive -- is there a way to do this just pointing the string pointers to Python-allocated buffers, or they are not NULL-terminated?

UPDATE I did look through a couple related questions, but could not find a direct one to deal with the same issues. Thank you very much

Mark Tolonen

You think correctly. That code works. Call with, for example:

setParameters(['abc','def','ghi'])

I haven't looked at the source, but ctypes on Python 2.7 (32-bit) does pass the internal Python buffer (tested by modifying the passed string in the C function and printing it after the call in Python), which is why you should only pass Python strings as above to functions that take const char* or at least are known not to modify the string. Since Python strings are immutable writing to them under C is a no-no. Use ctypes.create_string_buffer to create a writable string to pass to non-const char*.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Python

CPPYY/CTYPES passing array of strings as char* args[]

From Java

Passing an Array/List into Python

From Dev

Using Rust returned array in Python using ctypes

From Dev

Python ctypes usage passing pointers

From Dev

Passing a (uint8) NumPy array from python to c++ using ctypes

From Dev

Python Ctypes passing pointer for data

From Dev

Python: Similar functionality in struct and array vs ctypes

From Dev

Postgres function passing array of strings

From Dev

Passing audio data from Python to C with ctypes

From Dev

Passing an array of strings to UITableView headers

From Dev

Passing an array as an argument in Python

From Dev

Python -passing imgdata pointer to a function in C using ctypes

From Dev

Porting a VBA Type / C struct to a Python ctypes.Structure: array of strings with fixed length

From Dev

Python: Passing Function Arguments for strings

From Dev

python ctypes array will not return properly

From Dev

Python ctypes: How to call a function which should return an array of strings?

From Dev

Passing python strings to Fortran subroutine using ctypes

From Dev

Passing a python list to "c" DLL function which returns array data using ctypes

From Dev

ArgumentError when passing ctypes array to C function

From Dev

Passing struct from C to Python with ctypes

From Dev

Passing Numpy array to C with Ctypes differs between Linux and Windows

From Dev

How can I return a wchar_t** - null terminated array of UNICODE strings - to a Python script using CTypes

From Dev

Python ctypes passing string pointer / buffer

From Dev

When will Python garbage collect when passing an encoded str to ctypes library?

From Dev

Passing Array of strings to Sheets() in VBA

From Dev

ctypes return array of strings std::vector<std::string>>

From Dev

Pointer argument passing in python ctypes

From Dev

Passing objects by reference in python with ctypes

From Dev

Mallocing and Freeing in C, but passing the pointer through Python via ctypes

Related Related

  1. 1

    CPPYY/CTYPES passing array of strings as char* args[]

  2. 2

    Passing an Array/List into Python

  3. 3

    Using Rust returned array in Python using ctypes

  4. 4

    Python ctypes usage passing pointers

  5. 5

    Passing a (uint8) NumPy array from python to c++ using ctypes

  6. 6

    Python Ctypes passing pointer for data

  7. 7

    Python: Similar functionality in struct and array vs ctypes

  8. 8

    Postgres function passing array of strings

  9. 9

    Passing audio data from Python to C with ctypes

  10. 10

    Passing an array of strings to UITableView headers

  11. 11

    Passing an array as an argument in Python

  12. 12

    Python -passing imgdata pointer to a function in C using ctypes

  13. 13

    Porting a VBA Type / C struct to a Python ctypes.Structure: array of strings with fixed length

  14. 14

    Python: Passing Function Arguments for strings

  15. 15

    python ctypes array will not return properly

  16. 16

    Python ctypes: How to call a function which should return an array of strings?

  17. 17

    Passing python strings to Fortran subroutine using ctypes

  18. 18

    Passing a python list to "c" DLL function which returns array data using ctypes

  19. 19

    ArgumentError when passing ctypes array to C function

  20. 20

    Passing struct from C to Python with ctypes

  21. 21

    Passing Numpy array to C with Ctypes differs between Linux and Windows

  22. 22

    How can I return a wchar_t** - null terminated array of UNICODE strings - to a Python script using CTypes

  23. 23

    Python ctypes passing string pointer / buffer

  24. 24

    When will Python garbage collect when passing an encoded str to ctypes library?

  25. 25

    Passing Array of strings to Sheets() in VBA

  26. 26

    ctypes return array of strings std::vector<std::string>>

  27. 27

    Pointer argument passing in python ctypes

  28. 28

    Passing objects by reference in python with ctypes

  29. 29

    Mallocing and Freeing in C, but passing the pointer through Python via ctypes

HotTag

Archive