public void SetProperty(SPDRP property, Byte[] value) { using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { if (dis.IsInvalid) { throw new Win32Exception(); } SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) { throw new Win32Exception(); } if (!SetupApi.SetupDiSetDeviceRegistryProperty(dis, ref dd, property, value, (uint)value.Length)) { throw new Win32Exception(); } } }
/// <summary> /// Flag phantom/removed devices for reinstallation. /// See: http://msdn.microsoft.com/en-us/library/aa906206.aspx /// </summary> public static int CheckRemoved(string deviceHardwareID) { int devIndex; int removed = 0; bool bSuccess; // Initialize the SP_DEVINFO_DATA structure SetupApi.SP_DEVINFO_DATA devInfoData = new SetupApi.SP_DEVINFO_DATA(); devInfoData.cbSize = (uint)Marshal.SizeOf(typeof(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(removed); } for (devIndex = 0;; devIndex++) { 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; } uint status; uint pbmNumber; // If not Unplugged. bSuccess = (SetupApi.CM_Get_DevNode_Status(out status, out pbmNumber, devInfoData.DevInst, 0) == SetupApi.CR.NO_SUCH_DEVNODE); if (!bSuccess) { continue; } // Flag for reinstall on next plugin uint configFlags; bSuccess = SetupApi.SetupDiGetDeviceRegistryProperty(out configFlags, pDevInfo, ref devInfoData, SetupApi.SPDRP.CONFIGFLAGS); if (!bSuccess) { InfWizardStatus.Log(CategoryType.CheckRemoved, StatusType.Warning, "could not read SPDRP_CONFIGFLAGS for phantom device {0}", deviceHardwareID); continue; } // Mark for re-installation configFlags |= (uint)SetupApi.CONFIGFLAG.REINSTALL; uint[] flags = new uint[] { configFlags }; bSuccess = SetupApi.SetupDiSetDeviceRegistryProperty(pDevInfo, ref devInfoData, SetupApi.SPDRP.CONFIGFLAGS, flags, Marshal.SizeOf(typeof(uint))); if (!bSuccess) { InfWizardStatus.Log(CategoryType.CheckRemoved, StatusType.Warning, "could not write SPDRP_CONFIGFLAGS for phantom device {0}", deviceHardwareID); continue; } removed++; } if (removed > 0) { InfWizardStatus.Log(CategoryType.CheckRemoved, StatusType.Info, "flagged {0} removed devices for reinstallation", removed); } SetupApi.SetupDiDestroyDeviceInfoList(pDevInfo); return(removed); }