Exemplo n.º 1
0
        public static List <DeviceEntity> GetHiddenDevice()
        {
            UInt32 RequiredSize = 0;
            Guid   guid         = Guid.Empty;

            Guid[]          guids          = new Guid[1];
            SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
            bool            res            = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize);

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

                if (!res || RequiredSize == 0)
                {
                    Debug.WriteLine("类型不正确");
                }
            }
            if (res == true)
            {
                List <DeviceEntity> list        = new List <Hardware.DeviceEntity>();
                List <DeviceEntity> listCurrent = new List <Hardware.DeviceEntity>();

                Guid   myGUID   = System.Guid.Empty;
                IntPtr hDevInfo = SetupDiGetClassDevs(guids[0], 0, IntPtr.Zero, DIGCF_NOSET);
                if (hDevInfo.ToInt64() == -1)
                {
                    Debug.WriteLine("SetupDiGetClassDevs: 获取设备错误。");
                }
                else
                {
                    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);
                    var devsNow = SetupDiGetClassDevsExW(guids[0], null, IntPtr.Zero, DIGCF_PRESENT, null, null, 0);
                    if (devsNow.ToInt64() != -1)
                    {
                        for (uint i = 0; SetupDiEnumDeviceInfo(devsNow, i, DeviceInfoData); i++)
                        {
                            DeviceEntity  entity     = new DeviceEntity();
                            StringBuilder DeviceName = new StringBuilder("", 254);
                            bool          resName    = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero);
                            if (!resName)
                            {
                            }
                            else
                            {
                                entity.DeviceName = DeviceName.ToString();
                            }

                            StringBuilder DeviceID = new StringBuilder("", 254);
                            if (CM_Get_Device_IDA(DeviceInfoData.devInst, DeviceID, 254, 0) == CR_SUCCESS)
                            {
                                entity.DeviceID = DeviceID.ToString();
                            }
                            listCurrent.Add(entity);
                        }
                    }

                    for (uint i = 0; SetupDiEnumDeviceInfo(devs, i, DeviceInfoData); i++)
                    {
                        DeviceEntity entity = new DeviceEntity();

                        StringBuilder DeviceID = new StringBuilder("", 254);
                        if (CM_Get_Device_IDA(DeviceInfoData.devInst, DeviceID, 254, 0) == CR_SUCCESS)
                        {
                            entity.DeviceID = DeviceID.ToString();
                        }

                        if (listCurrent.Exists(p => p.DeviceID == DeviceID.ToString()))
                        {
                            continue;
                        }
                        else
                        {
                            StringBuilder DeviceName = new StringBuilder("", 254);
                            bool          resName    = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero);
                            if (!resName)
                            {
                            }
                            else
                            {
                                entity.DeviceName = DeviceName.ToString();
                            }

                            StringBuilder Mfg            = new StringBuilder("", 254);
                            bool          resHardwareMfg = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_MFG, 0, Mfg, MAX_DEV_LEN, IntPtr.Zero);
                            if (!resHardwareMfg)
                            {
                            }
                            else
                            {
                                entity.DevicePIDVID = Mfg.ToString();
                            }

                            StringBuilder realPath            = new StringBuilder("", 2047);
                            bool          resHardwareRealPath = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_LOCATION_PATHS, 0, realPath, MAX_DEV_LEN, IntPtr.Zero);
                            if (!resHardwareRealPath)
                            {
                            }
                            else
                            {
                                entity.RealTimePath = realPath.ToString();
                            }

                            StringBuilder installState            = new StringBuilder("", 64);
                            bool          resHardwareInstallState = SetupDiGetDeviceRegistryPropertyA(devs, DeviceInfoData, SPDRP_INSTALL_STATE, 0, installState, MAX_DEV_LEN, IntPtr.Zero);
                            if (!resHardwareInstallState)
                            {
                            }
                            else
                            {
                                entity.InstallState = installState.ToString();
                            }
                            //bool isHidde = !SetupDiSelectDevice(devsNow, DeviceInfoData);
                            entity.IsHiddenDevice = true;
                            list.Add(entity);
                        }
                    }
                    SetupDiDestroyDeviceInfoList(devs);
                    SetupDiDestroyDeviceInfoList(devsNow);
                }
                return(list);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        public static List <DeviceEntity> GetNomarlDevice()
        {
            UInt32 RequiredSize = 0;
            Guid   guid         = Guid.Empty;

            Guid[]          guids          = new Guid[1];
            SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
            bool            res            = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize);

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

                if (!res || RequiredSize == 0)
                {
                    Debug.WriteLine("类型不正确");
                }
            }
            if (res == true)
            {
                List <DeviceEntity> list = new List <Hardware.DeviceEntity>();
                Guid   myGUID            = System.Guid.Empty;
                IntPtr hDevInfo          = SetupDiGetClassDevs(guids[0], 0, IntPtr.Zero, DIGCF_PRESENT);
                if (hDevInfo.ToInt64() == -1)
                {
                    Debug.WriteLine("SetupDiGetClassDevs: 获取设备错误。");
                }
                else
                {
                    DeviceInfoData.cbSize    = Marshal.SizeOf(DeviceInfoData);
                    DeviceInfoData.devInst   = 0;
                    DeviceInfoData.classGuid = System.Guid.Empty;
                    DeviceInfoData.reserved  = 0;
                    UInt32 i;
                    for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
                    {
                        DeviceEntity  entity     = new DeviceEntity();
                        StringBuilder DeviceName = new StringBuilder("", 254);
                        bool          resName    = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero);
                        if (!resName)
                        {
                        }
                        else
                        {
                            entity.DeviceName = DeviceName.ToString();
                        }

                        StringBuilder DeviceID      = new StringBuilder("", 254);
                        bool          resHardwareID = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_HARDWAREID, 0, DeviceID, MAX_DEV_LEN, IntPtr.Zero);
                        if (!resHardwareID)
                        {
                            //设备ID未知
                            DeviceID = new StringBuilder("");
                        }
                        else
                        {
                            entity.DeviceID = DeviceID.ToString();
                        }


                        StringBuilder Mfg            = new StringBuilder("", 254);
                        bool          resHardwareMfg = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_MFG, 0, Mfg, MAX_DEV_LEN, IntPtr.Zero);
                        if (!resHardwareMfg)
                        {
                        }
                        else
                        {
                            entity.DevicePIDVID = Mfg.ToString();
                        }
                        StringBuilder realPath            = new StringBuilder("", 2047);
                        bool          resHardwareRealPath = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_LOCATION_PATHS, 0, realPath, MAX_DEV_LEN, IntPtr.Zero);
                        if (!resHardwareRealPath)
                        {
                        }
                        else
                        {
                            entity.RealTimePath = realPath.ToString();
                        }

                        StringBuilder installState            = new StringBuilder("", 64);
                        bool          resHardwareInstallState = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_INSTALL_STATE, 0, installState, MAX_DEV_LEN, IntPtr.Zero);
                        if (!resHardwareInstallState)
                        {
                        }
                        else
                        {
                            entity.InstallState = installState.ToString();
                        }

                        list.Add(entity);
                    }
                    SetupDiDestroyDeviceInfoList(hDevInfo);
                }
                return(list);
            }
            else
            {
                return(null);
            }
        }