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

时间:2009-02-13 00:11:39

标签: python ctypes

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

目前我可以这样加载库:

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

并成功映射了一堆函数。但是,出于错误检查的目的,其中许多设置了pbs_errno变量,因此我也需要访问它。但是,如果我尝试访问它,我会得到:

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

当然,它不是一个函数指针,试图调用它会导致seg错误。

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

Objdump将符号显示为:

00000000001294f8 g    DO .bss   0000000000000004  Base        pbs_errno

1 个答案:

答案 0 :(得分:17)

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

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

e.g。

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