相关疑难解决方法(0)

如何从python ctypes中取消引用内存位置?

我想在python ctypes中复制以下c代码:

main() {
  long *ptr = (long *)0x7fff96000000;
  printf("%lx",*ptr);
}
Run Code Online (Sandbox Code Playgroud)

我可以弄清楚如何将此内存位置称为函数指针,但不仅仅是执行正常的取消引用:

from ctypes import *
"""
>>> fptr = CFUNCTYPE(None, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/ctypes/__init__.py", line 104, in CFUNCTYPE
    class CFunctionType(_CFuncPtr):
TypeError: Error when calling the metaclass bases
    item 1 in _argtypes_ has no from_param method
"""
fptr = CFUNCTYPE(None, c_void_p) #add c_void_p since you have to have an arg
fptr2 = fptr(0x7fff96000000)
fptr2(c_void_p(0))
#python: segfault at 7fff96000000 ip 00007fff96000000 …
Run Code Online (Sandbox Code Playgroud)

python ctypes ffi

13
推荐指数
1
解决办法
2万
查看次数

标签 统计

ctypes ×1

ffi ×1

python ×1