Win32 dll / ctypes question
Thomas Heller
theller at python.net
Mon Dec 9 05:25:57 EST 2002
"nospam matthewm" <"nospam matthewm"@ihug.co.nz> writes:
> First off - AWESOME module.
> Can you (and if so how-to) access an exported static struct in a Win32
> dll using python and/or ctypes?
Sure, all you need is the address of the structure.
The following sample accesses some type structures in
python itself, it relies on the fact that id(obj) returns
the address of the object:
from ctypes import windll, Structure
class PythonType(Structure):
_fields_ = [
("ob_refcnt", "i"),
("ob_type", "i"),
("ob_size", "i"),
("tp_name", "z"),
("tp_basicsize", "i"),
("tp_itemsize", "i"),
("tp_dealloc", "i"),
("tp_print", "i"),
("tp_getattr", "i"),
("tp_setattr", "i"),
("tp_compare", "i"),
# and so on...
]
for obj in (type, int, float, dict, tuple):
p = PythonType.from_address(id(obj))
print p.tp_name, p.tp_basicsize, p.tp_itemsize
When executed, it prints:
type 436 20
int 12 0
float 16 0
dict 124 0
tuple 12 4
Thomas
More information about the Python-list
mailing list