示例#1
0
        //一个调用方法GetHardWareIdFromPortName

        /// <summary>
        /// 通过串口号获得对应设备的硬件ID(里面包括该串口设备的vid和pid)
        /// </summary>
        /// <param name="portName">串口号</param>
        /// <returns></returns>
        public string GetHardWareIdFromPortName(string portName)
        {
            //先判断当前机器是否有该串口号
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            int      k;
            //if (!DataOperator.IsContainElement(ports, portName,out k)) return null;
            Guid   myGUID     = Guid.Empty;
            string enumerator = "USB";

            try
            {
                IntPtr hDevInfo = HardWareOperation.SetupDiGetClassDevs(ref myGUID, enumerator, IntPtr.Zero, HardWareOperation.DIGCF_ALLCLASSES | HardWareOperation.DIGCF_PRESENT);
                if (hDevInfo.ToInt32() == HardWareOperation.INVALID_HANDLE_VALUE)
                {
                    throw new Exception("没有该类设备");
                }
                HardWareOperation.SP_DEVINFO_DATA deviceInfoData; //想避免在api中使用ref,就把structure映射成类
                deviceInfoData           = new HardWareOperation.SP_DEVINFO_DATA();
                deviceInfoData.cbSize    = 28;                    //如果要使用SP_DEVINFO_DATA,一定要给该项赋值28=16+4+4+4
                deviceInfoData.devInst   = 0;
                deviceInfoData.classGuid = System.Guid.Empty;
                deviceInfoData.reserved  = 0;
                UInt32        i;
                StringBuilder property = new StringBuilder(HardWareOperation.MAX_DEV_LEN);
                for (i = 0; HardWareOperation.SetupDiEnumDeviceInfo(hDevInfo, i, deviceInfoData); i++)
                {
                    //       Console.Write(deviceInfoData.classGuid.ToString());
                    //       HardWareOperation.SetupDiGetDeviceInstanceId(hDevInfo, deviceInfoData, porperty, (uint)porperty.Capacity, 0);
                    HardWareOperation.SetupDiGetDeviceRegistryProperty(hDevInfo, deviceInfoData,
                                                                       (uint)HardWareOperation.SPDRP_.SPDRP_CLASS,
                                                                       0, property, (uint)property.Capacity, IntPtr.Zero);
                    if (property.ToString().ToLower() != "ports")
                    {
                        continue;                                          //首先看看是不是串口设备(有些USB设备不是串口设备)
                    }
                    HardWareOperation.SetupDiGetDeviceRegistryProperty(hDevInfo, deviceInfoData,
                                                                       (uint)HardWareOperation.SPDRP_.SPDRP_FRIENDLYNAME,
                                                                       0, property, (uint)property.Capacity, IntPtr.Zero);
                    if (!property.ToString().ToLower().Contains(portName.ToLower()))
                    {
                        continue;                                                             //找到对应于portName的设备
                    }
                    HardWareOperation.SetupDiGetDeviceRegistryProperty(hDevInfo, deviceInfoData,
                                                                       (uint)HardWareOperation.SPDRP_.SPDRP_HARDWAREID,
                                                                       0, property, (uint)property.Capacity, IntPtr.Zero);
                    break;
                }
                HardWareOperation.SetupDiDestroyDeviceInfoList(hDevInfo);
                return(property.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(null);
            }
        }
示例#2
0
 public static extern bool SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet,
                                                      HardWareOperation.SP_DEVINFO_DATA DeviceInfoData,
                                                      StringBuilder DeviceInstanceId,
                                                      UInt32 DeviceInstanceIdSize,
                                                      UInt32 RequiredSize);