gpt4 book ai didi

ctypes 中的 python 和 cdll。不能使用 printf

转载 作者:行者123 更新时间:2023-11-28 17:50:45 25 4
gpt4 key购买 nike

我是 Python 的初学者。我使用的python版本是3.2.1.1我正在尝试按照 docs.python.org 中的教程学习 ctypes

在交互式提示中,

import ctypes
libc = cdll.msvcrt
printf = libc.printf
printf("%d", 42)

它应该返回值 42但就我而言,它返回 0。所以有什么问题?非常感谢。


现在我添加一个>>>from ctypes import cdll后,结果变成了这样

>>>from ctypes import *
>>>libc = cdll.msvcrt
>>>printf = libc.printf
>>>printf("%d", 42)
Traceback (most recent call last):
File "<stdin>", line1, in <module>
TypeError: 'CDLL' object is not callable

最佳答案

实际上,它应该返回 2 作为写入 stdout 的字节数。它似乎在我的 Windows 安装上运行良好(在我添加缺少的 from ctypes import cdll 之后)。您使用的是 Windows 吗? msvcrt 是 Windows 专用的 DLL。

关于ctypes 中的 python 和 cdll。不能使用 printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10595026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com