原文代码
from ctypes import *
msvcrt = cdll.msvcrt
message_string = "Hello World!\n"
msvcrt.printf("Testing:%s",message_string)
运行结果
...Python/codes/connectdlll/chapter1-printf.py"
T
Process finished with exit code 0
原因分析
因为Python3使用的是Unicode编码,而printf不支持该编码,所以需要转码
解决方案
# 方案1
from ctypes import *
msvcrt = cdll.msvcrt
message_string = b"Hello World!\n"
msvcrt.printf(b"Testing:%s",message_string)
# 方案2
from ctypes import *
msvcrt = cdll.msvcrt
message_string = "Hello World!\n".encode("utf-8")
msvcrt.printf("Testing:%s".encode("utf-8"),message_string)
# 方案3
from ctypes import *
msvcrt = cdll.msvcrt
message_string = "Hello World!\n"
msvcrt.wprintf("Testing:%s",message_string)
最终效果
...Python/codes/connectdlll/chapter1-printf.py"
Testing:Hello World!
Testing:Hello World!
Testing:Hello World!
Process finished with exit code 0
完成
资源来源于网络,如有侵权请联系作者删除!