有时我们需要确定系统中是否安装了特定的 Windows 修补程序或更新。要执行此任务,您可以使用两种方法
WMI(Windows 管理规范)
使用 Win32_QuickFixEngineering类,您可以检索小型系统范围的更新,通常称为快速修复工程 (QFE) 更新。
检查此代码,其中列出了系统中安装的更新
procedure GetWin32_QuickFixEngineeringInfo; const wbemFlagForwardOnly = $00000020; var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet: OLEVariant; FWbemObject : OLEVariant; oEnum : IEnumvariant; iValue : LongWord; begin; FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_QuickFixEngineering','WQL',wbemFlagForwardOnly); oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; while oEnum.Next(1, FWbemObject, iValue) = 0 do begin Writeln(Format('Caption %s',[FWbemObject.Caption]));// String //Writeln(Format('CSName %s',[FWbemObject.CSName]));// String Writeln(Format('Description %s',[FWbemObject.Description]));// String Writeln(Format('FixComments %s',[FWbemObject.FixComments]));// String Writeln(Format('HotFixID %s',[FWbemObject.HotFixID]));// String Writeln(Format('InstallDate %s',[FWbemObject.InstallDate]));// Datetime Writeln(Format('InstalledBy %s',[FWbemObject.InstalledBy]));// String Writeln(Format('InstalledOn %s',[FWbemObject.InstalledOn]));// String Writeln(Format('Name %s',[FWbemObject.Name]));// String Writeln(Format('ServicePackInEffect %s',[FWbemObject.ServicePackInEffect]));// String Writeln(Format('Status %s',[FWbemObject.Status]));// String Writeln(''); FWbemObject:=Unassigned; end; end;
现在要查找是否安装了特定更新,您可以检查HotFixID 属性值(这是与特定更新关联的唯一标识符)并编写这样的函数
//use in this way ISHotFixID_Installed('KB982799') function ISHotFixID_Installed(const HotFixID : string): Boolean; const wbemFlagForwardOnly = $00000020; wbemFlagReturnImmediately = $00000010; var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet: OLEVariant; FWbemObject : OLEVariant; oEnum : IEnumvariant; iValue : LongWord; begin; FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_QuickFixEngineering Where HotFixID="%s"',[HotFixID]),'WQL',wbemFlagForwardOnly OR wbemFlagReturnImmediately); oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; Result:= oEnum.Next(1, FWbemObject, iValue) = 0; end;
好的,这很好,但是从 Windows Vista 开始,Win32_QuickFixEngineering类只返回由基于组件的服务(CBS) 提供的更新,因此没有列出一些更新。
WUA(Windows 更新代理)
使用Windows 更新代理 API是检索更新列表的最佳选择,您可以从 delphi 访问此 API 的接口和对象,导入wuapi.dll文件或使用Microsoft.Update.Session创建后期绑定 com 对象图形用户界面。下一个示例使用后期绑定方式。
来自 MSDN 网站:
Windows Update 代理 (WUA) API 是一组 COM 接口,使系统管理员和程序员能够访问 Windows Update 和 Windows Server Update Services (WSUS)。可以编写脚本和程序来检查当前可用于计算机的更新,然后您可以安装或卸载更新。
要实现对已安装更新的搜索,我们需要使用IUpdateSearcher接口,使用Search方法设置搜索条件。
检查这个样本
//create the Com object instance updateSession:= CreateOleObject('Microsoft.Update.Session'); updateSearcher := updateSession.CreateUpdateSearcher; //set the search criteria, installed =1 means updates that are installed on the destination computer, Type='Software' retrieve only applications updates updateSearchResult:= updateSearcher.Search(Format('IsInstalled = 1 and Type=%s',[QuotedStr('Software')]));
现在要查找是否安装了特定更新,您可以解析IUpdate 接口的Title属性,其中包含更新的名称,如适用于基于 x64 的系统的 Windows 7 安全更新 (KB978542)
//use in this way ISHotFixID_Installed('KB982799') function ISHotFixID_Installed(const HotFixID : string): Boolean; var updateSession : OleVariant; updateSearcher : OleVariant; updateEntry : OleVariant; updateSearchResult : OleVariant; UpdateCollection : OleVariant; oEnum : IEnumvariant; iValue : LongWord; begin result:=False; updateSession:= CreateOleObject('Microsoft.Update.Session'); updateSearcher := updateSession.CreateUpdateSearcher; //this line improves the performance , the online porperty indicates whether the UpdateSearcher goes online to search for updates. so how we are looking for already installed updates we can set this value to false updateSearcher.online:=False; updateSearchResult:= updateSearcher.Search(Format('IsInstalled = 1 and Type=%s',[QuotedStr('Software')])); UpdateCollection := updateSearchResult.Updates; oEnum := IUnknown(UpdateCollection._NewEnum) as IEnumVariant; while oEnum.Next(1, updateEntry, iValue) = 0 do begin Result:=Pos(HotFixID,updateEntry.Title)>0; updateEntry:=Unassigned; if Result then break; end; end;
检查这些其他有用的功能
获取已安装更新列表
procedure GetListInstalledUpdates; var updateSession : OleVariant; updateSearcher : OleVariant; updateSearchResult : OleVariant; updateEntry : OleVariant; UpdateCollection : OleVariant; oEnum : IEnumvariant; iValue : LongWord; begin updateSession:= CreateOleObject('Microsoft.Update.Session'); updateSearcher := updateSession.CreateUpdateSearcher; Writeln('Searching'); //IUpdateSearcher::Search Method http://msdn.microsoft.com/en-us/library/aa386526%28v=VS.85%29.aspx updateSearcher.online:=False; updateSearchResult:= updateSearcher.Search(Format('IsInstalled = 1 and Type=%s',[QuotedStr('Software')])); UpdateCollection := updateSearchResult.Updates; oEnum := IUnknown(UpdateCollection._NewEnum) as IEnumVariant; //IUpdate Interface http://msdn.microsoft.com/en-us/library/aa386099%28v=VS.85%29.aspx while oEnum.Next(1, updateEntry, iValue) = 0 do begin Writeln(updateEntry.Title); updateEntry:=Unassigned; end; Writeln('Done'); end;
获取未安装更新列表(缓慢,因为需要在线查看)
procedure GetListNotInstalledUpdates; var updateSession : OleVariant; updateSearcher : OleVariant; updateSearchResult : OleVariant; updateEntry : OleVariant; UpdateCollection : OleVariant; oEnum : IEnumvariant; iValue : LongWord; begin updateSession:= CreateOleObject('Microsoft.Update.Session'); updateSearcher := updateSession.CreateUpdateSearcher; Writeln('Searching'); updateSearchResult:= updateSearcher.Search(Format('IsInstalled = 0 and Type=%s',[QuotedStr('Software')])); UpdateCollection := updateSearchResult.Updates; oEnum := IUnknown(UpdateCollection._NewEnum) as IEnumVariant; while oEnum.Next(1, updateEntry, iValue) = 0 do begin Writeln(updateEntry.Title); updateEntry:=Unassigned; end; Writeln('Done'); end;
获取隐藏的已安装更新列表
procedure GetListInstalledHiddenUpdates; var updateSession : OleVariant; updateSearcher : OleVariant; updateSearchResult : OleVariant; updateEntry : OleVariant; UpdateCollection : OleVariant; oEnum : IEnumvariant; iValue : LongWord; begin updateSession:= CreateOleObject('Microsoft.Update.Session'); updateSearcher := updateSession.CreateUpdateSearcher; Writeln('Searching'); updateSearcher.online:=False; updateSearchResult:= updateSearcher.Search(Format('IsHidden=1 and IsInstalled = 1 and Type=%s',[QuotedStr('Software')])); UpdateCollection := updateSearchResult.Updates; oEnum := IUnknown(UpdateCollection._NewEnum) as IEnumVariant; while oEnum.Next(1, updateEntry, iValue) = 0 do begin Writeln(updateEntry.Title); updateEntry:=Unassigned; end; Writeln('Done'); end;
本篇文章链接 地址:https://wmzos.com/?id=14
添加新评论