我有以下代码
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/