Get the memory address pointed to by a ctypes pointer

I have fixed this myself by reading the documentation.

I wanted to know the memory location of a block of memory allocated by a library. I had the ctypes pointer that pointed to said block. To get the memory address of the block I used ctypes.addressof(p_block.contents).

The confusion arose around my understanding that p_block.contents != p_block.contents, but then I realised all p_block.contents objects have the same underlying buffer. The address of the underlying buffer is obtained with ctypes.addressof.


real_adr = cast(anyctypespointer, c_void_p).value

Works for any kind of ctypes pointer - even function pointers etc. which lack .content

Tags:

Python

Ctypes