我可以用Python中的ctypes成功调用函数。现在我有一个填充了数据结构的缓冲区,我想要提取这些数据。最好的策略是什么?还有什么其他需要发布的内容吗?
函数:
函数:
class list():
def __init__(self):
#[...]
def getdirentries(self, path):
self.load_c()
self.fd = os.open(path, os.O_RDONLY)
self.statinfo = os.fstat(self.fd)
self.buffer = ctypes.create_string_buffer(self.statinfo.st_size)
nbytes = self.statinfo.st_size
transferred_bytes = self.libc.getdirentries(
self.fd,
ctypes.byref(self.buffer),
nbytes,
ctypes.byref(self.basep) )
#[...]
结构:
class dirent(ctypes.Structure):
_fields_ = [ ("d_fileno", ctypes.c_uint32), # /* file number of entry */
("d_reclen", ctypes.c_uint16), # /* length of this record */
("d_type", ctypes.c_uint8), # /* file type */
("d_namlen", ctypes.c_uint8), # /* length of string in d_name */
("d_name", ctypes.c_char * (MAXNAMELEN + 1) ) ]
输出如下:
已传输字节数:156
缓冲区大小:272
缓冲区内容:<ctypes.c_char_Array_272 object at 0x8c3f0>