using System; using System.Collections.Generic; using System.Linq; using System.Management; using System.Runtime.InteropServices; using Vinno.vCloud.FIS.CrossPlatform.Common; using Vinno.vCloud.FIS.CrossPlatform.Common.Enum; using Vinno.vCloud.FIS.CrossPlatform.Common.Hardware.Interface; namespace Vinno.vCloud.FIS.CrossPlatform.Windows.Hardware { public class DriverHelper : IDriverHelper { private string _captureCard = "LT8501-DVI Card"; private readonly string _manageString = "Win32_PnPEntity"; [DllImport("setupapi.dll", SetLastError = true)] public static extern int CM_Locate_DevNodeA(ref int pdnDevInst, string pDeviceID, int ulFlags); [DllImport("setupapi.dll")] public static extern int CM_Get_Parent(out UInt32 pdnDevInst, UInt32 dnDevInst, int ulFlags); [DllImport("setupapi.dll", SetLastError = true)] public static extern int CM_Get_Device_ID_Size(out int pulLen, UInt32 dnDevInst, int flags = 0); [DllImport("setupapi.dll", CharSet = CharSet.Unicode)] public static extern int CM_Get_Device_ID(UInt32 dnDevInst, char[] buffer, int bufferLen, int flags); public void test() { } public void InitializeCaptureCard(List captureCardList) { if (captureCardList != null) { foreach (var captureCard in captureCardList) { if (IsDeviceExist(captureCard)) { _captureCard = captureCard; CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"Driver Helper Initialize, the Capture Card is {_captureCard}"); return; } } } CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"Driver Helper Can't not found the Capture Card in the Capture CardList, the default Capture Card is {_captureCard}"); } private bool IsDeviceExist(string deviceName) { try { using (var mc = new ManagementClass(_manageString)) { using (ManagementObjectCollection moc = mc.GetInstances()) { foreach (ManagementObject mo in moc) { var nameProperty = mo.Properties.OfType().FirstOrDefault(x => x.Name == "Name"); if (nameProperty.Value?.ToString() == deviceName) { return true; } } } } return false; } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"IsDeviceExist Error:{ex}"); return false; } } public List GetHardwareIdByDeviceName(string deivceName, EnumHardwareType hardwareType, string id) { try { PropertyData deviceIdProperty; using (var mc = new ManagementClass(_manageString)) { using (ManagementObjectCollection moc = mc.GetInstances()) { var hardwareList = moc.OfType().Where(x => x.Properties.OfType().FirstOrDefault(y => y.Name == "Name")?.Value?.ToString() == deivceName).ToList(); if (!hardwareList.Any()) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"GetHardwareIdByDeviceName Error,HardwareList Count is 0,DeviceName:{deivceName}"); return new List(); } else if (hardwareList.Count() == 1 || (string.IsNullOrWhiteSpace(id) && hardwareType == EnumHardwareType.Camera)) { PropertyData deviceIdPropertyData; switch (hardwareType) { case EnumHardwareType.Camera: deviceIdProperty = hardwareList.First().Properties.OfType().FirstOrDefault(x => x.Name == "HardwareID"); if (deviceIdProperty?.Value is string[] hardwareIds) { return hardwareIds.ToList(); } break; case EnumHardwareType.Mic: var deviceId = hardwareList.First().Properties.OfType().FirstOrDefault(x => x.Name == "DeviceID")?.Value?.ToString(); var parentDeviceId = GetParentDriverId(deviceId); if (!string.IsNullOrWhiteSpace(parentDeviceId)) { var identifierArray2 = parentDeviceId.Split('\\'); if (identifierArray2.Count() >= 3 && identifierArray2[2].Length > 10) { var identifier = identifierArray2[2].Substring(0, 10).ToLower(); var parentHardware = moc.OfType().FirstOrDefault(x => x.Properties.OfType().FirstOrDefault(y => y.Name == "DeviceID").Value != null && x.Properties.OfType().FirstOrDefault(y => y.Name == "DeviceID").Value.ToString().ToLower().Contains(identifier) && deivceName.Contains(x.Properties.OfType().FirstOrDefault(z => z.Name == "Name")?.Value?.ToString())); if (parentHardware != null) { deviceIdPropertyData = parentHardware.Properties.OfType().FirstOrDefault(x => x.Name == "HardwareID"); if (deviceIdPropertyData?.Value is string[] hardwareIds2) { return hardwareIds2.ToList(); } } } } CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"GetHardwareIdByDeviceName Error: ParentDeviceId is Null, Type : Mic"); break; case EnumHardwareType.Speaker: deviceId = hardwareList.First().Properties.OfType().FirstOrDefault(x => x.Name == "DeviceID")?.Value?.ToString(); parentDeviceId = GetParentDriverId(deviceId); if (!string.IsNullOrWhiteSpace(parentDeviceId)) { var identifierArray2 = parentDeviceId.Split('\\'); if (identifierArray2.Count() >= 3 && identifierArray2[2].Length > 10) { var identifier = identifierArray2[2].Substring(0, 10).ToLower(); var parentHardware = moc.OfType().FirstOrDefault(x => x.Properties.OfType().FirstOrDefault(y => y.Name == "DeviceID").Value != null && x.Properties.OfType().FirstOrDefault(y => y.Name == "DeviceID").Value.ToString().ToLower().Contains(identifier) && deivceName.Contains(x.Properties.OfType().FirstOrDefault(z => z.Name == "Name")?.Value?.ToString())); if (parentHardware != null) { deviceIdPropertyData = parentHardware.Properties.OfType().FirstOrDefault(x => x.Name == "HardwareID"); if (deviceIdPropertyData?.Value is string[] hardwareIds3) { return hardwareIds3.ToList(); } } } } CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"GetHardwareIdByDeviceName Error: ParentDeviceId is Null, Type : Speaker"); break; } deviceIdProperty = hardwareList.First().Properties.OfType().FirstOrDefault(x => x.Name == "HardwareID"); if (deviceIdProperty?.Value is string[] hardwareIds4) { return hardwareIds4.ToList(); } } else if (hardwareList.Count() > 1) { PropertyData deviceIdPropertyData; switch (hardwareType) { case EnumHardwareType.Camera: var identifierArray = id.Split('#'); if (identifierArray.Count() < 4) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"GetHardwareIdByDeviceName Error,The Type: Camera,The id:{id},don't have enough # seperator, it will return the default value"); } else if (identifierArray[3].Length < 10) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"GetHardwareIdByDeviceName Error,The Type: Camera,The id:{id},the 3rd part length<10, it will return the default value"); } else { var identifier = identifierArray[2].Substring(0, 10).ToLower(); foreach (var hardware in hardwareList) { var deviceId = hardware.Properties.OfType().FirstOrDefault(x => x.Name == "DeviceID")?.Value?.ToString(); if (deviceId.ToLower().Contains(identifier)) { deviceIdPropertyData = hardware.Properties.OfType().FirstOrDefault(x => x.Name == "HardwareID"); if (deviceIdPropertyData?.Value is string[] hardwareIds) { return hardwareIds.ToList(); } } } CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"GetHardwareIdByDeviceName Error,The Type: Camera,The id:{id},Device Id Pair Fail,Identifier:{identifier}, it will return the default value"); } break; case EnumHardwareType.Mic: foreach (var hardware in hardwareList) { var deviceId = hardware.Properties.OfType().FirstOrDefault(x => x.Name == "DeviceID")?.Value?.ToString(); var parentDeviceId = GetParentDriverId(deviceId); if (!string.IsNullOrWhiteSpace(parentDeviceId)) { var identifierArray2 = parentDeviceId.Split('\\'); if (identifierArray2.Count() >= 3 && identifierArray2[2].Length > 10) { var identifier = identifierArray2[2].Substring(0, 10).ToLower(); var parentHardware = moc.OfType().FirstOrDefault(x => x.Properties.OfType().FirstOrDefault(y => y.Name == "DeviceID").Value != null && x.Properties.OfType().FirstOrDefault(y => y.Name == "DeviceID").Value.ToString().ToLower().Contains(identifier) && deivceName.Contains(x.Properties.OfType().FirstOrDefault(z => z.Name == "Name")?.Value?.ToString())); if (parentHardware != null) { deviceIdPropertyData = parentHardware.Properties.OfType().FirstOrDefault(x => x.Name == "HardwareID"); if (deviceIdPropertyData?.Value is string[] hardwareIds3) { return hardwareIds3.ToList(); } } } } } CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"GetHardwareIdByDeviceName Error,The Type: Mic,The id:{id},Get Parent Device Id and Pair Fail, it will return the default value"); break; case EnumHardwareType.Speaker: foreach (var hardware in hardwareList) { var deviceId = hardware.Properties.OfType().FirstOrDefault(x => x.Name == "DeviceID")?.Value?.ToString(); var parentDeviceId = GetParentDriverId(deviceId); if (!string.IsNullOrWhiteSpace(parentDeviceId)) { var identifierArray2 = parentDeviceId.Split('\\'); if (identifierArray2.Count() >= 3 && identifierArray2[2].Length > 10) { var identifier = identifierArray2[2].Substring(0, 10).ToLower(); var parentHardware = moc.OfType().FirstOrDefault(x => x.Properties.OfType().FirstOrDefault(y => y.Name == "DeviceID").Value != null && x.Properties.OfType().FirstOrDefault(y => y.Name == "DeviceID").Value.ToString().ToLower().Contains(identifier) && deivceName.Contains(x.Properties.OfType().FirstOrDefault(z => z.Name == "Name")?.Value?.ToString())); if (parentHardware != null) { deviceIdPropertyData = parentHardware.Properties.OfType().FirstOrDefault(x => x.Name == "HardwareID"); if (deviceIdPropertyData?.Value is string[] hardwareIds3) { return hardwareIds3.ToList(); } } } } } CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"GetHardwareIdByDeviceName Error,The Type: Speaker,The id:{id},Get Parent Device Id and Pair Fail, it will return the default value"); break; } deviceIdPropertyData = hardwareList.First().Properties.OfType().FirstOrDefault(x => x.Name == "HardwareID"); if (deviceIdPropertyData?.Value is string[] hardwareIds2) { return hardwareIds2.ToList(); } } } } return new List(); } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"GetVId Error:{ex}"); return new List(); } } public bool EnableCaptureCard() { using (var mc = new ManagementClass(_manageString)) { using (ManagementObjectCollection moc = mc.GetInstances()) { foreach (ManagementObject mo in moc) { var nameProperty = mo.Properties.OfType().FirstOrDefault(x => x.Name == "Name"); if (nameProperty.Value?.ToString() == _captureCard) { try { mo.InvokeMethod("Enable", null, new InvokeMethodOptions(null, TimeSpan.MaxValue)); return true; } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Driver Enable Fail,ex:{ex}"); return false; } } } } CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Enable Failed: Can't Find Device"); return false; } } public bool DisableCaptureCard() { using (var mc = new ManagementClass(_manageString)) { using (ManagementObjectCollection moc = mc.GetInstances()) { foreach (ManagementObject mo in moc) { var nameProperty = mo.Properties.OfType().FirstOrDefault(x => x.Name == "Name"); if (nameProperty.Value?.ToString() == _captureCard) { try { mo.InvokeMethod("Disable", null, new InvokeMethodOptions(null, TimeSpan.MaxValue)); return true; } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Driver Disable Fail,ex:{ex}"); return false; } } } } CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Disable Failed: Can't Find Device"); return false; } } public void RefreshAllDevice() { try { using (var mc = new ManagementClass(_manageString)) { using (ManagementObjectCollection moc = mc.GetInstances()) { foreach (ManagementObject mo in moc) { var name = mo.Properties.OfType().FirstOrDefault(x => x.Name == "Name")?.Value?.ToString(); var hardwareIdProperty = mo.Properties.OfType().FirstOrDefault(x => x.Name == "HardwareID"); var deviceId = mo.Properties.OfType().FirstOrDefault(x => x.Name == "DeviceID")?.Value?.ToString(); var hardwareIdString = string.Empty; if (hardwareIdProperty?.Value is string[] hardwareIds) { foreach (var hardwareId in hardwareIds) { hardwareIdString += hardwareId + " "; } } var parentDeviceId = GetParentDriverId(deviceId); CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"Name:{name},HardwareId:{hardwareIdString},DeviceID:{deviceId},ParentDeviceId:{parentDeviceId}"); } } } } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RefreshAllDevice Error:{ex}"); } } /// /// 根据DeviceID获取父级DeviceID /// /// DeviceID /// resultDeviceID /// private string GetParentDriverId(string DeviceID) { try { int CM_LOCATE_DEVNODE_NORMAL = 0x00000000; int CR_SUCCESS = 0x00000000; UInt32 parentInst; int curInst = 0; int pLen = 0; int apiResult = CM_Locate_DevNodeA(ref curInst, DeviceID, CM_LOCATE_DEVNODE_NORMAL); if (apiResult != CR_SUCCESS) { return ""; } apiResult = CM_Get_Parent(out parentInst, (UInt32)curInst, CM_LOCATE_DEVNODE_NORMAL); if (apiResult != CR_SUCCESS) { return ""; } apiResult = CM_Get_Device_ID_Size(out pLen, parentInst, CM_LOCATE_DEVNODE_NORMAL); if (apiResult != CR_SUCCESS) { return ""; } char[] ptrInstanceBuf = new char[pLen]; //获取设备id字符串地址 apiResult = CM_Get_Device_ID(parentInst, ptrInstanceBuf, pLen, 0); if (apiResult != CR_SUCCESS) { return ""; } return new string(ptrInstanceBuf); } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"DriverHelper GetParentDriverId Error:{ex}"); return ""; } } } }