rkmax
rkmax

Reputation: 18133

ctypes.cast always throw ArgumentError

I have the following code

 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)

throw a ArgumentError from cast function ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type

the question is why? where is the error in my code i'm a newbiew with ctypes but i find examples like cast(c_int, POINTER(c_char_p)) for pŕint c_int without get a c_int(2)

Upvotes: 2

Views: 4047

Answers (1)

Vaughn Cato
Vaughn Cato

Reputation: 64308

From the documentation:

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.

I think you just want to do this:

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

Upvotes: 2

Related Questions