Re: How to get binary data to callback function
"Mark Tolonen" <[email protected]>
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
"Nikolaus Rath" <[email protected]> wrote in message news:[email protected]... > Nikolaus Rath <[email protected]> writes: >> "Martin (gzlist)" <[email protected]> writes: >>> On 22/12/2009, Nikolaus Rath <[email protected]> wrote: >>>> >>>> When I define a callback function with a prototype of >>>> >>>> ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_int) >>> >>> You have the prototype wrong, c_char_p is for null-terminated strings: >>> >>> <http://docs.python.org/library/ctypes.html#ctypes.c_char_p> >>> >>> You want to use `ctypes.POINTER(ctypes.c_char)` instead for the >>> definition. >> >> Ah, tricky. Thanks for helping out. I have just submitted a doc patch to >> make >> this more clear (http://bugs.python.org/issue7569). > > This actually leads to a new question. How can I construct a > POINTER(c_char)? c_char_p('string') works nicely, but > POINTER(c_char)('string') raises a TypeError. > > What's the trick that I need here? POINTER(c_char)(c_char('a')), but that only gives you character. Take a look at c_buffer: >>> b=c_buffer('abcdef\0defghi',20) >>> b <ctypes.c_char_Array_20 object at 0x01361A80> >>> b.raw 'abcdef\x00defghi\x00\x00\x00\x00\x00\x00\x00' >>> b.value 'abcdef' >>> b[6] '\x00' Note .value returns the first null-terminated string in the buffer. Use .raw if you want the whole thing. -Mark ------------------------------------------------------------------------------ This SF.Net email is sponsored by the Verizon Developer Community Take advantage of Verizon's best-in-class app development support A streamlined, 14 day to market process makes app distribution fast and easy Join now and get one step closer to millions of Verizon customers http://p.sf.net/sfu/verizon-dev2dev