I have am inside a callback that allows me to access the pointers passed to it as int (i.e. the Python type() function returns int).
What so I have to do if I want convert this into a pointer of a struct whose layout I know and can define as a ctypes class. Say I have this already:
class data_t(Structure):
_fields = [ ("foo", c_int), ("bar", c_wchar_p), ("baz", c_char_p) ]
How can a variable x for which type(x) gives the output int now be cast into a pointer to the above struct type?
The gist: I have no influence on the declaration of the callback function or the declaration of the arguments I get to see, so I need some way to get from the Python int type to a ctypes pointer and access the struct members from there …
This is Python 2.6, but I reckon most 2.x will be similar enough at least. I can’t get rid of this version requirement as it is embedded into a product that requires this particular Python version.
You can use
ctypes.cast, or just declare the callback to return aPOINTER(data_t). Examples of both below and tested on Python 2.7 and Python 3.2:test DLL code (Windows)
Python
Output