from ctypes import *
msvcrt = cdll.msvcrt
message_string = "Hello world!\n"
msvcrt.printf("Testing: %s", message_string)
I’m going through a book about Ctypes and Python but the example code just doesn’t work.
Could it be because the book was written for python 2 whereas I am on Python 3?
printf is only printing the first letter.
The C
printffunction is expecting byte strings. In Python 3 all strings are unicode so you’ll have to encode to bytes:If you have any non-ascii characters then encode to the relevant windows codepage instead.