How to generate character array in ctypes and pass its pointer as function parameter


orca

I want to use ctypes to use some information in C++ which is contained in a serialized buffer in a list of Python characters. I have tried the following but I don't understand why my parameter type is throwing an error.

I have a C wrapper for some C++ code in the form

extern "C" {
    special_object *special_object_new() { return new special_object(); }

    int function(special_object *O, int buffer_size, char *buffer) {
        char *buf = (char*) malloc(buffer_size);
        for(int i = 0; i < buffer_size; i++) buf[i] = buffer[i];
        O->do_something_in_cpp_with_buf(buf);
        free(buf);
        buf = NULL;
        return 0;
    }
}

I want to pass functiona character buffer from Python ctypes buf. This char buffer originally came as a list of chars in Python, so I first converted it to a C char array using

import ctypes
import cdll
buf_c = (ctypes.c_char*len(buf))(*buf)

then use

buf_p = ctypes.cast(buf_c, ctypes.POINTER(ctypes.c_char))

has a type

>>> <class 'ctypes.LP_c_char'>

I already know the size ( integer) of this buffer .buf_size

I do the following (load the library and declare function parameters),

lib = cdll.LoadLibrary('PATH_TO_LIB/lib.so')
lib.function.restype = ctypes.c_int
lib.function.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char)]

Then, I create the class that contains the function,

class Special(object):
    def __init__(self):
        self.obj = lib.special_object_new()

    def py_function(self, buffer_size, buffer):
        res = lib.function(self.obj, buffer_size, buffer)

Finally, I try to call it as

s = Special()
s.py_function(ctypes.c_int(buf_size), buffer)

but i get the error

ArgumentError: argument 1: <type 'exceptions.TypeError'>: wrong type

I can't figure out what I'm doing wrong when doing pointers? Any help would be greatly appreciated!

Mark Tolonen

You must define all parameters for each function. Here is a working example:

test file

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

#include <stdio.h>

struct special_object {
    special_object() {
        printf("created\n");
    }

    ~special_object() {
        printf("destroyed\n");
    }

    void print(char* buffer, int buffer_size) {
        for(int i = 0; i < buffer_size; i++)
            printf("%02X ",static_cast<unsigned char>(buffer[i]));
        printf("\n");
    }
};

extern "C" {
    API special_object *special_object_new() {
        return new special_object();
    }

    API void special_object_print(special_object *O, char *buffer, int buffer_size) {
        O->print(buffer, buffer_size);
    }

    API void special_object_delete(special_object *O) {
        delete O;
    }
}

test.py

from ctypes import *

lib = CDLL('test')
lib.special_object_new.argtypes = ()
lib.special_object_new.restype = c_void_p
lib.special_object_print.argtypes = c_void_p, c_char_p, c_int
lib.special_object_print.restype = None
lib.special_object_delete.argtypes = c_void_p,
lib.special_object_delete.restype = None

class Special:
    def __init__(self):
        self.obj = lib.special_object_new()
    def __del__(self):
        lib.special_object_delete(self.obj)
    def print(self,buffer):
        lib.special_object_print(self.obj,buffer,len(buffer))

s = Special()
s.print(bytes([0x01,0x02,0xaa,0x55]))
s.print(b'hello, world!')

output:

created
01 02 AA 55
68 65 6C 6C 6F 2C 20 77 6F 72 6C 64 21
destroyed

Related


How to pass char pointer as parameter in ctypes python

Naveen kumar Katta rathanaiah Please help me to convert the following line of C++ code to ctypes python: Ret = openFcn(&Handle, "C:\\Config.xml"); Here are the declarations for each: typedef uint16_t (* OpenDLLFcnP)(void **, const char *); OpenDLLFcnP openFcn

How to pass a character pointer to a function

nitishy2j I'm trying to solve a problem where I have to use a character pointer and store an email, and then pass that pointer to a function so I can verify if there are any special characters in the input string. this is the code #include <stdio.h> #include <

How to pass pointer/array as parameter to GTK callback function?

Hospital I would like to pass a list of integers numbersto a callback function which printerwill print what is when the numbersbutton is pressed . The last post in this forum thread suggested me to do something like this: #include <gtk/gtk.h> static void prin

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Use function pointer to pass string array as parameter

peter 117 I am trying to pass a function pointer to another function which has an array of strings as parameter. So far I have the following: void pass_function(string args[]) { //so something with args. } void takes_a_function(void(*function)(string[

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?