Re: Question about strings and pointers

Valentin Haenel <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
* Valentin Haenel <[email protected]> [100220]:
> * Ananda Regmi <[email protected]> [100208]:
> > Try this to create the ctypes string in python and then pass pointer  with
> > your function call.
> > 
> > import ctypes
> > temp_str = 'this is the substring we would like to print'
> > my_string = ctypes.create_string_buffer(temp_str, len(temp_str))
> 
> O.k. and how would i obtain pointers into the buffer? for example my_string[12]
> again returns an object of type str.

Well then, after playing with it for a while now, i have a working solution, not
exactly elegant, but it works. The idea of using a string_buffer was a pretty
good start.

The trick i used is to specify the argument types of the c function as c_int
instead of POINTER(c_char). I can then use 'addressof' to obtain the location in
memory of the string (in fact a pointer to the first character of the string).
Lastly i used multiples of sizeof(int) to index into the string_buffer.

code is attached.

V-

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev

_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users
demo-wrapper.py (text/x-python, 312 B)
from ctypes import *
lib = cdll.LoadLibrary('./libdemo-library.so.1')
lib.print_substring.argtypes = [c_int, c_int]
test = "this is the substring we would like to print"
test_buf = create_string_buffer(test, len(test))
add = addressof(test_buf)
lib.print_substring(add+12*sizeof(c_char), add+21*sizeof(c_char))
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.