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

user955566

I want to pass a python list containing string data to a "c" DLL, which process the data and should return an array containing integer data. What will be the python code and c code using "ctypes". I summarize it as follows:

I want to pass following data from python script, e.g:

`list=["AO10","AO20","AO30"]` and

i expect that DLL code should return an array of integers, e.g

arr={10,20,30}  

I have tried the following code but the program halted without giving any data

Python Script

from ctypes import *

mydll = CDLL("C:\\abc.dll")
mydll.sumabc.argtypes = (POINTER(c_char_p), c_int)
list= ["AO10","AO20","AO30"]
array_type = c_char_p * 3
mydll.sumabc.restype = None
my_array = array_type(*a)
mydll.epicsData(my_array, c_int(3))
print(list(my_array))

c DLL

#include "stdafx.h"
#include "myheader.h"

int* epicsData(char *in_data, int size)
{
  for(int i = 1; i < size; i++)
  {
     in_data[i] =i*10;
  }
  return in_data[]
}
Mark Tolonen

The given C code doesn't match the Python wrapper. The function name doesn't match and the types don't match. Here's a working example for your learning:

test.c

#include <string.h>

#ifdef _WIN32
#   define API __declspec(dllexport)  // Windows-specific export
#else
#   define API
#endif

/* This function takes pre-allocated inputs of an array of byte strings
 * and an array of integers of the same length.  The integers will be
 * updated in-place with the lengths of the byte strings.
 */
API void epicsData(char** in_data, int* out_data, int size)
{
    for(int i = 0; i < size; ++i)
        out_data[i] = (int)strlen(in_data[i]);
}

test.py

from ctypes import *

dll = CDLL('test')
dll.epicsData.argtypes = POINTER(c_char_p),POINTER(c_int),c_int
dll.epicsData.restype = None

data = [b'A',b'BC',b'DEF'] # Must be byte strings.

# Create the Python equivalent of C 'char* in_data[3]' and 'int out_data[3]'.
in_data = (c_char_p * len(data))(*data)
out_data = (c_int * len(data))()

dll.epicsData(in_data,out_data,len(data))
print(list(out_data))

Output:

[1, 2, 3]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I convert a Python list into a C array by using ctypes?

From Dev

Python Ctypes: Convert returned C array to python list, WITHOUT numpy

From Dev

How to interface a NumPy complex array with C function using ctypes?

From Dev

How to pass a python list to C function (dll) using ctypes

From Dev

Python ctypes: Passing an array of strings

From Dev

Loading dll using Python Ctypes

From Dev

Python ctypes arguments with DLL - pointer to array of doubles

From Dev

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

From Dev

Python Ctypes passing pointer for data

From Python

Trouble using Python ctypes to run C dll function (array output with unknown size)

From Dev

C dll equivalent to python ctypes

From Dev

Returning a C array from a C function to Python using ctypes

From Dev

Passing audio data from Python to C with ctypes

From Dev

C function called from Python via ctypes returns incorrect value

From Dev

type error using ctypes with windows dll function

From Dev

Python ctypes function returns ValueError when C function returns NULL

From Dev

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

From Dev

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

From Dev

How do I get the value of a global variable in a dll which is described by an ordinal not a name using ctypes in Python?

From Dev

C++ function in python from .dll using ctypes - function not found and access violation

From Dev

Get const array from DLL using ctypes

From Dev

Pass byte numpy array to C function using ctypes

From Dev

ArgumentError when passing ctypes array to C function

From Dev

Passing a function which returns array as a parameter

From Dev

Accessing DLL function using ctypes

From Dev

List of pointers of ctypes.Structures in Python and C++ function

From Dev

C Function Returns Integer Instead of Float - Python ctypes

From Dev

Python ctypes TypeError while calling a DLL function

From Dev

imported some function from c++ dll to python using ctypes, but some functions doesn't work as expected

Related Related

  1. 1

    How do I convert a Python list into a C array by using ctypes?

  2. 2

    Python Ctypes: Convert returned C array to python list, WITHOUT numpy

  3. 3

    How to interface a NumPy complex array with C function using ctypes?

  4. 4

    How to pass a python list to C function (dll) using ctypes

  5. 5

    Python ctypes: Passing an array of strings

  6. 6

    Loading dll using Python Ctypes

  7. 7

    Python ctypes arguments with DLL - pointer to array of doubles

  8. 8

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

  9. 9

    Python Ctypes passing pointer for data

  10. 10

    Trouble using Python ctypes to run C dll function (array output with unknown size)

  11. 11

    C dll equivalent to python ctypes

  12. 12

    Returning a C array from a C function to Python using ctypes

  13. 13

    Passing audio data from Python to C with ctypes

  14. 14

    C function called from Python via ctypes returns incorrect value

  15. 15

    type error using ctypes with windows dll function

  16. 16

    Python ctypes function returns ValueError when C function returns NULL

  17. 17

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

  18. 18

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

  19. 19

    How do I get the value of a global variable in a dll which is described by an ordinal not a name using ctypes in Python?

  20. 20

    C++ function in python from .dll using ctypes - function not found and access violation

  21. 21

    Get const array from DLL using ctypes

  22. 22

    Pass byte numpy array to C function using ctypes

  23. 23

    ArgumentError when passing ctypes array to C function

  24. 24

    Passing a function which returns array as a parameter

  25. 25

    Accessing DLL function using ctypes

  26. 26

    List of pointers of ctypes.Structures in Python and C++ function

  27. 27

    C Function Returns Integer Instead of Float - Python ctypes

  28. 28

    Python ctypes TypeError while calling a DLL function

  29. 29

    imported some function from c++ dll to python using ctypes, but some functions doesn't work as expected

HotTag

Archive