delphihikvision

Hikvision camera SDK [Delphi] - error 7 NET_DVR_NETWORK_FAIL_CONNECT


I cannot connect to hikvison camera, NET_DVR_Login_V30 returns -1, and NET_DVR_GetLastError returns 7: "NET_DVR_NETWORK_FAIL_CONNECT - Connecting device failed. Device offline or network connection timed out."

Camera is 100% online and configured, I can successfully connect using ClientDemo.exe!

In wireshark I see that it don't even try to connect.

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;

Regards


Solution

  • To make it work the original code need 2 fixes:
    1. Correct variable declarations from String to AnsiString
    2. Pass strings to dll as PAnsiChar (as G Wimpassinger pointed)

    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);