海康威视相机SDK [Delphi]-错误7 NET_DVR_NETWORK_FAIL_CONNECT

时间:2019-01-03 13:39:40

标签: delphi hikvision

我无法连接到Hikvison摄像机,NET_DVR_Login_V30返回-1,而NET_DVR_GetLastError返回7: “ NET_DVR_NETWORK_FAIL_CONNECT-连接设备失败。设备脱机或网络连接超时。”

相机已100%联机且已配置,我可以使用ClientDemo.exe成功连接!

在wireshark中,我什至没有尝试连接。

type
  LPNET_DVR_DEVICEINFO_V30 = ^NET_DVR_DEVICEINFO_V30;

  NET_DVR_DEVICEINFO_V30 = Record
    sSerialNumber: array [0 .. 47] of BYTE;
    byAlarmInPortNum: BYTE;
    byAlarmOutPortNum: BYTE;
    byDiskNum: BYTE;
    byDVRType: BYTE;
    byChanNum: BYTE;
    byStartChan: BYTE;
    byAudioChanNum: BYTE;
    byIPChanNum: BYTE;
    byZeroChanNum: BYTE;
    byMainProto: BYTE;
    bySubProto: BYTE;
    bySupport: BYTE;
    bySupport1: BYTE;
    byRes1: BYTE;
    wDevType: Word;
    byRes2: array [0 .. 15] of BYTE;
  end;

  (...)

var
  Form1: TForm1;
  ErrorValues: LongWord;
  lUserID: LONG;
  struDeviceInfo: NET_DVR_DEVICEINFO_V30;
  DVRIP, DVRUserName, DVRPassWord: String;


function NET_DVR_Init(): boolean; stdcall;
  external 'HCNetSDK.dll' name 'NET_DVR_Init';

function NET_DVR_Cleanup(): boolean; stdcall;
  external 'HCNetSDK.dll' name 'NET_DVR_Cleanup';

function NET_DVR_SetConnectTime(dwWaitTime, dwTryTimes: DWORD): boolean;
  stdcall; external 'HCNetSDK.dll' name 'NET_DVR_SetConnectTime';

function NET_DVR_SetReconnect(dwInterval: DWORD; bEnableRecon: boolean)
  : boolean; stdcall; external 'HCNetSDK.dll' name 'NET_DVR_SetReconnect';

function NET_DVR_GetLastError(): LongWord; stdcall;
  external 'HCNetSDK.dll' name 'NET_DVR_GetLastError';

function NET_DVR_Login_V30(sDVRIP: PChar; wDVRPort: Word; sUserName: PChar;
  sPassword: PChar; lpDeviceInfo: LPNET_DVR_DEVICEINFO_V30): LONG; stdcall;
  external 'HCNetSDK.dll' name 'NET_DVR_Login_V30';

function NET_DVR_Logout_V30(lUserID: LONG): boolean; stdcall;
  external 'HCNetSDK.dll' name 'NET_DVR_Logout_V30';


implementation

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
  DVRIP := 'xxx.xxx.xxx.xxx';
  DVRUserName := 'demo';
  DVRPassWord := '12345';

  NET_DVR_Init();

  NET_DVR_SetConnectTime(2000, 1);
  NET_DVR_SetReconnect(10000, true);

  lUserID := NET_DVR_Login_V30(PChar(DVRIP), 8000, PChar(DVRUserName),
    PChar(DVRPassWord), @struDeviceInfo);
  Memo1.Lines.Add('lUserID:' + inttostr(lUserID));

  ErrorValues := NET_DVR_GetLastError();
  Memo1.Lines.Add('ErrorValues:' + inttostr(ErrorValues));

  NET_DVR_Logout_V30(lUserID);
  NET_DVR_Cleanup();

end;

致谢

1 个答案:

答案 0 :(得分:1)

要使其正常工作,原始代码需要进行2个修复:
1.将变量声明从String改成AnsiString
2.以PAnsiChar(如G Wimpassinger所指出的那样)将字符串传递到dll。

DVRIP, DVRUserName, DVRPassWord: AnsiString;
...

function NET_DVR_Login_V30(sDVRIP: PAnsiChar; wDVRPort: Word; sUserName: PAnsiChar;
  sPassword: PAnsiChar; lpDeviceInfo: LPNET_DVR_DEVICEINFO_V30): LONG; stdcall;
  external 'HCNetSDK.dll' name 'NET_DVR_Login_V30';
...

lUserID := NET_DVR_Login_V30(PAnsiChar(DVRIP), 8000, PAnsiChar(DVRUserName),
  PAnsiChar(DVRPassWord), @struDeviceInfo);