Python ctypes地址的CFuncType

时间:2010-01-10 22:04:30

标签: python ctypes

与我的other question

相关

如何获取CFuncType对象的地址(acctual函数指针)? addressof()不报告正确的地址。

C代码:

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

Python代码:

def test():
  return 42

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

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

输出:

7030864
3411932

尝试从C调用*(7030864)会导致崩溃,但调用*(3411932)会按预期工作。 addressof()有什么问题?

1 个答案:

答案 0 :(得分:6)

cast(f, c_void_p)从python中获取正确的地址