Python ctypes addressof CFuncType

萝らか妹 提交于 2021-02-07 14:25:32

问题


Related to my other question

How can I get the address (acctual function pointer) to a CFuncType object? addressof() does not report the correct address.

C code:

extern "C" _declspec(dllexport)
int addr(int (*func)())
{
    int r = (int)func;
    return r;
}

Python code:

def test():
  return 42

t = CFUNCTYPE(c_int)
f = t(test)

print addressof(f)
print dll.addr(f)

Output:

7030864
3411932

trying to call *(7030864) from C causes a crash, but calling *(3411932) works as expected. What's wrong with addressof()?


回答1:


cast(f, c_void_p) gets the correct address from within python



来源:https://stackoverflow.com/questions/2038839/python-ctypes-addressof-cfunctype

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!