How to dereference a memory location from python ctypes?

前端 未结 1 349
醉话见心
醉话见心 2020-12-06 01:14

I want to replicate the following c code in python ctypes:

main() {
  long *ptr = (long *)0x7fff96000000;
  printf(\"%lx\",*ptr);
}

I can f

相关标签:
1条回答
  • 2020-12-06 01:55

    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)
    
    0 讨论(0)
提交回复
热议问题