Recreating a char array from a pointer

B

buffinator

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? :/

/buffis
 
T

Thomas Heller

buffinator said:
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
 
B

buffinator

Oh, nice
Im running 2.4 but im gonna download it and give it a try then

Thanks!

Thomas said:
buffinator said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top