摘要://MonitorhelperpublicclassRegeditMonitorHelper{///////检测注册表项名称的更改,并检测注册表创建和删除事件////publicstaticintReG_NOTIFY_CHANGE_NAME=0x1;//////检测属性的更改////publicstaticindReG_NOT
// monitorhelper
public class RegeditMonitorHelper
{
/// <summary>
/// 检测注册表项名称变化,以及侦测注册表的创建和删除事件
/// </summary>
public static int REG_NOTIFY_CHANGE_NAME = 0x1;
/// <summary>
/// 侦测属性的变化
/// </summary>
public static int REG_NOTIFY_CHANGE_ATTRIBUTES = 0x2;
/// <summary>
/// 侦测上一次修改时间的变化
/// </summary>
public static int REG_NOTIFY_CHANGE_LAST_SET = 0x4;
/// <summary>
/// 侦测对安全特性的改动
/// </summary>
public static int REG_NOTIFY_CHANGE_SECURITY = 0x8;
public static uint HKEY_CURRENT_USER = 0x80000001;
public static uint HKEY_LOCAL_MACHINE = 0x80000002;
public const UInt32 INFINITE = 0xFFFFFFFF;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);
[DllImport("kernel32.dll")]
public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] lpHandles, bool bWaitAll, uint dwMilliseconds);
[DllImport("kernel32.dll")]
public static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);
[DllImport("Advapi32.dll")]
public static extern int RegNotifyChangeKeyValue(IntPtr hKey, bool watchSubtree, int notifyFilter, IntPtr hEvent, bool asynchronous);
[DllImport("advapi32.dll", EntryPoint = "RegOpenKeyEx")]
public static extern int RegOpenKeyEx(uint hKey, string lpSubKey, RegOption dwOptions, RegSAM samDesired, ref IntPtr phkResult);
[DllImport("kernel32.dll")]
public static extern IntPtr SetEvent(IntPtr lpEventAttributes);
[Flags]
public enum RegOption
{
NonVolatile = 0x0,
Volatile = 0x1,
CreateLink = 0x2,
BackupRestore = 0x4,
OpenLink = 0x8
}
[Flags]
public enum RegSAM
{
QueryValue = 0x0001,
SetValue = 0x0002,
CreateSubKey = 0x0004,
EnumerateSubKeys = 0x0008,
Notify = 0x0010,
CreateLink = 0x0020,
WOW64_32Key = 0x0200,
WOW64_64Key = 0x0100,
WOW64_Res = 0x0300,
Read = 0x00020019,
Write = 0x00020006,
Execute = 0x00020019,
AllAccess = 0x000f003f
}
}
private const string szCamPath = @"SOFTWAREMicrosoftWindowsCurrentVersionCapabilityAccessManagerConsentStorewebcam";
// 注册表停止的点
IntPtr terminate1CamEvent = IntPtr.Zero;
private void CreateCameraMonitor()
{
IntPtr mhModeNwKey = IntPtr.Zero;
IntPtr modeEvent = IntPtr.Zero;
IntPtr[] hEvents = null;
try
{
int finx = RegeditMonitorHelper.RegOpenKeyEx(RegeditMonitorHelper.HKEY_LOCAL_MACHINE, szCamPath, 0,
RegeditMonitorHelper.RegSAM.Read | RegeditMonitorHelper.RegSAM.QueryValue | RegeditMonitorHelper.RegSAM.WOW64_64Key,
ref mhModeNwKey);
if (finx == 0) {
int _regFilter = RegeditMonitorHelper.REG_NOTIFY_CHANGE_NAME |
RegeditMonitorHelper.REG_NOTIFY_CHANGE_ATTRIBUTES |
RegeditMonitorHelper.REG_NOTIFY_CHANGE_LAST_SET |
RegeditMonitorHelper.REG_NOTIFY_CHANGE_SECURITY;
modeEvent = RegeditMonitorHelper.CreateEvent(IntPtr.Zero, false, false, null);
terminate1CamEvent = RegeditMonitorHelper.CreateEvent(IntPtr.Zero, false, false, null);
hEvents = new IntPtr[] { modeEvent, terminate1CamEvent };
while (true)
{
_logger.Info("Monitor thread--->webcam,CreateCameraMonitor111");
RegeditMonitorHelper.RegNotifyChangeKeyValue(mhModeNwKey, true, _regFilter, modeEvent, true);
uint trigger = RegeditMonitorHelper.WaitForMultipleObjects(2, hEvents, false, RegeditMonitorHelper.INFINITE);//无限等待modeEvent(注册表变化)和terminateEvent(关闭事件)反馈的结果,省的一直循环判断,浪费性能
if (trigger == 0)
{
_logger.Info($"UseWinCamera = {UseWinCamera}");
UseWinCamera = ResponseAgent.GetIntance().GetWinCameraSupport();// 获取当前状态
_logger.Info($"get cam page UseWinCamera = {UseWinCamera};");
}
else if (trigger == 1)
{
_logger.Info("Monitor webcam stop monitor");
break;
}
else
{
_logger.Info($"Monitor webcam Error trigger:{trigger}");
break;
}
}
}
else
{
_logger.Info($"MonitorAdjustDistance Open {szCamPath} ErrorCode:{finx}");
}
}
catch (Exception ex)
{
_logger.Info($"CreateCameraMonitor111,{ex.Message}");
}
}
// start monitor
System.Threading.Thread thread1 = new System.Threading.Thread(CreateCameraMonitor1);// 启动注册表监控线程
thread1.Start();
// stop monitor
RegeditMonitorHelper.SetEvent(terminate1CamEvent);