Пример #1
0
        public void Reenumerate(UInt32 flags)
        {
            CR ret = SetupApi.CM_Reenumerate_DevNode(DevInst, flags);

            CMException.Throw(ret, "CM_Reenumerate_DevNode");
        }
Пример #2
0
        /// <summary>
        /// Force re-enumeration of a device (force installation)
        /// TODO: allow root re-enum
        /// </summary>
        public static int UpdateDriver(string deviceHardwareID)
        {
            int  devIndex;
            int  refreshed = 0;
            bool bSuccess;

            // Initialize the SP_DEVINFO_DATA structure
            SetupApi.SP_DEVINFO_DATA devInfoData = new SetupApi.SP_DEVINFO_DATA();

            // List all connected USB devices
            IntPtr pDevInfo = SetupApi.SetupDiGetClassDevs(0, "USB", IntPtr.Zero, SetupApi.DICFG.ALLCLASSES);

            if (pDevInfo == IntPtr.Zero || pDevInfo == new IntPtr(-1))
            {
                return(refreshed);
            }
            for (devIndex = 0;; devIndex++)
            {
                devInfoData.cbSize = (uint)Marshal.SizeOf(typeof(SetupApi.SP_DEVINFO_DATA));
                bSuccess           = SetupApi.SetupDiEnumDeviceInfo(pDevInfo, devIndex, ref devInfoData);

                // Reached the end of the deviceInfo list.
                if (!bSuccess)
                {
                    break;
                }

                // Find the hardware ID
                string[] saHardwareIDs;
                bSuccess = SetupApi.SetupDiGetDeviceRegistryProperty(out saHardwareIDs,
                                                                     pDevInfo,
                                                                     ref devInfoData,
                                                                     SetupApi.SPDRP.HARDWAREID);

                // Failed getting hardware id
                if (!bSuccess)
                {
                    break;
                }

                // Failed getting hardware id
                if (saHardwareIDs.Length == 0)
                {
                    continue;
                }

                // Check all hardwareids for a match
                bool bFound = false;
                foreach (string s in saHardwareIDs)
                {
                    if (s.Trim().ToLower() == deviceHardwareID.Trim().ToLower())
                    {
                        bFound = true;
                        break;
                    }
                }

                // Hardware did not match; goto next device
                if (!bFound)
                {
                    continue;
                }

                // Re-enumerate the device node
                SetupApi.CR status = SetupApi.CM_Reenumerate_DevNode((int)devInfoData.DevInst,
                                                                     SetupApi.CM.REENUMERATE_RETRY_INSTALLATION);
                if (status == SetupApi.CR.SUCCESS)
                {
                    InfWizardStatus.Log(CategoryType.RefreshDriver, StatusType.Success, "re-enumeration of {0} succeeded...", deviceHardwareID);
                }
                else if (status == SetupApi.CR.INVALID_DEVNODE)
                {
                    continue;
                }
                else
                {
                    InfWizardStatus.Log(CategoryType.RefreshDriver, StatusType.Warning, "failed to re-enumerate device node: CR code {0}", status);
                    continue;
                }



                refreshed++;
            }
            // return the number of devices that were re-enumerated.
            return(refreshed);
        }