From 6c3973f7817ca6f12d31fb80fb9985820383139e Mon Sep 17 00:00:00 2001 From: sleevezipper Date: Tue, 12 Jan 2021 22:39:49 +0100 Subject: [PATCH] also check _USER for cam/mic access and support Microsoft apps too #18 --- .../Domain/Sensors/MicrophoneActiveSensor.cs | 78 +++++++++++++++++-- .../Domain/Sensors/WebcamActiveSensor.cs | 78 +++++++++++++++++-- 2 files changed, 144 insertions(+), 12 deletions(-) diff --git a/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs b/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs index 0acf53d..e85f854 100644 --- a/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs +++ b/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs @@ -36,18 +36,84 @@ namespace hass_workstation_service.Domain.Sensors [SupportedOSPlatform("windows")] private bool IsMicrophoneInUse() { - using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\microphone\NonPackaged")) + using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\microphone")) { foreach (var subKeyName in key.GetSubKeyNames()) { - using (var subKey = key.OpenSubKey(subKeyName)) + // NonPackaged has multiple subkeys + if (subKeyName == "NonPackaged") { - if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + using (var nonpackagedkey = key.OpenSubKey(subKeyName)) { - var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; - if (endTime <= 0) + foreach (var nonpackagedSubKeyName in nonpackagedkey.GetSubKeyNames()) { - return true; + using (var subKey = nonpackagedkey.OpenSubKey(nonpackagedSubKeyName)) + { + if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + { + var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; + if (endTime <= 0) + { + return true; + } + } + } + } + } + } + else + { + using (var subKey = key.OpenSubKey(subKeyName)) + { + if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + { + var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; + if (endTime <= 0) + { + return true; + } + } + } + } + } + } + + using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\microphone")) + { + foreach (var subKeyName in key.GetSubKeyNames()) + { + // NonPackaged has multiple subkeys + if (subKeyName == "NonPackaged") + { + using (var nonpackagedkey = key.OpenSubKey(subKeyName)) + { + foreach (var nonpackagedSubKeyName in nonpackagedkey.GetSubKeyNames()) + { + using (var subKey = nonpackagedkey.OpenSubKey(nonpackagedSubKeyName)) + { + if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + { + var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; + if (endTime <= 0) + { + return true; + } + } + } + } + } + } + else + { + using (var subKey = key.OpenSubKey(subKeyName)) + { + if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + { + var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; + if (endTime <= 0) + { + return true; + } } } } diff --git a/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs b/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs index 6806e78..c690c03 100644 --- a/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs +++ b/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs @@ -38,18 +38,84 @@ namespace hass_workstation_service.Domain.Sensors [SupportedOSPlatform("windows")] private bool IsWebCamInUseRegistry() { - using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam\NonPackaged")) + using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam")) { foreach (var subKeyName in key.GetSubKeyNames()) { - using (var subKey = key.OpenSubKey(subKeyName)) + // NonPackaged has multiple subkeys + if (subKeyName == "NonPackaged") { - if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + using (var nonpackagedkey = key.OpenSubKey(subKeyName)) { - var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; - if (endTime <= 0) + foreach (var nonpackagedSubKeyName in nonpackagedkey.GetSubKeyNames()) { - return true; + using (var subKey = nonpackagedkey.OpenSubKey(nonpackagedSubKeyName)) + { + if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + { + var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; + if (endTime <= 0) + { + return true; + } + } + } + } + } + } + else + { + using (var subKey = key.OpenSubKey(subKeyName)) + { + if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + { + var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; + if (endTime <= 0) + { + return true; + } + } + } + } + } + } + + using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam")) + { + foreach (var subKeyName in key.GetSubKeyNames()) + { + // NonPackaged has multiple subkeys + if (subKeyName == "NonPackaged") + { + using (var nonpackagedkey = key.OpenSubKey(subKeyName)) + { + foreach (var nonpackagedSubKeyName in nonpackagedkey.GetSubKeyNames()) + { + using (var subKey = nonpackagedkey.OpenSubKey(nonpackagedSubKeyName)) + { + if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + { + var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; + if (endTime <= 0) + { + return true; + } + } + } + } + } + } + else + { + using (var subKey = key.OpenSubKey(subKeyName)) + { + if (subKey.GetValueNames().Contains("LastUsedTimeStop")) + { + var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long)subKey.GetValue("LastUsedTimeStop") : -1; + if (endTime <= 0) + { + return true; + } } } }