Recreating a char array from a pointer
Thomas Heller
theller at ctypes.org
Sat Feb 24 11:42:36 EST 2007
buffinator schrieb:
> I have an application that has to send a string as a pointer to memory,
> and then another one that has to retriece it and fetch the string.
> Converting the string to an array and sending the pointer was easy
>
> import array
> a=array.array("c","mytextgoeshere")
> my_pointer = a.buffer_info()[0]
>
>
> But then I tried to get the data back... and I just can't find out how.
> I ended up writing a C module for this that works fine in linux, but
> this application is meant for windows and I just can't get C-modules to
> compiler there correctly since I suck at windows.
>
> The relevant code from the module is
>
> static PyObject *
> pointrtostr_casttostr(PyObject *self, PyObject *args)
> {
> char *mystr;
> long int p;
>
> if (!PyArg_ParseTuple(args, "l", &p))
> return NULL;
>
> mystr = (char*)p;
> return Py_BuildValue("s", mystr);
> }
>
> My question is... can I do the above code in python without involving C
> since it is quite a big hassle to have to go through the module building
> in windows? :/
You can use the ctypes package for this. It is in the standard library in Python 2.5,
for older versions it is a separate download.
Thomas
More information about the Python-list
mailing list