(资料图片)
非门禁和报警主机产品,可通过该文章来获取和设置相关用户的密码,而门禁和报警主机无法使用,码错误会报17。
调用方法:NET_DVR_GetDVRConfig(userId, 4181, 0, ptrDevUser, (uint)nSize, ref dwReturn)
//参数配置 begin[DllImport(@"..\..\runtime\hk\HCNetSDK.dll")]public static extern bool NET_DVR_GetDVRConfig(int lUserID, uint dwCommand, int lChannel, IntPtr lpOutBuffer, uint dwOutBufferSize, ref uint lpBytesReturned);
参数 | 说明 |
---|---|
lUserID | 登录时返回的句柄 |
dwCommand | 4181为:获取用户参数 |
lChannel | 从0开始,每32个用户为一组 |
lpOutBuffer | true为成功,false为失败 |
dwOutBufferSize | 接收数据的长度,lpOutBuffer的长度 |
lpBytesReturned | 实际指针的长度 |
返回值 | true为成功,false为失败 |
声明变量类:NET_DVR_USER_V51
[StructLayoutAttribute(LayoutKind.Sequential)]public struct NET_DVR_USER_V51{ public uint dwSize; //结构体大小 public uint dwMaxUserNum; //设备支持的最大用户数-只读 [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_USERNUM_V30, ArraySubType = UnmanagedType.Struct)] public NET_DVR_USER_INFO_V51[] struUser; /* 用户参数 */ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = PASSWD_LEN, ArraySubType = UnmanagedType.I1)] public byte[] sloginPassword; /* 登陆密码确认 */ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 240, ArraySubType = UnmanagedType.I1)] public byte[] byRes; //保留}
声明变量类:NET_DVR_USER_INFO_V51
修改密码或其他用户参数时,需修改该变量的值
//单用户参数(子结构)(扩展)[StructLayoutAttribute(LayoutKind.Sequential)]public struct NET_DVR_USER_INFO_V51{ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = NAME_LEN, ArraySubType = UnmanagedType.I1)] public byte[] sUserName; /* 用户名只能用16字节 */ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = PASSWD_LEN, ArraySubType = UnmanagedType.I1)] public byte[] sPassword; /* 密码 */ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_RIGHT, ArraySubType = UnmanagedType.I1)] public byte[] byLocalRight; /* 本地权限 */ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_RIGHT, ArraySubType = UnmanagedType.I1)] public byte[] byRemoteRight;/* 远程权限 */ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V40, ArraySubType = UnmanagedType.U4)] public uint[] dwNetPreviewRight; /* 远程可以预览的通道,从前往后顺序排列,遇到0xffffffff后续均为无效*/ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V40, ArraySubType = UnmanagedType.U4)] public uint[] dwLocalRecordRight; /* 本地可以录像的通道,从前往后顺序排列,遇到0xffffffff后续均为无效*/ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V40, ArraySubType = UnmanagedType.U4)] public uint[] dwNetRecordRight; /* 远程可以录像的通道,从前往后顺序排列,遇到0xffffffff后续均为无效*/ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V40, ArraySubType = UnmanagedType.U4)] public uint[] dwLocalPlaybackRight; /* 本地可以回放的通道,从前往后顺序排列,遇到0xffffffff后续均为无效*/ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V40, ArraySubType = UnmanagedType.U4)] public uint[] dwNetPlaybackRight; /* 远程可以回放的通道,从前往后顺序排列,遇到0xffffffff后续均为无效*/ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V40, ArraySubType = UnmanagedType.U4)] public uint[] dwLocalPTZRight; /* 本地可以PTZ的通道,从前往后顺序排列,遇到0xffffffff后续均为无效*/ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V40, ArraySubType = UnmanagedType.U4)] public uint[] dwNetPTZRight; /* 远程可以PTZ的通道,从前往后顺序排列,遇到0xffffffff后续均为无效*/ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V40, ArraySubType = UnmanagedType.U4)] public uint[] dwLocalBackupRight; /* 本地备份权限通道,从前往后顺序排列,遇到0xffffffff后续均为无效*/ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V40, ArraySubType = UnmanagedType.U4)] public uint[] dwLocalPreviewRight;/* 本地预览权限通道,从前往后顺序排列,遇到0xffffffff后续均为无效*/ public NET_DVR_IPADDR struUserIP; /* 用户IP地址(为0时表示允许任何地址) */ [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MACADDR_LEN, ArraySubType = UnmanagedType.I1)] public byte[] byMACAddr; /* 物理地址 */ public byte byPriority; /* 优先级,0xff-无,0--低,1--中,2--高 */ public byte byAlarmOnRight; // 报警输入口布防权限 1-有权限,0-无权限 public byte byAlarmOffRight; // 报警输入口撤防权限 1-有权限,0-无权限 public byte byBypassRight; // 报警输入口旁路权限 1-有权限,0-无权限 [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 2, ArraySubType = UnmanagedType.I1)] public byte[] byRes1; //四字节对齐 [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_RIGHT, ArraySubType = UnmanagedType.I1)] public byte[] byPublishRight; //信息发布专有权限 public uint dwPasswordValidity; //密码有效期,仅管理员用户可以修改,单位:天,填0表示永久生效 [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = PASSWD_LEN, ArraySubType = UnmanagedType.I1)] public byte[] byKeypadPassword; //键盘密码 public byte byUserOperateType; //用户操作类型,1-网络用户,2-键盘用户,3-网络用户+键盘用户 [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 1007, ArraySubType = UnmanagedType.I1)] public byte[] byRes;}
uint error = 0;//设置用户信息配置// 设置返回的字节数UInt32 dwReturn = 0;// 声明NET_DVR_USER_V51参数CHCNetSDK.NET_DVR_USER_V51 devUser = new CHCNetSDK.NET_DVR_USER_V51();// 获得NET_DVR_USER_V51参数的字节长度Int32 nSize = Marshal.SizeOf(devUser);// 声明与NET_DVR_USER_V51参数的字节长度相同的内存空间IntPtr ptrDevUser = Marshal.AllocHGlobal(nSize);// 将NET_DVR_USER_V51参数压入内存空间中Marshal.StructureToPtr(devUser, ptrDevUser, false);// 调用海康方法bool b = CHCNetSDK.NET_DVR_GetDVRConfig(userId, 4181, 0, ptrDevUser, (uint)nSize, ref dwReturn);if (b){// 将内存空间的数据转换为NET_DVR_USER_V51// 该值就是获取的参数信息 CHCNetSDK.NET_DVR_USER_V51 strUserInfo = (CHCNetSDK.NET_DVR_USER_V51)Marshal.PtrToStructure(ptrDevUser, typeof(CHCNetSDK.NET_DVR_USER_V51)); // 释放内存空间 Marshal.FreeHGlobal(ptrDevUser);}else{error = CHCNetSDK.NET_DVR_GetLastError();}
NET_DVR_GetDVRConfig
同上
NET_DVR_SetDVRConfig(userId, 4182, 0, ptrPicCfg1, (uint)nSize1)
[DllImport(@"..\..\runtime\hk\HCNetSDK.dll")]public static extern bool NET_DVR_SetDVRConfig(int lUserID, uint dwCommand, int lChannel, System.IntPtr lpInBuffer, uint dwInBufferSize);
参数 | 说明 |
---|---|
lUserID | 登录时返回的句柄 |
dwCommand | 4182为:设置用户参数 |
lChannel | 从0开始,每32个用户为一组 |
lpInBuffer | NET_DVR_USER_V51实例压入的内存地址 |
dwInBufferSize | NET_DVR_USER_V51实例的长度 |
返回值 | true是成功,false是失败 |
通过NET_DVR_GetDVRConfig方法获得配置项后,通过给struAcsAlarmInfo1.struUser[0].sPassword赋值数据来设置admin用户密码。
注意:返回的struAcsAlarmInfo1.struUser[0].sPassword是无法获得到密码的
//声明NET_DVR_USER_V51类CHCNetSDK.NET_DVR_USER_V51 devUser = new CHCNetSDK.NET_DVR_USER_V51();Int32 nSize = Marshal.SizeOf(devUser);IntPtr ptrDevUser = Marshal.AllocHGlobal(nSize);// 将NET_DVR_USER_V51实例压入内存中,得到IntPtr指针Marshal.StructureToPtr(devUser, ptrDevUser, false);// 获得用户配置参数b = CHCNetSDK.NET_DVR_GetDVRConfig(userId, 4181, 0, ptrDevUser, (uint)nSize, ref dwReturn);if (b){ // 将参数从内存中提取出来 CHCNetSDK.NET_DVR_USER_V51 struAcsAlarmInfo = (CHCNetSDK.NET_DVR_USER_V51)Marshal.PtrToStructure(ptrDevUser, typeof(CHCNetSDK.NET_DVR_USER_V51)); // 释放内存 Marshal.FreeHGlobal(ptrDevUser); // 尝试获得密码,结果为0 String password1 = Encoding.UTF8.GetString(struAcsAlarmInfo.struUser[0].sPassword); // 设置新密码 Encoding.UTF8.GetBytes(newPassword).CopyTo(struAcsAlarmInfo.struUser[0].sPassword, 0); Int32 nSize1 = Marshal.SizeOf(struAcsAlarmInfo); IntPtr ptrPicCfg1 = Marshal.AllocHGlobal(nSize1); // 将struAcsAlarmInfo压入内存 Marshal.StructureToPtr(struAcsAlarmInfo, ptrPicCfg1, false); // 调用设置方法 b = CHCNetSDK.NET_DVR_SetDVRConfig(userId, 4182, 0, ptrPicCfg1, (uint)nSize1); // 不管有没有错误,都获得错误码 error = CHCNetSDK.NET_DVR_GetLastError(); // 释放内存 Marshal.FreeHGlobal(ptrPicCfg1);}return error;
注:门禁和报警主机会返回false,错误码为23,意思是设备不支持。需要使用NET_DVR_SetAlarmDeviceUser方法进行设置
X 关闭
Copyright © 2015-2022 时代变频网版权所有 备案号: 联系邮箱: 514 676 113@qq.com