Exemplo n.º 1
0
        public static int EnumerateDevices(UInt32 DeviceIndex, string ClassName, StringBuilder DeviceName)
        {
            UInt32 RequiredSize = 0;
            Guid   guid         = Guid.Empty;

            Guid[]          guids = new Guid[1];
            IntPtr          NewDeviceInfoSet;
            SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();


            bool res = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);

            if (RequiredSize == 0)
            {
                //incorrect class name:
                DeviceName = new StringBuilder("");
                return(-2);
            }

            if (!res)
            {
                guids = new Guid[RequiredSize];
                res   = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);

                if (!res || RequiredSize == 0)
                {
                    //incorrect class name:
                    DeviceName = new StringBuilder("");
                    return(-2);
                }
            }

            //get device info set for our device class
            NewDeviceInfoSet = SetupDiGetClassDevsA(ref guids[0], 0, IntPtr.Zero, DIGCF_PRESENT);
            if (NewDeviceInfoSet.ToInt32() == -1)
            {
                //device information is unavailable:
                DeviceName = new StringBuilder("");
                return(-3);
            }

            DeviceInfoData.cbSize = 28;
            //is devices exist for class
            DeviceInfoData.DevInst   = 0;
            DeviceInfoData.ClassGuid = System.Guid.Empty;
            DeviceInfoData.Reserved  = 0;

            res = SetupDiEnumDeviceInfo(NewDeviceInfoSet,
                                        DeviceIndex, DeviceInfoData);
            if (!res)
            {
                //no such device:
                SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
                DeviceName = new StringBuilder("");
                return(-1);
            }

            DeviceName.Capacity = MAX_DEV_LEN;
            if (!SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet, DeviceInfoData,
                                                   SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero))
            {
                res = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
                                                        DeviceInfoData, SPDRP_DEVICEDESC, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero);
                if (!res)
                {
                    //incorrect device name:
                    SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
                    DeviceName = new StringBuilder("");
                    return(-4);
                }
            }
            return(0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 枚举设备类型
        /// </summary>
        /// <param name="ClassIndex"></param>
        /// <param name="ClassName">设备类型名称</param>
        /// <param name="ClassDescription">设备类型说明</param>
        /// <param name="DevicePresent"></param>
        /// <returns></returns>
        public static int EnumerateClasses(UInt32 ClassIndex, StringBuilder ClassName, StringBuilder ClassDescription, ref bool DevicePresent)
        {
            Guid            ClassGuid = Guid.Empty;
            IntPtr          NewDeviceInfoSet;
            UInt32          result;
            SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
            bool            resNam         = false;
            UInt32          RequiredSize   = 0;

            result        = CM_Enumerate_Classes(ClassIndex, ref ClassGuid, 0);
            DevicePresent = false;
            SP_CLASSIMAGELIST_DATA imagelist = new SP_CLASSIMAGELIST_DATA();

            if (result != CR_SUCCESS)
            {
                return((int)result);
            }
            resNam = SetupDiClassNameFromGuidA(ref ClassGuid, ClassName, RequiredSize, ref RequiredSize);
            if (RequiredSize > 0)
            {
                ClassName.Capacity = (int)RequiredSize;
                resNam             = SetupDiClassNameFromGuidA(ref ClassGuid, ClassName, RequiredSize, ref RequiredSize);
            }
            NewDeviceInfoSet = SetupDiGetClassDevsA(ref ClassGuid, 0, IntPtr.Zero, DIGCF_PRESENT);
            if (NewDeviceInfoSet.ToInt32() == -1)
            {
                DevicePresent = false;
                return(0);
            }

            UInt32 numD = 0;

            DeviceInfoData.cbSize    = 28;
            DeviceInfoData.DevInst   = 0;
            DeviceInfoData.ClassGuid = System.Guid.Empty;
            DeviceInfoData.Reserved  = 0;

            Boolean res1 = SetupDiEnumDeviceInfo(
                NewDeviceInfoSet,
                numD,
                DeviceInfoData);

            if (!res1)
            {
                DevicePresent = false;
                return(0);
            }
            SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
            IntPtr KeyClass = SetupDiOpenClassRegKeyExA(
                ref ClassGuid, MAXIMUM_ALLOWED, DIOCR_INSTALLER, IntPtr.Zero, 0);

            if (KeyClass.ToInt32() == -1)
            {
                DevicePresent = false;
                return(0);
            }
            UInt32 sizeB = MAX_SIZE_DEVICE_DESCRIPTION;

            ClassDescription.Capacity = MAX_SIZE_DEVICE_DESCRIPTION;
            UInt32 res = RegQueryValueA(KeyClass, 0, ClassDescription, ref sizeB);

            if (res != 0)
            {
                ClassDescription = new StringBuilder("");           //No device description
            }
            DevicePresent = true;
            ClassesGuid   = DeviceInfoData.ClassGuid;
            return(0);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 通过设备类型枚举设备信息
        /// </summary>
        /// <param name="DeviceIndex"></param>
        /// <param name="ClassName"></param>
        /// <param name="DeviceName"></param>
        /// <returns></returns>
        public static void EnumerateDevices(UInt32 DeviceIndex, string ClassName, StringBuilder DeviceName, StringBuilder DeviceID, StringBuilder Mfg, StringBuilder IsInstallDrivers)
        {
            UInt32 RequiredSize = 0;
            Guid   guid         = Guid.Empty;

            Guid[]          guids = new Guid[1];
            IntPtr          NewDeviceInfoSet;
            SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();


            bool res = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);

            if (RequiredSize == 0)
            {
                //类型不正确
                DeviceName = new StringBuilder("");
                Debug.WriteLine("类型不正确");
            }

            if (res == false)
            {
                guids = new Guid[RequiredSize];
                res   = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);

                if (!res || RequiredSize == 0)
                {
                    //类型不正确
                    DeviceName = new StringBuilder("");
                    Debug.WriteLine("类型不正确");
                }
            }
            //通过类型获取设备信息
            NewDeviceInfoSet = SetupDiGetClassDevsA(ref guids[0], 0, IntPtr.Zero, DIGCF_PRESENT);
            if (NewDeviceInfoSet.ToInt64() == -1)
            {
                //设备不可用
                DeviceName = new StringBuilder("");
                Debug.WriteLine("设备不可用");
            }
            DeviceInfoData.cbSize    = Marshal.SizeOf(DeviceInfoData);
            DeviceInfoData.devInst   = 0;
            DeviceInfoData.classGuid = System.Guid.Empty;
            DeviceInfoData.reserved  = 0;

            var devs = SetupDiGetClassDevsExW(guids[0], null, IntPtr.Zero, DIGCF_NOSET, null, null, 0);

            for (uint i = 0; SetupDiEnumDeviceInfo(devs, i, DeviceInfoData); i++)
            {
                DeviceName.Capacity       = MAX_DEV_LEN;
                DeviceID.Capacity         = MAX_DEV_LEN;
                Mfg.Capacity              = MAX_DEV_LEN;
                IsInstallDrivers.Capacity = MAX_DEV_LEN;
                if (!SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero))
                {
                    res = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_DEVICEDESC, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero);
                    if (!res)
                    {
                        //类型不正确
                        SetupDiDestroyDeviceInfoList(devs);
                        Debug.WriteLine("类型不正确");
                    }
                }
                Debug.WriteLine("=============" + DeviceName);
                //设备ID
                bool resHardwareID = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_HARDWAREID, 0, DeviceID, MAX_DEV_LEN, IntPtr.Zero);
                if (!resHardwareID)
                {
                    //设备ID未知
                    DeviceID.Append("未知");
                }
                //设备供应商
                bool resMfg = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_MFG, 0, Mfg, MAX_DEV_LEN, IntPtr.Zero);
                if (!resMfg)
                {
                    //设备供应商未知
                    Mfg.Append("未知");
                }
                //设备是否安装驱动
                bool resIsInstallDrivers = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_DRIVER, 0, IsInstallDrivers, MAX_DEV_LEN, IntPtr.Zero);
                if (!resIsInstallDrivers)
                {
                    //设备是否安装驱动
                    IsInstallDrivers = new StringBuilder("");
                }
            }
            SetupDiDestroyDeviceInfoList(devs);
            //释放当前设备占用内存
            SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 通过设备类型枚举设备信息
        /// </summary>
        /// <param name="DeviceIndex"></param>
        /// <param name="ClassName"></param>
        /// <param name="DeviceName"></param>
        /// <returns></returns>
        public static int EnumerateDevices(UInt32 DeviceIndex, string ClassName, StringBuilder DeviceName, StringBuilder DeviceID, StringBuilder Mfg, StringBuilder IsInstallDrivers)
        {
            UInt32 RequiredSize = 0;
            Guid   guid         = Guid.Empty;

            Guid[]          guids = new Guid[1];
            IntPtr          NewDeviceInfoSet;
            SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();


            bool res = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);

            if (RequiredSize == 0)
            {
                //类型不正确
                DeviceName = new StringBuilder("");
                return(-2);
            }

            if (res == false)
            {
                guids = new Guid[RequiredSize];
                res   = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);

                if (!res || RequiredSize == 0)
                {
                    //类型不正确
                    DeviceName = new StringBuilder("");
                    return(-2);
                }
            }

            //通过类型获取设备信息
            NewDeviceInfoSet = SetupDiGetClassDevsA(ref guids[0], 0, IntPtr.Zero, DIGCF_PRESENT);
            if (NewDeviceInfoSet.ToInt32() == -1)
            {
                //设备不可用
                DeviceName = new StringBuilder("");
                return(-3);
            }

            DeviceInfoData.cbSize = 28;
            //正常状态
            DeviceInfoData.DevInst   = 0;
            DeviceInfoData.ClassGuid = System.Guid.Empty;
            DeviceInfoData.Reserved  = 0;

            res = SetupDiEnumDeviceInfo(NewDeviceInfoSet,
                                        DeviceIndex, DeviceInfoData);
            if (!res)
            {
                //没有设备
                SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
                DeviceName = new StringBuilder("");
                return(-1);
            }



            DeviceName.Capacity       = MAX_DEV_LEN;
            DeviceID.Capacity         = MAX_DEV_LEN;
            Mfg.Capacity              = MAX_DEV_LEN;
            IsInstallDrivers.Capacity = MAX_DEV_LEN;
            if (!SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet, DeviceInfoData,
                                                   SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero))
            {
                res = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
                                                        DeviceInfoData, SPDRP_DEVICEDESC, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero);
                if (!res)
                {
                    //类型不正确
                    SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
                    DeviceName = new StringBuilder("");
                    return(-4);
                }
            }
            //设备ID
            bool resHardwareID = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
                                                                   DeviceInfoData, SPDRP_HARDWAREID, 0, DeviceID, MAX_DEV_LEN, IntPtr.Zero);

            if (!resHardwareID)
            {
                //设备ID未知
                DeviceID = new StringBuilder("");
                DeviceID.Append("未知");
            }
            //设备供应商
            bool resMfg = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
                                                            DeviceInfoData, SPDRP_MFG, 0, Mfg, MAX_DEV_LEN, IntPtr.Zero);

            if (!resMfg)
            {
                //设备供应商未知
                Mfg = new StringBuilder("");
                Mfg.Append("未知");
            }
            //设备是否安装驱动
            bool resIsInstallDrivers = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
                                                                         DeviceInfoData, SPDRP_DRIVER, 0, IsInstallDrivers, MAX_DEV_LEN, IntPtr.Zero);

            if (!resIsInstallDrivers)
            {
                //设备是否安装驱动
                IsInstallDrivers = new StringBuilder("");
            }
            //释放当前设备占用内存
            SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
            return(0);
        }
        public static int EnumerateClasses(UInt32 ClassIndex,
                                           ref StringBuilder ClassName, StringBuilder ClassDescription,
                                           ref bool DevicePresent)
        {
            Guid            ClassGuid = Guid.Empty;
            IntPtr          NewDeviceInfoSet;
            SP_DEVINFO_DATA DeviceInfoData;
            UInt32          result;
            StringBuilder   name         = new StringBuilder("");
            bool            resNam       = false;
            UInt32          RequiredSize = 0;

            IntPtr ptr;

            result = CM_Enumerate_Classes(ClassIndex, ref ClassGuid, 0);


            ClassName     = new StringBuilder("");
            DevicePresent = false;
            //incorrect device class:
            if (result == CR_INVALID_DATA)
            {
                return(-2);
            }
            //device class is absent
            if (result == CR_NO_SUCH_VALUE)
            {
                return(-1);
            }
            //bad param. - fatal error
            if (result != CR_SUCCESS)
            {
                return(-3);
            }


            name.Capacity = 0;
            resNam        = SetupDiClassNameFromGuidA(ref ClassGuid, name, RequiredSize,
                                                      ref RequiredSize);
            if (RequiredSize > 0)
            {
                name.Capacity = (int)RequiredSize;
                resNam        = SetupDiClassNameFromGuidA(ref ClassGuid, name,
                                                          RequiredSize, ref RequiredSize);
            }

            NewDeviceInfoSet = SetupDiGetClassDevsA(
                ref ClassGuid,
                0,
                IntPtr.Zero,
                DIGCF_PRESENT);

            if (NewDeviceInfoSet.ToInt32() == -1)
            {
                DevicePresent = false;
                ClassName     = name;
                return(0);
            }

            IntPtr KeyClass = SetupDiOpenClassRegKeyExA(
                ref ClassGuid, MAXIMUM_ALLOWED, DIOCR_INSTALLER, IntPtr.Zero, 0);

            if (KeyClass.ToInt32() == -1)
            {
                DevicePresent = false;
                ClassName     = name;
                return(0);
            }


            UInt32        sizeB = 1000;
            String        abcd  = "";
            StringBuilder CD    = new StringBuilder("");

            ClassDescription.Capacity = 1000;

            UInt32 res = RegQueryValueA(KeyClass, 0, ClassDescription, ref sizeB);


            if (res != 0)
            {
                ClassDescription = new StringBuilder("");
            }
            SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
            ClassName     = name;
            DevicePresent = true;

            return(0);
        }