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

ThomasI

I need to create a c-function, which should be called from Python - using ctypes - and which should provide an array of strings to the calling python function. I have full control of both environments.

Preferably, it should work with Python 2 and 3.

I have this c-function:

long ProvideTexts(wchar_t** texts, long textCount, long stringMaxSize)

the return value is an error code. texts is the actual array. textCount is the number of elements in the array. stringMaxSize is the maximum size of each string.

I am trying to allocate everything in Python and to have the c++ function overwrite the strings. I am not sure if this is the best way. I figured it was the simplest way to go since the memory will need to be de-allocated in Python.

This is my (non-working) code to create the strings and call the function:

import ctypes
stringMaxSize = 1000
ProvideTextsMethod = loadedDll.ProvideTexts
ProvideTextsMethod.restype = ctypes.c_long
arrayType = ctypes.c_wchar_p * textCount
pyArray = arrayType()
for i in range(0, textCount):
    pyArray[i] = ctypes.create_unicode_buffer(stringMaxSize) # fails here
ProvideTextsMethod.argtypes = [arrayType, ctypes.c_long, ctypes.c_long]
errorCode = ProvideTextsMethod(pyArray, textCount, stringMaxSize)

I get this error:

incompatible types, c_wchar_Array_1000 instance instead of c_wchar_p instance

What do I need to change?

Ashwith Rego

You'll need to cast it since pyArray is an array of c_whar_p:

for i in range(0, textCount):
    pyArray[i] = ctypes.cast(ctypes.create_unicode_buffer(stringMaxSize), ctypes.c_wchar_p)

Does this help?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How should I annotate a function returning a ctypes array in Python?

From Dev

Python ctypes to return an array of function pointers

From Dev

Python function that returns the first location as index in which the strings differ. If the strings are identical, it should return -1

From Dev

How should I call a function from .so using python ctypes.CDLL?

From Dev

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

From Dev

How to call a function with an array of strings

From Dev

Python ctypes: Passing an array of strings

From Dev

python ctypes array will not return properly

From Dev

How do write a function that takes in 2 strings and return an array of common characters (should not contain any duplicates)?

From Dev

Callbacks with ctypes (How to call a python function from C)

From Dev

How to call function overloaded by function which return partail function

From

How to return an array from Go[lang] to Python using ctypes?

From Dev

How to create function which return array of widgets

From Dev

How to call R function (which should not be exported) from Rcpp?

From Dev

Have C library call python function with ctypes

From Dev

How call function which is return from another one?

From Dev

Python function to return strings

From Dev

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

From Dev

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

From Dev

Convert Numpy array to ctypes `int` pointer to call Cython function from Python

From Dev

How to call a python flask function which is call by a post request?

From Dev

How to configure and export function that should return char array?

From Dev

How to call a function based on the changes in an array which is linked to the local storage

From Dev

How to create function which return only even numbers in array?

From Dev

How to return a struct which contains an array pointer from a function?

From Dev

How to return an array of strings from a function in a COM module?

From Dev

when should a function return in hapijs route call

From Dev

How should I call this function? I need a function which returns a tuple

From Dev

ctypes: How to access array of structure returned by function?

Related Related

  1. 1

    How should I annotate a function returning a ctypes array in Python?

  2. 2

    Python ctypes to return an array of function pointers

  3. 3

    Python function that returns the first location as index in which the strings differ. If the strings are identical, it should return -1

  4. 4

    How should I call a function from .so using python ctypes.CDLL?

  5. 5

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

  6. 6

    How to call a function with an array of strings

  7. 7

    Python ctypes: Passing an array of strings

  8. 8

    python ctypes array will not return properly

  9. 9

    How do write a function that takes in 2 strings and return an array of common characters (should not contain any duplicates)?

  10. 10

    Callbacks with ctypes (How to call a python function from C)

  11. 11

    How to call function overloaded by function which return partail function

  12. 12

    How to return an array from Go[lang] to Python using ctypes?

  13. 13

    How to create function which return array of widgets

  14. 14

    How to call R function (which should not be exported) from Rcpp?

  15. 15

    Have C library call python function with ctypes

  16. 16

    How call function which is return from another one?

  17. 17

    Python function to return strings

  18. 18

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

  19. 19

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

  20. 20

    Convert Numpy array to ctypes `int` pointer to call Cython function from Python

  21. 21

    How to call a python flask function which is call by a post request?

  22. 22

    How to configure and export function that should return char array?

  23. 23

    How to call a function based on the changes in an array which is linked to the local storage

  24. 24

    How to create function which return only even numbers in array?

  25. 25

    How to return a struct which contains an array pointer from a function?

  26. 26

    How to return an array of strings from a function in a COM module?

  27. 27

    when should a function return in hapijs route call

  28. 28

    How should I call this function? I need a function which returns a tuple

  29. 29

    ctypes: How to access array of structure returned by function?

HotTag

Archive