Dahua Device Network SDK_C# Development (1): Device Login

Due to project needs, we need to connect to Dahua's network hard disk video recorder (NVR) to obtain the information and status of all channel devices under the recorder. We directly use Dahua's official SDK and Dahua's NETSDKCS library 最低只支持.NET Framework 4.0. The specific usage steps are as follows:

1.SDK download

SDK download address: https://support.dahuatech.com/tools/sdkExploit , pay attention to the difference between 32-bit and 64-bit
SDK

2.dll introduction

Copy the dll in the libs folder to your own project as needed (because these are C++ libraries, they can only be copied and cannot be introduced in vs). For example, if I only need to log in to the device to obtain device information, I only need to introduce dhnetsdk.dll. Of course you can also copy them all.
Library file
Open any demo with VS, find the NetSDKCS project, and introduce NetSDKCS.dll into your project after compilation.
NetSDKCS

3.NetSDK initialization

Note 同一个进程内,只有第一次初始化有效that it can be executed once at the program entry. After use, the cleanup interface needs to be called to release resources.

//初始化接口
NETClient.Init(null, IntPtr.Zero, null);
//清理接口
NETClient.Cleanup();

During initialization, you can set various callback functions such as disconnection callback and reconnection callback. If necessary, you can refer to the official instruction manual.

4. Log in to the device

Input the device IP, port, username and password to log in. Login will return the information of the currently logged in device. You need to log out of the device after each use.

/// <summary>
/// 登录ID
/// </summary>
private static IntPtr m_loginID = IntPtr.Zero;
/// <summary>
/// 登录设备信息
/// </summary>
private static NET_DEVICEINFO_Ex m_DeviceInfo;

/// <summary>
/// 登录/登出设备
/// </summary>
/// <param name="ip">设备IP</param>
/// <param name="portStr">设备端口,默认37777</param>
/// <param name="userName">用户名</param>
/// <param name="pswd">密码</param>
/// <returns></returns>
public static bool LoginOrLogout(string ip, string portStr, string userName, string pswd)
{
    
    
    if (m_loginID == IntPtr.Zero)
    {
    
    
        if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(portStr) || string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(pswd)) return false;
        ushort port = 0;
        try
        {
    
    
            port = Convert.ToUInt16(portStr.Trim());
        }
        catch
        {
    
    
            return false;
        }
        m_DeviceInfo = new NET_DEVICEINFO_Ex();
        m_loginID = NETClient.LoginWithHighLevelSecurity(ip.Trim(), port, userName.Trim(), pswd.Trim(), EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref m_DeviceInfo);
        if (m_loginID == IntPtr.Zero)
        {
    
    
            return false;
        }
        else
            return true;
    }
    else
    {
    
    
        bool result = NETClient.Logout(m_loginID);
        if (!result)
        {
    
    
            return false;
        }
        m_loginID = IntPtr.Zero;
        m_DeviceInfo = new NET_DEVICEINFO_Ex();
        return true;
    }
}

Guess you like

Origin blog.csdn.net/qq_29242649/article/details/123048173