csl
csl

Reputation: 11358

How to get the pointer address of a ctypes.c_char_p instance

I want to extract the integer address that a ctypes.c_char_p instance points to.

For example, in

>>> import ctypes
>>> s = ctypes.c_char_p("hello")
>>> s
c_char_p(4333430692)

the value I'd like to fetch is 4333430692 — the address of the string hello\0 in memory:

(lldb) x 4333430692
0x1024ae7a4: 68 65 6c 6c 6f 00 5f 70 00 00 00 00 05 00 00 00  hello._p........

I've read the ctypes docs, but there doesn't seem to be any way of doing that. The closest is ctypes.addressof, but that only gives me the location of the pointer, of course.

The reason why I want this is because I'm calling some C functions that actually expects raw addresses encoded as integers (whose size equals the native pointer width).

Upvotes: 6

Views: 4926

Answers (1)

tynn
tynn

Reputation: 39843

You could just cast it to c_void_p and get the value:

>>> ctypes.cast(s, ctypes.c_void_p).value
4333430692

Upvotes: 12

Related Questions