internal void UpdateNotInitializedFirstEgsDeviceOnSomeDeviceConnected() { // TODO: MUSTDO: Test with connecting and re-connecting USB connector, because various problems can occur here. try { // At first it checks HID connection. SetupDi.Update(); foreach (var device in DeviceList) { device.UpdateHidDeviceConnectionStatus(); } foreach (var device in DeviceList) { device.TrySetIsCameraDeviceConnectedToTrue(); } } catch (EgsHostApplicationIsClosingException) { // NOTE: The exception occurs from inside of Timer, so the application thread cannot catch it. EgsDevicesManager.Dispose() is called and then the application exit in the event handler of EgsDevicesManager.Disposing. Debug.WriteLine(Resources.CommonStrings_ApplicationWillExit); this.Dispose(); } catch (Exception ex) { if (ApplicationCommonSettings.IsDebugging) { Debugger.Break(); } Debug.WriteLine(ex.Message); } }
private static DeviceInfoSet GetDevicesInternal(Guid deviceInterfaceId, string enumerator, bool presentDevices, bool currentProfile, GetClassDevsFlags flags = GetClassDevsFlags.None) { if (deviceInterfaceId == DeviceInterfaceClasses.All) { flags |= GetClassDevsFlags.AllClasses; } if (presentDevices) { flags |= GetClassDevsFlags.Present; } if (currentProfile) { flags |= GetClassDevsFlags.Profile; } var deviceInfoSet = SetupDi.GetClassDevs(deviceInterfaceId, enumerator, flags); if (deviceInfoSet != null) { return(deviceInfoSet); } throw new DeviceManagerWindowsException("Unable to enumerate devices."); }
protected override bool LoadValue(DeviceInfo deviceInfo, out DeviceRegistryPropertyType propertyType, out Api.Buffer buffer) { if (!SetupDi.GetDeviceRegistryProperty(deviceInfo, Key, out propertyType, out buffer)) { // Only "not found" errors are valid failures. var lastError = ErrorHelpers.GetLastError(); if (lastError == ErrorCode.NotFound) { return(false); } // Everything else is an unexpected failure. throw new DeviceManagerWindowsException("Unable to query device registry property."); } return(true); }
public IEnumerable <DeviceInfo> GetDevices() { for (var index = 0; ; ++index) { DeviceInfo deviceInfo; if (!SetupDi.EnumDeviceInfo(this, index, out deviceInfo)) { if (ErrorHelpers.GetLastError() != ErrorCode.NoMoreItems) { throw new DeviceManagerWindowsException("Unable to enumerate devices."); } yield break; } yield return(deviceInfo); } }
private bool _disposed = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // TODO: dispose managed state (managed objects). } if (_handle != IntPtr.Zero) { SetupDi.DestroyDeviceInfoList(_handle); _handle = IntPtr.Zero; } _disposed = true; } }
private static string GetDeviceRegistryProperty(IntPtr hDeviceInfo, SP_DEVINFO_DATA deviceInfoData, DeviceRegistryProperty propertyType) { RegistryType propertyRegistryType = RegistryType.REG_NONE; StringBuilder propertyBuffer = new StringBuilder(512); UInt32 sizeRequired = 0; bool result = SetupDi.SetupDiGetDeviceRegistryProperty( hDeviceInfo, deviceInfoData, propertyType, ref propertyRegistryType, propertyBuffer, (UInt32)propertyBuffer.Capacity, ref sizeRequired); if ((result == true) & (propertyRegistryType == RegistryType.REG_SZ)) { return(propertyBuffer.ToString()); } else { return(null); } }
/// <summary> /// Return the list of all available COM port into the system /// </summary> /// <returns></returns> public static ReadOnlyCollection <SerialPortInfo> GetAvailablePorts() { IntPtr hdevInfo = SetupDi.SetupDiGetClassDevs( ref GUID_DEVINTERFACE_COMPORT, null, IntPtr.Zero, SetupDIGetClassDevsFlags.DIGCF_PRESENT | SetupDIGetClassDevsFlags.DIGCF_DEVICEINTERFACE); if (hdevInfo == INVALID_HANDLE_VALUE) { throw new Exception("can't access to serial port communication information."); } List <SerialPortInfo> ports = new List <SerialPortInfo>(); uint index = 0; SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA(); while (SetupDi.SetupDiEnumDeviceInfo(hdevInfo, index++, deviceInfoData) == true) { IntPtr hKey = SetupDi.SetupDiOpenDevRegKey( hdevInfo, deviceInfoData, SetupDiOpenDevRegKeyScopeFlags.DICS_FLAG_GLOBAL, 0, SetupDiOpenDevRegKeyKeyTypeFlags.DIREG_DEV, REGSAM.KEY_QUERY_VALUE); if (hKey != IntPtr.Zero) { SerialPortInfo portInfo = new SerialPortInfo(); UInt32 resultLength = 1024; IntPtr registryName = Marshal.AllocHGlobal(1024); var retValue = Registry.NtQueryKey(hKey, 3, registryName, 1024, ref resultLength); if (retValue == 0) { string registryNameStr = Marshal.PtrToStringAuto((IntPtr)(registryName.ToInt32() + 4), Marshal.ReadInt32(registryName) / 2); Marshal.FreeHGlobal(registryName); var registryPath = registryNameStr.Split('\\'); if (registryPath != null && registryPath.Length > 3) { var sNumber = registryPath[registryPath.Length - 2]; if (sNumber != null && sNumber.Contains('_')) { portInfo.SerialName = sNumber.Remove(sNumber.IndexOf('_')); } else { portInfo.SerialName = sNumber; } var deviceVidPid = registryPath[registryPath.Length - 3].ToUpperInvariant(); if (deviceVidPid != null && deviceVidPid.Length > 0 && deviceVidPid.Contains("pid_") && deviceVidPid.Contains("vid_")) { try { char separator = '&'; if (deviceVidPid.Contains('&')) { separator = '&'; } else if (deviceVidPid.Contains('+')) { separator = '+'; } var position = deviceVidPid.IndexOf("vid_") + 4; var vid = deviceVidPid.Substring(position, deviceVidPid.IndexOf(separator) - position); position = deviceVidPid.IndexOf("pid_") + 4; var pid = deviceVidPid.Substring(position, deviceVidPid.Length - position); if (pid.Contains(separator)) { pid = pid.Remove(pid.IndexOf(separator)); } portInfo.DevicePid = int.Parse(pid, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture); portInfo.DeviceVid = int.Parse(vid, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture); } catch { } } } } RegistryType type = RegistryType.REG_NONE; UInt32 capacity; StringBuilder data = new StringBuilder(256); capacity = (UInt32)data.Capacity; int result = Registry.RegQueryValueEx(hKey, "PortName", 0, ref type, data, ref capacity); if ((result == Registry.ERROR_SUCCESS) & (type == RegistryType.REG_SZ)) { portInfo.Name = data.ToString(); string property = GetDeviceRegistryProperty(hdevInfo, deviceInfoData, DeviceRegistryProperty.SPDRP_DEVICEDESC); if (property != null) { portInfo.Description = property; } property = GetDeviceRegistryProperty(hdevInfo, deviceInfoData, DeviceRegistryProperty.SPDRP_MFG); if (property != null) { portInfo.Manufacturer = property; } property = GetDeviceRegistryProperty(hdevInfo, deviceInfoData, DeviceRegistryProperty.SPDRP_FRIENDLYNAME); if (property != null) { portInfo.FriendlyName = property; } property = GetDeviceRegistryProperty(hdevInfo, deviceInfoData, DeviceRegistryProperty.SPDRP_LOCATION_INFORMATION); if (property != null) { portInfo.LocalInformation = property; } property = GetDeviceRegistryProperty(hdevInfo, deviceInfoData, DeviceRegistryProperty.SPDRP_SERVICE); if (property != null) { portInfo.Service = property; } } ports.Add(portInfo); Registry.RegCloseKey(hKey); } } SetupDi.SetupDiDestroyDeviceInfoList(hdevInfo); //PatchInZModemSerials(ports); return(new ReadOnlyCollection <SerialPortInfo>(ports)); }