ctypes - Pointer from Address April 27, 2011
There are times you need to construct a ctypes
pointer from an integer address you have,
say the id
of a python object. I scratched my head for quite a while until I found out a how
to do it properly (with some help from the stackoverflow guys). Here’s what I got:
Example
Some words of caution:
- I’m relying here on
id
returning the address of an object. This is a weak assumption, but it holds (and is likely to continue to hold) for CPython. - There are APIs for what I showed in the example above… use them instead!
I’m using this code to dig into the vicious [OVERLAPPED](http://msdn.microsoft.com/en-us/library/ms684342(v=vs.85%29.aspx) structure that’s held inside PyOVERLAPPED for really low-level hacking… Anyway, if anyone finds this recipe useful, feel free to use it.