示例#1
0
        /// <summary>
        ///     Obtains teh serial ports and their descriptions using SetupAPI.
        ///     Prefered over the WMI implementation because it is significantly
        ///     faster, but requires admin access.
        /// </summary>
        private static IEnumerable <SerialPortInfo> GetPortsSetupApi()
        {
            var guids = new[] {
                Guid.Parse(GUID_DEVINTERFACE_COMPORT2)
                // Guid.Parse(GUID_DEVINTERFACE_COMPORT)
            };

            foreach (var guid in guids)
            {
                var guidCopy       = guid;
                var hDeviceInfoSet = SetupApi.SetupDiGetClassDevs(ref guidCopy, null, IntPtr.Zero,
                                                                  SetupApi.DiGetFlags.Present);

                if (hDeviceInfoSet == IntPtr.Zero)
                {
                    throw new Exception("Failed to get device information set for the COM ports");
                }

                var iMemberIndex = 0;
                while (true)
                {
                    var deviceInfoData = new SetupApi.SpDevInfoData();
                    deviceInfoData.cbSize = (uint)Marshal.SizeOf(typeof(SetupApi.SpDevInfoData));
                    bool success = SetupApi.SetupDiEnumDeviceInfo(hDeviceInfoSet, (uint)iMemberIndex, deviceInfoData);
                    if (!success)
                    {
                        break;
                    }
                    var spiq = new SerialPortInfo {
                        Name = GetDeviceName(hDeviceInfoSet, deviceInfoData)
                    };
                    GetRegistryProperties(spiq, hDeviceInfoSet, deviceInfoData);
                    if (!string.IsNullOrEmpty(spiq.Name) && spiq.Name.Contains("COM"))
                    {
                        yield return(spiq);
                    }
                    iMemberIndex++;
                }

                SetupApi.SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
            }
        }
示例#2
0
        private static void GetRegistryProperties(SerialPortInfo spiq, IntPtr hDeviceInfoSet, SetupApi.SpDevInfoData deviceInfoData)
        {
            var        propertyBuffer  = new StringBuilder(256);
            const uint propRegDataType = 0;
            var        length          = (uint)propertyBuffer.Capacity;
            bool       success         = SetupApi.SetupDiGetDeviceRegistryProperty(hDeviceInfoSet, deviceInfoData, (int)SetupApi.SPDRP.SPDRP_DEVICEDESC,
                                                                                   propRegDataType, propertyBuffer, length, out length);

            //if (!success) throw new Exception("Can not read registry for device " + deviceInfoData.ClassGuid);
            if (success)
            {
                spiq.Description = propertyBuffer.ToString();
            }

            propertyBuffer = new StringBuilder(256);
            length         = (uint)propertyBuffer.Capacity;
            success        = SetupApi.SetupDiGetDeviceRegistryProperty(hDeviceInfoSet, deviceInfoData, (int)SetupApi.SPDRP.SPDRP_HARDWAREID,
                                                                       propRegDataType, propertyBuffer, length, out length);
            if (success)
            {
                spiq.HardwareID = propertyBuffer.ToString();
            }
        }
示例#3
0
 protected bool Equals(SerialPortInfo other)
 {
     return(string.Equals(Name, other.Name));
 }