【问题标题】:ctype Linux so library Pointer TypeErrorctype Linux so 库指针类型错误
【发布时间】:2019-07-29 22:31:01
【问题描述】:

我正在使用 ctypes 加载 C 库(@​​987654321@ 文件)。然后我设置参数并调用一个函数并得到以下错误;

ctypes.ArgumentError: argument 4: <class 'TypeError'>: expected LP_c_ushort instance instead of _ctypes.PyCSimpleType

我猜它与第 3 行末尾的 POINTER 定义有关。你能帮忙吗?

import ctypes
mylib = ctypes.cdll.LoadLibrary('./libfocas32.so')
mylib.cnc_allclibhndl3.argtypes = ctypes.c_wchar_p, ctypes.c_ushort, ctypes.c_long, ctypes.POINTER(ctypes.c_ushort)
mylib.cnc_allclibhndl3.restype = ctypes.c_long
h = ctypes.c_ushort
ret = mylib.cnc_allclibhndl3('192.168.1.1',9000,1,(h))

【问题讨论】:

    标签: python arguments function-pointers ctypes


    【解决方案1】:

    (1) h 必须接收c_ushort 类型的实例,因此:

    h = ctypes.c_ushort()
    

    (2)假设指向h的指针只在函数调用期间使用,不应该存储更长时间,使用

    ret = mylib.cnc_allclibhndl3('192.168.1.1',9000,1,ctypes.byref(h))
    

    【讨论】:

    • 我刚刚尝试了你的建议,现在得到了这个错误:TypeError: byref() argument must be a ctypes instance, not '_ctypes.PyCSimpleType'
    • @Semih 我在您的代码中发现了另一个问题。相应地更改了答案。
    • 现在代码一直运行到 ret=mylib.cnc.... 行,然后程序突然终止,没有任何消息。我已将这部分放入尝试 - 异常错误块不返回任何内容,并且代码在尝试下在此行终止。哎呀:(
    • @Semih 根据this,第一个参数是一个普通的char指针,而不是wchar指针和返回类型a(有符号)short
    • @Semih ... 声明为WINAPI,因此不要使用ctypes.cdll,而是使用ctypes.windll
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2020-12-07
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 2017-08-29
    相关资源
    最近更新 更多