开发者社区> 问答> 正文

使用python ctypes调用dll函数时参数无效

以下是他们提供的手册。

_uint32_t nCameras = ntcGetCameraCount();
if ( nCameras > 0 )
{
 pCamInfo = new NEPTUNE_CAM_INFO[nCameras];
 ntcGetCameraInfo(pCamInfo, nCameras);
}
NeptuneCamHandle hCamHandle;
// if unicast
ntcOpen(pCamInfo[0].strCamID, &hCamHandle);
// if multicast
ntcOpen(pCamInfo[0].strCamID, &hCamHandle, NEPTUNE_DEV_ACCESS_CONTROL);

这是我如何使用它在我的代码:

class _NEPTUNE_CAM_INFO_(Structure):
    pass


_NEPTUNE_CAM_INFO_._fields_ = [
    ('strVendor', c_char * 512),
    ('strModel', c_char * 512),
    ('strSerial', c_char * 512),
    ('strUserID', c_char * 512),
    ('strIP', c_char * 512),
    ('strMAC', c_char * 32),
    ('strSubnet', c_char * 512),
    ('strGateway', c_char * 512),
    ('strCamID', c_char * 512),
]
NEPTUNE_CAM_INFO = _NEPTUNE_CAM_INFO_


class IMICamera:

    def __init__(self):
        self._handle = c_void_p()  # 记录当前连接设备的句柄
        self.handle = pointer(self._handle)  # 创建句柄指针
        self.IMI_ntcInit()

    def IMI_ntcInit(self):
        IMICamCtrldll.ntcGetCameraCount.restype = c_int
        return IMICamCtrldll.ntcInit()
        # return MvCamCtrldll.ntcInit()

    def IMI_ntcGetCameraCount(self, count):
        IMICamCtrldll.ntcGetCameraCount.argtype = (c_void_p,)
        IMICamCtrldll.ntcGetCameraCount.restype = c_int
        return IMICamCtrldll.ntcGetCameraCount(byref(count))

    def IMI_ntcGetCameraInfo(self, pInfo, count):
        IMICamCtrldll.ntcGetCameraInfo.argtype = (c_void_p, c_void_p)
        IMICamCtrldll.ntcGetCameraInfo.restype = c_int
        return IMICamCtrldll.ntcGetCameraInfo(byref(pInfo), count)

    def IMI_ntcOpen(self, pstrDevID):
        IMICamCtrldll.ntcOpen.argtype = (c_char, c_void_p)
        IMICamCtrldll.ntcOpen.restype = c_int
        return IMICamCtrldll.ntcOpen(pstrDevID, byref(self.handle))
if __name__ == '__main__':
    mv = IMICamera()
    count = c_uint()
    res = mv.IMI_ntcGetCameraCount(count)
    if res != 0:
        print('IMI_ntcGetCameraCount', res)
    pInfo = (NEPTUNE_CAM_INFO * count.value)()
    res = mv.IMI_ntcGetCameraInfo(pInfo, count)
    if res != 0:
        print('IMI_ntcGetCameraInfo', res)
    strCamID = pInfo[1].strCamID
    pid = (c_char * 512)()
    for i in range(len(strCamID)):
        pid[i] = strCamID[i]

    pid2 = ctypes.create_string_buffer(512)
    pid2.value = strCamID

    pid3 = c_char_p(b'00:09:7e:02:80:88')
    res = mv.IMI_ntcOpen(pid3)

我尝试了许多类型参数,您可以在我的代码中看到。 在我运行我的代码后,结果总是得到error_code -203,这意味着NEPTUNE_ERR_InvalidParameter。 我该怎么解决这个问题?我做错了什么?谢谢! 问题来源StackOverflow 地址:/questions/59385744/invalid-parameter-when-using-python-ctypes-to-call-dll-function

展开
收起
kun坤 2019-12-25 22:13:26 12103 0
1 条回答
写回答
取消 提交回答
  • import ctypes ll=ctypes.CDLL("pythontest3.dll") path= ctypes.c_char_p("C:\Users\Public\Pictures\Sample Pictures\a.jpg") path2= ctypes.c_char_p("C:\Users\Public\Pictures\Sample Pictures\b.jpg") ll.MSE.restype = ctypes.c_double#指定MSE的返回类型为double ll.PSNR.restype=ctypes.c_double#指定PSNR的返回值为double

    指定返回值类型很重要,否则得到的结果会有错误

    ll.abc.argtypes =[ctypes.c_char_p,ctypes.c_char_p]#指定参数类型 print ll.MSE(path.value,path.value)

    2021-02-26 14:39:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载