ctypes.cast.

>>> import ctypes
>>> c_long_p = ctypes.POINTER(ctypes.c_long)
>>> some_long = ctypes.c_long(42)
>>> ctypes.addressof(some_long)
4300833936
>>> ctypes.cast(4300833936, c_long_p)
<__main__.LP_c_long object at 0x1005983b0>
>>> ctypes.cast(4300833936, c_long_p).contents
c_long(42)
Answer from habnabit on Stack Overflow
🌐
tentacles666
tentacles666.wordpress.com › 2012 › 01 › 21 › python-ctypes-dereferencing-a-pointer-to-a-c
Python ctypes: dereferencing a pointer to a C structure | tentacles666
March 19, 2013 - Next, you need to tell ctypes that newfoo() returns a pointer to such a thing: ... Finally, after invoking newfoo(), you need to use the pointer’s contents member to dereference it:
🌐
Behaviour
behaviour.space › posts › 2021-03-28-ctypes-weird-and-inconvenient-typing.html
The weird and inconvenient typing of Python's ctypes library
March 28, 2021 - In the first graph we’ll use the code from above. Pythas is designed with this issue in mind. It doesn’t actually hand over char-pointers but char-pointer-pointers! Hence, ctypes doesn’t automatically convert the string and we can dereference the pointer after conversion.
🌐
Python
docs.python.org › 3 › library › ctypes.html
ctypes — A foreign function library for Python
>>> null_ptr = POINTER(c_int)() >>> print(bool(null_ptr)) False >>> ctypes checks for NULL when dereferencing pointers (but dereferencing invalid non-NULL pointers would crash Python):
🌐
CodersLegacy
coderslegacy.com › home › python › using pointers with ctypes and python
Using Pointers with Ctypes and Python - CodersLegacy
July 8, 2022 - First we create a variable using ... then return a pointer object. By using the .contents attribute, we can dereference a pointer and gain access to the original object it was pointing to....
🌐
Netlify
pointerspy.netlify.app › reference
Reference - pointers.py
Dereference the pointer. ... Move/copy a value into the memory at the pointers address. ... Base class for a pointer that has a size attribute. ... Size of the target value. Convert the address to a ctypes pointer.
🌐
GeeksforGeeks
geeksforgeeks.org › using-pointers-in-python-using-ctypes
Using Pointers in Python using ctypes - GeeksforGeeks
January 9, 2023 - In languages like C/C++ pointers ... that pointer variable i.e while printing it we put the certain format specifier and then use an asterisk (*) with that variable to get the value it was pointing....
Top answer
1 of 1
1

Function pointers can use CFUNCTYPE(retval,params...) to declare C function pointers. The code below is a guess based on the description and minimal code:

test.c

#if defined(_WIN32)
#   define API __declspec(dllexport)
#else
#   define API
#endif

#include <stdlib.h>

struct instance; // forward declaration...

typedef int (*callback)(struct instance *); // so callback can be defined...

typedef struct instance { // and structure declared.
    int x;
    callback f;
} instance;

int func(instance* p) {  // Callback
    return ++p->x;
}

API instance* get_instance(void) {
    instance* p = malloc(sizeof(instance));
    p->x = 5;
    p->f = func;
    return p;
}

API void free_instance(instance* p) {
    free(p);
}

test.py

from ctypes import *

# Forward declaration...
class Instance(Structure):
    pass

# so the callback parameter pointer can be declared...
CALLBACK = CFUNCTYPE(c_int,POINTER(Instance))

# and then the fields can be defined.
Instance._fields_ = (('x',c_int),
                     ('f',CALLBACK))

dll = CDLL('./test')
dll.get_instance.argtypes = ()
dll.get_instance.restype = POINTER(Instance)
dll.free_instance.argtypes = POINTER(Instance),
dll.free_instance.restype = None

instance = dll.get_instance()
try:
    print(instance.contents.x)
    print(instance.contents.f(instance))
    print(instance.contents.f(instance))
    print(instance.contents.f(instance))
finally:
    dll.free_instance(instance)

Output:

5
6
7
8

References:

  • https://docs.python.org/3/library/ctypes.html#incomplete-types
  • https://docs.python.org/3/library/ctypes.html#callback-functions
🌐
Python
docs.python.org › 2.5 › lib › ctypes-pointers.html
14.14.1.14 Pointers
December 23, 2008 - >>> null_ptr = POINTER(c_int)() >>> print bool(null_ptr) False >>> ctypes checks for NULL when dereferencing pointers (but dereferencing non-NULL pointers would crash Python):
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 54353482 › ctypes-nasm-how-to-dereference-a-pointer-to-an-array-of-pointers
CTypes NASM - how to dereference a pointer to an array of pointers - Stack Overflow
UPDATE: the code below DOES work to dereference the pointer. I had incorrectly inserted some lines at the entry point that had the effect of overriding the memory location f1_ptr. The important ...
🌐
OCaml
ocaml.org › p › ctypes › 0.8.1 › doc › Ctypes › index.html
ctypes 0.8.1 · OCaml Package
A null pointer. ... !@ p dereferences the pointer p. If the reference type is a scalar type then dereferencing constructs a new value.
🌐
Tomerfiliba
tomerfiliba.com › recipes › ctype-pointers
ctypes - Pointer from Address - Tomer Filiba
import ctypes def deref(addr, typ): return ctypes.cast(addr, ctypes.POINTER(typ)).contents
🌐
Dsheets
dsheets.github.io › codoc › ctypes.0.3.4 › _build › src › ctypes › ctypes
Ctypes
A null pointer. ... !@ p dereferences the pointer p. If the reference type is a scalar type then dereferencing constructs a new value.
🌐
Python
bugs.python.org › issue41883
Issue 41883: ctypes pointee goes out of scope, then pointer in struct dangles and crashes - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/86049
🌐
Post.Byes
post.bytes.com › home › forum › topic › python
ctypes: return a pointer to a struct - Post.Byes - Bytes
Re: ctypes: return a pointer to ... LP_IP2LocationR ecord has no attribute called 'country_short' . IP2LocationReco rd does. > Use the 'contents' attribute to dereference the pointer....
🌐
OCaml
ocaml.org › p › ctypes › 0.9.4 › doc › Ctypes › index.html
ctypes 0.9.4 · OCaml Package
A null pointer. ... !@ p dereferences the pointer p. If the reference type is a scalar type then dereferencing constructs a new value.
🌐
SourceForge
sourceforge.net › home › browse › ctypes › mailing lists
[ctypes-users] Re: Dereferencing a pointer | ctypes
July 6, 2004 - ctypes-commit · ctypes-devel · ctypes-users · View entire thread · Thanks for helping keep SourceForge clean.
🌐
Hacker News
news.ycombinator.com › item
Pointers.py: Bringing the hell of pointers to Python | Hacker News
March 10, 2022 - Where `to_ptr(x)` is: · ptr = ctypes.pointer(ctypes.py_object(x)) And `ptr.dereference()` is: