![](/img/trans.png)
[英]How to access data from pointer in struct from Python with ctypes?
[英]How do I access a typedef struct pointer to an internal object with ctypes?
我在 Python 3.6 中使用 ctypes 來連接 C 庫。 其中一個函數啟用庫並將類型定義返回到需要傳遞給后續函數調用的內部對象。 下面是一個例子:
// Type definition of the internal object.
typedef struct lib_internal_obj * lib_internal_obj_t;
/**
* Struct containing the available options for the library
*/
struct lib_options {
char ip[255];
int bufsize_MB;
};
/** Constructor of a lib object.
* \return A newly created lib object or NULL if construction failed
*/
extern lib_internal_obj_t lib_internal_obj_new(struct lib_options * options);
/**
* Example of how the internal object is used.
*
*/
extern int lib_get_options(lib_internal_obj_t obj, struct lib_options *options);
/**
* Destructor of the lib object.
*/
extern void lib_del(lib_internal_obj_t *obj);
我對如何在 Python 中使用 ctypes 返回和存儲此對象感到迷茫。 我認為它像 Python 中的普通函數調用一樣工作。 這是我嘗試過的:
import ctypes as c
class LIB_OPTIONS(c.Structure):
"""Struct containing the available options for the library"""
_fields_ = [
('ip', c.c_char * 255),
('bufsize_MB', c.c_int)
]
# Open C lib
c_lib = c.CDLL('./path/to/lib/lib.so', mode=1)
# Set options
lib_opts = LIB_OPTIONS()
lib_opts.ip = "192.168.1.1".encode('utf-8')
lib_opts.bufsize_MB = 2
# Not sure how to handle this
lib_object = c_lib.lib_internal_obj_new(c.byref(lib_opts))
當我運行它時,我收到以下錯誤:
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
我很確定這與clib.lib_internal_obj_new
調用有關。 有沒有辦法返回 typedef 結構並從 ctypes 正確存儲它。 任何幫助是極大的贊賞。
ctypes
出現問題的通常原因是沒有為被調用的函數定義.argtypes
和.restype
。 特別是,返回類型默認為c_int
(通常為 32 位),在 64 位系統上,您的函數將返回 64 位指針。
import ctypes as c
class LIB_OPTIONS(c.Structure):
"""Struct containing the available options for the library"""
_fields_ = [('ip', c.c_char * 255),
('bufsize_MB', c.c_int)]
c_lib = c.CDLL('./test')
c_lib.lib_internal_obj_new.argtypes = c.POINTER(LIB_OPTIONS), # 1-tuple declaring input parameter
c_lib.lib_internal_obj_new.restype = c.c_void_p # generic pointer
lib_opts = LIB_OPTIONS(b'192.168.1.1',2)
lib_object = c_lib.lib_internal_obj_new(c.byref(lib_opts))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.