Skip to main content
 首页 » 编程设计

python之ctypes.cast 总是抛出 ArgumentError

2024年11月01日8Leo_wl

我有以下代码

 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))