On 19/05/12 13:20:24, Nobody wrote:
> On Sat, 19 May 2012 11:30:46 +0200, Johannes Bauer wrote:
> 
>> import ctypes
>> libc = ctypes.cdll.LoadLibrary("/lib64/libc-2.14.1.so")
>> print(libc.strchr("abcdef", ord("d")))
> 
> In 3.x, a string will be passed as a wchar_t*, not a char*. IOW, the
> memory pointed to by the first argument to strchr() will consist mostly of
> NUL bytes.
> 
> Either use a "bytes" instead of a string:
> 
>       > print(libc.strchr(b"abcdef", ord("d")))
>       1984444291
> 
> or specify the argument types to force a conversion:
> 
>       > libc.strchr.argtypes = [c_char_p, c_int]
>       > print(libc.strchr("abcdef", ord("d")))
>       1984755787

You'll also want to specify the return type:

>>> libc.strchr.argtypes = [c_char_p, c_int]
>>> print(libc.strchr(b"abcdef", ord("d")))
7224211
>>> libc.strchr.restype = c_char_p
>>> print(libc.strchr(b"abcdef", ord("d")))
b'def'

Hope this helps,

-- HansM

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to