I am trying this simple ctypes example and getting the error mentioned
>>> from ctypes import create_string_buffer
>>> str = create_string_buffer("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\ctypes\__init__.py", line 59, in create_string_buffer
buf.value = init
TypeError: str/bytes expected instead of str instance
Does anyone know what am I doing wrong?
On the same note, I am trying to pass a pointer to a string to a C function from my python code so that I can perform some string operation there and return another string. Can someone give me some sample code on how to do that?
extern "C" __declspec(dllexport) char * echo(char* c)
{
// do stuff
return c;
}
With regards to getting it working, if you pass it a
bytesobject, it works:Looking at the code for
create_string_buffer:Doing it directly,
This works fine.
This shows that same behaviour. Looks to me as though you’ve found a bug.
Time to visit http://bugs.python.org. There are a few bugs related to
c_charandcreate_string_bufferwhich are in the same field, but none reporting that giving it astrnow fails (but there are definite examples showing it used to work in Py3K).