gpt4 book ai didi

python - 使用 ctypes 从共享库映射全局变量

转载 作者:太空狗 更新时间:2023-10-30 00:19:29 25 4
gpt4 key购买 nike

我想映射一个 int 值 pbs_errno 使用 ctypes 在库 libtorque.so 中声明为全局。

目前我可以像这样加载库:

from ctypes import *
libtorque = CDLL("libtorque.so")

并成功映射了一堆功能。然而,出于错误检查的目的,他们中的许多人设置了 pbs_errno 变量,所以我也需要访问它。但是,如果我尝试访问它,我会得到:

>>> pytorque.libtorque.pbs_errno
<_FuncPtr object at 0x9fc690>

当然,它不是函数指针,尝试调用它会导致段错误。

它在主头文件中声明为 int pbs_errno;,在 API 头文件中声明为 extern int pbs_errno;

Objdump 将符号显示为:

00000000001294f8 g    DO .bss   0000000000000004  Base        pbs_errno

最佳答案

ctypes 文档中有一节关于访问 dll 中导出的值:

http://docs.python.org/library/ctypes.html#accessing-values-exported-from-dlls

例如

def pbs_errno():    return c_int.in_dll(libtorque, "pbs_errno")

关于python - 使用 ctypes 从共享库映射全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29203765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com