473,734 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recreating a char array from a pointer

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","mytextgoesh ere")
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_cas ttostr(PyObject *self, PyObject *args)
{
char *mystr;
long int p;

if (!PyArg_ParseTu ple(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
Feb 24 '07 #1
2 1425
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","mytextgoesh ere")
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_cas ttostr(PyObject *self, PyObject *args)
{
char *mystr;
long int p;

if (!PyArg_ParseTu ple(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

Feb 24 '07 #2
Oh, nice
Im running 2.4 but im gonna download it and give it a try then

Thanks!

Thomas Heller wrote:
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","mytextgoes here")
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_ca sttostr(PyObjec t *self, PyObject *args)
{
char *mystr;
long int p;

if (!PyArg_ParseTu ple(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
Feb 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
20950
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
10
3466
by: techie | last post by:
I'm creating a class BookType that will store information about books. Each book type can have up to 4 authors. I defined a new type for an array of char: typedef char array4_t; This will hold 25 characaters I declared a 2 dimensional array of char in BookType:
21
18882
by: Bret | last post by:
I'm curious why char** argv is acceptable in the main() declaration. In the comp.lang.c FAQ (question 6.18) it says that pointers to pointers and pointers to an array are not interchangable. However the declaration: int main(int argc, char** argv) is common.
3
2209
by: sieg1974 | last post by:
Hi, I have made this simple program to understand char ** pointers, but I still having many questions. int main() { char ** testPointerPointerChar = 0; char * A = "string01";
5
3978
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
33
3181
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
15
1828
by: RAB | last post by:
I am programming in the palm environment using C++ and am stuck. I have created an char array: const char MyArray = { {"string1"}, {"string2"}, .... {"string50"} }
3
3494
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/ #include <cstdio> #define MAX_WORD_LEN 80 #define MAX_SIZE 1000
30
3275
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
0
8777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9184
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6737
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6033
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4551
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3262
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.