Get pointers to struct members in Python ctypes

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

Get pointers to struct members in Python ctypes
How to get a stacktrace in python
Get date in Python
How to get the files in the [Python] folder
What to do to get google spreadsheet in python
Use os.getenv to get environment variables in Python
How to get the variable name itself in python
How to get the number of digits in Python
Get YouTube Comments in Python
To flush stdout in Python
Get last month in python
Login to website in Python
Super tiny struct in python
Get Terminal size in Python
Explicitly get EOF in python
Speech to speech in python [text to speech]
How to develop in Python
Get Evernote notes in Python
Post to Slack in Python
Get Japanese synonyms in Python
Developed a library to get Kindle collection list in Python
I tried "How to get a method decorated in Python"
To return char * in a callback function using ctypes in Python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Hit REST in Python to get data from New Relic
Get Python scripts to run quickly in Cloud Run using responder
Call github api in python to get pull request information
[Python] How to do PCA in Python
Link to get started with python
Convert markdown to PDF in Python
Get data from Quandl in Python
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
Try to calculate Trace in Python
How to get started with Python
Get, post communication memo in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
Get the desktop path in Python
Get the host name in Python
Python version to get unused ports
How to handle Japanese in Python
Get started with Python in Blender
An alternative to `pause` in Python
What to do if you get a minus zero in Python
How to get a string from a command line argument in python
Hit the New Relic API in Python to get the server status
Get additional data in LDAP with python
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
Get exchange rates from open exchange rates in Python
Install Pyaudio to play wave in python
How to access environment variables in Python
Get Suica balance in Python (using libpafe)
I tried to implement permutation in Python