代码之家  ›  专栏  ›  技术社区  ›  Knio

Python ctypes地址为CFuncType

  •  3
  • Knio  · 技术社区  · 16 年前

    与我的 other question

    如何获取CFuncType对象的地址(实际函数指针)?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 回复  |  直到 9 年前
        1
  •  7
  •   Knio    16 年前

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