python ctypes,带参数回调函数-抛出错误“segmentation fault(core dumped)”

dldeef67  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(341)

我正在制作一个python应用程序,它使用ctypes库与第三方提供的c共享库进行通信。
我得到了一个演示c
程序,演示了共享库的用法,所以我基本上是将c++代码重写为python/ctypes。
有些函数运行得很好,但现在不断出现错误
分段故障(堆芯倾倒)
在以下代码上(特别是在应该回调python函数的行上):

from ctypes import *
LIB_PATH = 'libxxyy.so'

DEFINE1 = 8
DEFINE1_A = (DEFINE1 + 1 + 3) & 0xfffffffc
DEFINE2 = 255
DEFINE2_A = (DEFINE2 + 1 + 3) & 0xfffffffc

class VAR_ENTRY(Structure):
    _fields_ = [
        ('Name', c_char * (DEFINE1_A + DEFINE2_A)),
        ('Format', c_uint16),
        ('Len', c_uint16)
    ]

ListVariablesCallBack = CFUNCTYPE(c_uint8, POINTER(VAR_ENTRY), c_void_p)    #see Update 1 below
@CFUNCTYPE(c_uint8, POINTER(VAR_ENTRY), c_void_p)
def ListVariablesCallBack_func(pvEntry, userParam):
    print("hello from callback")
    # Then read data from pvEntry, add 1 to userParam
    return 0

def MODULE_ListVariables(moduleHandle: c_void_p, cb, userParam: c_void_p) -> c_int32:
    '''
    cb – Callback function to be called for every variable.
    userParam – Parameter, that is passed to callback function.
    '''
    x1lib.MODULE_ListVariables.argtypes = (c_void_p, ListVariablesCallBack, c_void_p)  # See Update 1 below
    x1lib.MODULE_ListVariables.ABCtype = c_int32
    return x1lib.MODULE_ListVariables(moduleHandle, cb, userParam)      # ERROR OCCURS HERE

def browseVariables(targetHandle: c_void_p, moduleName: c_char_p) -> c_int32:
    modHandle = # calculated
    countModules = c_uint32(0)
    ret = MODULE_ListVariables(modHandle, ListVariablesCallBack_func, byref(countModules))
    return

def main():
    targetHandle = # calculated
    browseVariables(targetHandle, c_char_p(b"ABC"))

if __name__ == "__main__":
    x1lib = CDLL(LIB_PATH)
    main()

我翻译的cpp演示如下所示:

/*
 * <demo.h>
 */

# define CALLBACK

typedef struct
{   
    CHAR8   Name[DEFINE1_A + DEFINE2_A];
    UINT16  Format;
    UINT16  Len;
} VAR_ENTRY;

typedef BOOL8 (CALLBACK *ListVariablesCallBack) (const VAR_ENTRY * varEntry, const VOID* userParam);

X1LIB SINT32 MODULE_ListVariables(M1C_H_MODULE moduleHandle, const ListVariablesCallBack cb, const VOID * userParam); 

/*
 * <demo.cpp>
 */
static BOOL8 CALLBACK listVariablesCallback(const VAR_ENTRY *pvEntry, const VOID *userParam)
{
    UINT32 *countElements = (UINT32 *)userParam;
    (*countElements)++;
    printf("%s\n", pvEntry->Name);
    return true;
}

SINT32 browseVariables(M1C_H_TARGET targetHandle, CHAR8* moduleName)
{
    /* modHandle calculated*/
    UINT32 countModules=0;
    ret = MODULE_ListVariables(modHandle, listVariablesCallback, &countModules);
    return;
}

int main(int argc, char* argv[])
{
    browseVariables(targetHandle, (CHAR8 *)"ABC");
}

我做错什么了(见更新1)
使用的来源(到目前为止):https://docs.python.org/3.3/library/ctypes.html?highlight=ctypes#callback-函数ionshttps://stackoverflow.com/a/33485103
更新1:
所以,我设法让脚本运行,通过添加行 ListVariablesCallBack = CFUNCTYPE(c_uint8, POINTER(VAR_ENTRY), c_void_p) 就在门的上方 @CFUNCTYPE(c_uint8, POINTER(VAR_ENTRY), c_void_p) 并通过将模块\u listvariables函数中的argtypes设置为: x1lib.MODULE_ListVariables.argtypes = (c_void_p, ListVariablesCallBack, c_void_p) (我在上面的脚本中添加了更改)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题