WindowsUpdate.cs (3557B)
1 using System.Runtime.InteropServices; 2 using System.Text.RegularExpressions; 3 4 namespace DriverUpdater; 5 6 // Finds and installs driver updates through the Windows Update Agent API, the same API the Settings 7 // app uses. These are drivers the vendors submitted to Microsoft and Microsoft signed. It's all COM, 8 // called through dynamic so the project needs no interop assembly. 9 static partial class WindowsUpdate 10 { 11 // ServerSelection value that asks Microsoft's servers directly, even on a machine managed by WSUS. 12 const int ServerSelectionWindowsUpdate = 2; 13 14 // OperationResultCode values. 15 const int Succeeded = 2; 16 const int SucceededWithErrors = 3; 17 18 public static List<DriverUpdate> Search(IReadOnlyList<InstalledDevice> installed) 19 { 20 dynamic session = Com.Create("Microsoft.Update.Session"); 21 dynamic searcher = session.CreateUpdateSearcher(); 22 searcher.ServerSelection = ServerSelectionWindowsUpdate; 23 dynamic result = searcher.Search("IsInstalled=0 and IsHidden=0 and Type='Driver'"); 24 25 var updates = new List<DriverUpdate>(); 26 foreach (dynamic update in result.Updates) 27 { 28 string hardwareId = update.DriverHardwareID; 29 string title = update.Title; 30 var device = installed.FirstOrDefault(d => 31 d.HardwareIds.Contains(hardwareId, StringComparer.OrdinalIgnoreCase)); 32 33 updates.Add(new DriverUpdate 34 { 35 Device = update.DriverModel, 36 Source = "Windows Update", 37 InstalledVersion = device?.DriverVersion ?? "", 38 AvailableVersion = VersionAtEndOfTitle().Match(title).Groups[1].Value, 39 ReleaseDate = update.DriverVerDate, 40 SizeBytes = (long)(decimal)update.MaxDownloadSize, 41 WindowsUpdateItem = update, 42 Status = "Update available", 43 }); 44 } 45 return updates; 46 } 47 48 public static void Download(dynamic update) 49 { 50 dynamic session = Com.Create("Microsoft.Update.Session"); 51 dynamic downloader = session.CreateUpdateDownloader(); 52 downloader.Updates = SingleUpdateCollection(update); 53 ThrowIfFailed(downloader.Download()); 54 } 55 56 // Returns true if Windows needs a restart to finish the install. 57 public static bool Install(dynamic update) 58 { 59 if (!update.EulaAccepted) 60 update.AcceptEula(); 61 62 dynamic session = Com.Create("Microsoft.Update.Session"); 63 dynamic installer = session.CreateUpdateInstaller(); 64 installer.Updates = SingleUpdateCollection(update); 65 dynamic result = installer.Install(); 66 ThrowIfFailed(result); 67 return result.RebootRequired; 68 } 69 70 static dynamic SingleUpdateCollection(dynamic update) 71 { 72 dynamic collection = Com.Create("Microsoft.Update.UpdateColl"); 73 collection.Add(update); 74 return collection; 75 } 76 77 static void ThrowIfFailed(dynamic result) 78 { 79 int code = result.ResultCode; 80 if (code is Succeeded or SucceededWithErrors) 81 return; 82 83 int hresult = result.GetUpdateResult(0).HResult; 84 throw new COMException($"Windows Update error 0x{hresult:X8}", hresult); 85 } 86 87 // Driver update titles usually end with the version, as in "NVIDIA - Display - 32.0.15.6094" 88 // or "Apple, Inc. USBDevice Driver Update (552.0.0.0)". The API has no version field. 89 [GeneratedRegex(@"(\d+(?:\.\d+){1,3})\)?$")] 90 private static partial Regex VersionAtEndOfTitle(); 91 }