I wanted to do something like this with Python ctypes
typedef struct{
int x;
int y;
} point;
point p = {100, 200};
someFunc(&p.y); //Pass a pointer to y
Just where I wrote it normally
from ctypes import *
class Point(Structure):
_fields_ = [("x", c_int), ("y", c_int)]
p = Point(100, 200)
somedll.someFunc(byref(p.y))
TypeError: byref() argument must be a ctypes instance, not 'int'
Get angry
The value you get with p.y
is not a ctypes instance, it's just a (Python) int number, so you can't reference it by byref
do this
y_offset = Point.y.offset
y_addr = addressof(p) + y_offset
somedll.someFunc(y_addr)
I got the pointer or the address itself, but it's probably the same.
Thanks
Class object (of a subclass of Structure) .member name.offset
Seems to be able to get the offset of that member from the beginning of the structure
If you get the address of the beginning of the structure and add the offset, you can get the address of the member on a sunny day.
By the way
Class object.member name.size
You can get the size of that member with
If you use it a lot, it would be better to make it a function as follows.
def get_address_of_member(obj, member_name):
return addressof(obj) + obj.__class__.__dict__[member_name].offset
p = Point(100, 200)
somedll.someFunc(get_address_of_member(p, "y"))
I don't think it's smart, but it's okay If there is a better way, I would appreciate it if you could teach me.
Recommended Posts