Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问指向具有ctypes的内部对象的typedef结构指针?_Python_C_Shared Libraries_Ctypes - Fatal编程技术网

Python 如何访问指向具有ctypes的内部对象的typedef结构指针?

Python 如何访问指向具有ctypes的内部对象的typedef结构指针?,python,c,shared-libraries,ctypes,Python,C,Shared Libraries,Ctypes,我正在使用Python3.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_

我正在使用Python3.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\u internal\u obj\u 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))

非常感谢。所以这可能构成另一个问题,但是如何将lib_对象传递给“c_lib.lib_del”?我试图将其作为c\u void\u p传递,但我遇到了一个分段错误。@jms Set
c\u lib.lib\u del.argtypes=c\u void\u p,
并调用
c\u lib.lib\u del(lib\u对象)
。如果仍然存在问题,请发布一些C函数的最小实现。