海康威视摄像头 SDK [Delphi] - 错误 7 NET_DVR_NETWORK_FAIL_CONNECT

Hikvision camera SDK [Delphi] - error 7 NET_DVR_NETWORK_FAIL_CONNECT

无法连接海康威视摄像头,NET_DVR_Login_V30returns-1,NET_DVR_GetLastErrorreturns7: "NET_DVR_NETWORK_FAIL_CONNECT - Connecting device failed. Device offline or network connection timed out."

相机 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;

此致

要使其正常工作,原始代码需要 2 个修复:
1. 将变量声明从 String 更正为 AnsiString
2. 将字符串作为 PAnsiChar 传递给 dll(如 G Wimpassinger 所指)

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