예제 #1
0
        /// <summary>
        ///     Obtains the 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.AddToBuffer(GUID_DEVINTERFACE_COMPORT)
            };

            foreach (var guid in guids)
            {
                var    guidCopy       = guid;
                IntPtr 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");
                }

                Int32 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)
                    {
                        // No more devices in the device information set
                        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;
            uint       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));
 }