在 xlib 上使用 ctypes 时出现分段错误
Getting segmentation fault when using ctypes on xlib
我正在尝试从 xlib 屏幕保护程序中获取空闲时间
我试过调试它,错误开始出现在行
之后
dpy = xlib.XOpenDisplay(os.environ['DISPLAY'].encode('ascii'))
这是我的代码
class XScreenSaverInfo( ctypes.Structure):
""" typedef struct { ... } XScreenSaverInfo; """
_fields_ = [('window', ctypes.c_ulong), # screen saver window
('state', ctypes.c_int), # off,on,disabled
('kind', ctypes.c_int), # blanked,internal,external
('since', ctypes.c_ulong), # milliseconds
('idle', ctypes.c_ulong), # milliseconds
('event_mask', ctypes.c_ulong)] # events
xlib = ctypes.cdll.LoadLibrary('libX11.so')
dpy = xlib.XOpenDisplay(os.environ['DISPLAY'].encode('ascii'))
root = xlib.XDefaultRootWindow(dpy)
xss = ctypes.cdll.LoadLibrary( 'libXss.so')
xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)
xss_info = xss.XScreenSaverAllocInfo()
xss.XScreenSaverQueryInfo( dpy, root, xss_info)
print("Idle time in milliseconds: %d") % xss_info.contents.idle
我收到 Segmentation fault (core dumped)
错误。
请帮助:)
错误是因为 argtypes(和 restype)没有被指定,如 [Python 3.Docs]: ctypes - Specifying the required argument types (function prototypes) 中所述。在这种情况下发生的事情有很多例子,这里有两个:
在调用每个函数之前声明它们:
xlib.XOpenDisplay.argtypes = [ctypes.c_char_p]
xlib.XOpenDisplay.restype = ctypes.c_void_p # Actually, it's a Display pointer, but since the Display structure definition is not known (nor do we care about it), make it a void pointer
xlib.XDefaultRootWindow.argtypes = [ctypes.c_void_p]
xlib.XDefaultRootWindow.restype = ctypes.c_uint32
xss.XScreenSaverQueryInfo.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.POINTER(XScreenSaverInfo)]
xss.XScreenSaverQueryInfo.restype = ctypes.c_int
我正在尝试从 xlib 屏幕保护程序中获取空闲时间
我试过调试它,错误开始出现在行
之后dpy = xlib.XOpenDisplay(os.environ['DISPLAY'].encode('ascii'))
这是我的代码
class XScreenSaverInfo( ctypes.Structure):
""" typedef struct { ... } XScreenSaverInfo; """
_fields_ = [('window', ctypes.c_ulong), # screen saver window
('state', ctypes.c_int), # off,on,disabled
('kind', ctypes.c_int), # blanked,internal,external
('since', ctypes.c_ulong), # milliseconds
('idle', ctypes.c_ulong), # milliseconds
('event_mask', ctypes.c_ulong)] # events
xlib = ctypes.cdll.LoadLibrary('libX11.so')
dpy = xlib.XOpenDisplay(os.environ['DISPLAY'].encode('ascii'))
root = xlib.XDefaultRootWindow(dpy)
xss = ctypes.cdll.LoadLibrary( 'libXss.so')
xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)
xss_info = xss.XScreenSaverAllocInfo()
xss.XScreenSaverQueryInfo( dpy, root, xss_info)
print("Idle time in milliseconds: %d") % xss_info.contents.idle
我收到 Segmentation fault (core dumped)
错误。
请帮助:)
错误是因为 argtypes(和 restype)没有被指定,如 [Python 3.Docs]: ctypes - Specifying the required argument types (function prototypes) 中所述。在这种情况下发生的事情有很多例子,这里有两个:
在调用每个函数之前声明它们:
xlib.XOpenDisplay.argtypes = [ctypes.c_char_p]
xlib.XOpenDisplay.restype = ctypes.c_void_p # Actually, it's a Display pointer, but since the Display structure definition is not known (nor do we care about it), make it a void pointer
xlib.XDefaultRootWindow.argtypes = [ctypes.c_void_p]
xlib.XDefaultRootWindow.restype = ctypes.c_uint32
xss.XScreenSaverQueryInfo.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.POINTER(XScreenSaverInfo)]
xss.XScreenSaverQueryInfo.restype = ctypes.c_int