kadaj kadaj - 1 year ago 121
Python Question

ctypes: printf returns length which is int instead of the string

I'm using ctypes and loading msvcrt.dll in Python 2.5.

>>> from ctypes import *
>>> libname = 'msvcrt.dll'
>>> libc = CDLL(libname)
>>> libc.printf("Hello World\n")

Why doesn't it print
Hello World

Answer Source

The C printf() function itself is defined to return the number of characters printed to the output. This is the value that Python receives when you call libc.printf().

The ctypes tutorial provides information on why the output from printf() may not work within your Python REPL (my psychic debugging skills indicate that you're running the Windows GUI IDLE).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download