Re: How to pass String BYREF to C function?

TP <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
On Wed, Aug 18, 2010 at 5:33 AM, marco cassisa <[email protected]> wrote:
> Hi
>
> 2010/8/18 Andrew Wilson <[email protected]>
>>
>> Hey Lynton,
>>   Your c code needs to properly terminate the string with a '\n' in order
>> for python to print the result correctly.
>>   Andrew
>>
>> On Wed, Aug 18, 2010 at 8:11 AM, Lynton Grice
>> <[email protected]> wrote:
>>>
>>> ....
>>>
>>> I get the following output in WingIDE
>>>
>>>
>>>
>>> helloö
>
> but create_string_buffer() shouldn't take care of terminating the string
> with 0?
>
> following
> http://docs.python.org/library/ctypes.html
>  You should be careful, however, not to pass them to functions expecting
> pointers to mutable memory. If you need mutable memory blocks, ctypes has a
> create_string_buffer() function which creates these in various ways. The
> current memory block contents can be accessed (or changed) with the raw
> property; if you want to access it as NUL terminated string, use the value
> property:
>
> anyway, the second argument of create_string_buffer() can be the length of
> the string buffer array, so you can set it to the string's lenght +1
> bye
> marco
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by
>
> Make an app they can't live without
> Enter the BlackBerry Developer Challenge
> http://p.sf.net/sfu/RIM-dev2dev
> _______________________________________________
> ctypes-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/ctypes-users
>
>

I also have to say I'm not convinced that he needs to use
create_string_buffer(). After all Lynton's  code is NOT modifying the
first argument. It's merely copying bytes from it. And the error
message he originally got was about the 2nd argument not the first.

So he should be able to just say:

            result = executeUserExit("hello, world", SOME UNKNOWN CODE
FOR THIS ARG)

The way Andrew treats the 2nd argument also seems "incorrect" to me
even if it works. There ought to be a way to say that the 2nd argument
is a ptr to a char ptr and that the called function is going to set
it. I haven't used ctypes enough however to figure out what the right
answer is without writing my own test case :(

Also depending on what version of python you are using (or the use of
the "from __future__ import unicode_literals) strings may not be bytes
anymore. But I gather from what he's seeing that he must not be using
Python 3.0 or using that special __future__ statement.

I find it interesting that we have so many non-definitive replies so
far. Maybe all the ctypes gurus aren't up yet?

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev
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.