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

时间:2019-12-18 05:00:36

标签: python c ctypes

以下部分是他们提供的手册。

定义

  

NeptuneCamHandle ntcOpen(_char_t * pstrDevID,NeptuneCamHandle * phCamHandle,ENeptuneDevAccess eAccessFlag = NEPTUNE_DEV_ACCESS_EXCLUSIVE)

参数

  • [IN] pstrDevID:摄像机的ID字符串
  • [OUT] phCamHandle:摄像头手柄
  • [IN,可选] AccessFlag:控制模式

示例

_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。

我该如何解决?我做错了什么?谢谢!

0 个答案:

没有答案