/// <summary> /// Applies the specified driver to the specified device. /// </summary> /// <param name="deviceInfo">The device info.</param> /// <param name="driverInstance">The driver instance.</param> public static void ApplyDriverToDevice( DeviceInformation deviceInfo, DriverInstance driverInstance) { IntPtr deviceInfoSet; SP_DEVINFO_DATA devInfoData; GetDeviceHandleFromMatch(deviceInfo, out deviceInfoSet, out devInfoData); SP_DRVINFO_DATA drvInfoData; GetDriverHandleFromMatch(driverInstance, deviceInfoSet, devInfoData, out drvInfoData); try { bool needReboot = false; Win32SetupApi.DiInstallDevice( IntPtr.Zero, deviceInfoSet, ref devInfoData, ref drvInfoData, 0, ref needReboot); RebootRequired = needReboot; } catch (Exception) { throw; } }
/// <summary> /// Gets a driver handle from matching. /// </summary> /// <param name="driverInstance">The driver instance.</param> /// <param name="deviceInfoSet">The device info set.</param> /// <param name="deviceInfoData">The device info data.</param> /// <param name="drvInfoData">The DRV info data.</param> private static void GetDriverHandleFromMatch( DriverInstance driverInstance, IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, out SP_DRVINFO_DATA drvInfoData) { drvInfoData = new SP_DRVINFO_DATA(); drvInfoData.Size = Marshal.SizeOf(typeof(SP_DRVINFO_DATA)); if (Win32SetupApi.SetupDiBuildDriverInfoList(deviceInfoSet, ref deviceInfoData, Constants.SPDIT_COMPATDRIVER)) { for (int i = 0; Win32SetupApi.SetupDiEnumDriverInfo(deviceInfoSet, ref deviceInfoData, Constants.SPDIT_COMPATDRIVER, i, ref drvInfoData); i++) { if (driverInstance.Description.Equals(drvInfoData.Description)) { return; } } } drvInfoData = new SP_DRVINFO_DATA(); return; }