python - ctypes.cast 总是抛出 ArgumentError

标签 python python-3.x ctypes

我有以下代码

 from ctypes import cast, c_char_p, c_int, byref, POINTER

 # ...
 mj, mn, pt = c_int(), c_int(), c_int()
 // FreeType
 __dll__.FT_Library_Version(__handler__, byref(mj), byref(mn), byref(pt))

 print("{0}.{1}.{2}".format(cast(mj, POINTER(c_char_p).value, cast(mn, POINTER(c_char_p).value, cast(pt, POINTER(c_char_p)).value)

抛出一个ArgumentError来自cast功能ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type

问题是为什么?我的代码中的错误在哪里我是 ctypes 的新手,但我找到了类似 cast(c_int, POINTER(c_char_p)) 的示例用于 pŕint c_int没有得到c_int(2)

最佳答案

来自文档:

The cast() function can be used to cast a ctypes instance into a pointer to a different ctypes data type. cast() takes two parameters, a ctypes object that is or can be converted to a pointer of some kind, and a ctypes pointer type. It returns an instance of the second argument, which references the same memory block as the first argument.

我想你只是想这样做:

print("%d.%d.%d"%(mj.value,mn.value,pt.value))

关于python - ctypes.cast 总是抛出 ArgumentError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16909254/

相关文章:

python - 如何从字典的值打印 Unicode 表情符号

python - 如何使用 sorted() 同时对两列进行排序

python - ChromeDriver 意外退出

python - 将 dict 中的键添加到现有 Pandas 数据框中的列标题

python - 将 python bool 值转换为 javascript bool 值的更简洁的方法是什么?

python - 从 mod_wsgi django 应用程序导入 numpy 时偶尔出现 ctypes 错误

python - 将 kivy 与 pywinauto 一起使用时出现 ctypes.ArgumentError

python - Python:将十六进制字节流转换为 “int16"

python - 使用 SQLAlchemy 添加多对多关系

c - 编译共享库时出现链接错误