kadaj kadaj - 7 months ago 31
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


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).