I have a structure which contains character array on C side
stuct s
{
int x;
char buffer[100];
}
and on my python side I define
class myS(ctypes.Structure):
_fields_ = [("x", c_int),
("buffer",type(create_string_buffer(100)))]
Now, when I do
buf = create_string_buffer(64)
s1 = myS(10,buf)
It gives me error
TypeError: expected string or Unicode object, c_char_Array_100 found
I want a string which will be changed by my C function. how to do it?
You don't have to create a buffer. The buffer is in the structure when you instantiate it.
Here's a quick DLL:
#include <string.h>
struct s
{
int x;
char buffer[100];
};
__declspec(dllexport) void func(struct s* a)
{
a->x = 5;
strcpy(a->buffer,"here is the contents of the string.");
}
And here's Python code to call it:
import ctypes
class myS(ctypes.Structure):
_fields_ = [
("x", ctypes.c_int),
("buffer",ctypes.c_char * 100)]
s1 = myS()
dll = ctypes.CDLL('test')
dll.func(ctypes.byref(s1))
print s1.buffer
print s1.x
Output:
here is the contents of the string.
5
You can assign a regular Python string to a 100*c_char field:
class myS(ctypes.Structure):
_fields_ = [("x", c_int),
("buffer", 100*c_char)]
s1 = myS(10, "foo")
s1.buffer = "bar"
If, however, you have a string buffer object, you can take its value:
buf = create_string_buffer(64)
s1 = myS(10,buf.value)
Note also that
>>> type(create_string_buffer(100)) == 100*c_char
True