Python 3.5 ctypes libc printf() 仅打印字符串的第一个字节 Windows 7/10

标签 python c windows pydev

我正在尝试遵循 Gray Hat Python 示例之一,它在 Python 2.7 中运行良好,但在 Python 3.5 中结果被截断。

from ctypes import * 

msvcrt = cdll.msvcrt
message_string = "Hello World!\n"
msvcrt.printf("Testing: %s\n", message_string)

您可以在下面看到上面代码的输出只是字母 T

enter image description here

基于一些与此类似的其他帖子,在最后一行添加 b 会有所帮助,但随后 message_string 会被截断。

from ctypes import * 

msvcrt = cdll.msvcrt
message_string = "Hello World!\n"
msvcrt.printf(b"Testing: %s\n", message_string)

enter image description here

如何在 Windows 7 或 10 上使用 Python 3.5 打印存储在 message_string 变量中的整个字符串?

最佳答案

开始工作了!也需要在变量声明中添加 b。这么小的细节也...请参阅下面调整后的代码:

from ctypes import * 

msvcrt = CDLL('msvcrt')
message_string = b"Hello World!\n"
msvcrt.printf(b"Testing: %s\n", message_string)

在 Windows 7 64 位、带 Python 3.5 和 Windows 10 64 位、带 Python 3.4 上测试

enter image description here

Gray Hat Python - chapter1-printf.py - 示例

关于Python 3.5 ctypes libc printf() 仅打印字符串的第一个字节 Windows 7/10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37474192/

相关文章:

c - stdarg.h中定义的函数va_start(va_list ap,parmN),为什么标识符parmN不能用寄存器存储类声明?

python - 在python中使用多个进程记录到一个文件

python - 将背景图片设置为QTextEdit

python - 有没有办法接受参数选择作为单个字符串

c - 在 C 中子方法内使用循环的奇怪结果

c - c-mpi 程序在本地执行失败

c# - 如何以编程方式更改 LAN 设置(代理配置)

c++ - QNetworkProxy - 绕过某些地址

windows - 让 FilePaths 在 Batch 中正常工作(写入文本文件)

python - 根据命令行参数调用 python 函数