Reputation: 313
printf
returns 1 instead of "Hello World!" which is the desired result.
I googled it and think its because of the changes in the way sequences are treated.
How do I modify the code to print "Hello World!"?
www.mail-archive.com/[email protected]/msg15119.html
import ctypes
msvcrt=ctypes.cdll.msvcrt
string=b"Hello World!"
msvcrt.printf("%s", string)
Upvotes: 2
Views: 3053
Reputation: 110108
The first argument needs to be a byte string as well:
msvcrt.printf(b"%s", string)
The return value of printf is the number of characters printed, which should be 12 in this case.
Edit:
If you want the string to be returned instead of printed, you can use sprintf
instead. This is dangerous and NOT recommended.
s = ctypes.create_string_buffer(100) #must be large enough!!
msvcrt.sprintf(s, b'%s', b'Hello World!')
val = s.value
I don't know why you'd want to do this though, since Python has its own string formatting. sprintf
is a dangerous method since it is susceptible to buffer overflows.
Upvotes: 4