尝试使用 wuapi 查询 Windows 7 Enterprise SP1 的更新时,附加的 VBScript 示例返回的结果与附加的 C++ 示例不同。VBScript 包括 Microsoft Office 的更新,这正是我想要的。在我的研究过程中,我发现了 IUpdateSearcher3 接口,它公开了 SearchScope,这似乎是我需要使用的。但是,我在创建界面时遇到了困难。我的问题是,为什么两个版本的结果不同,如何使 C++ 版本包含 Office 检查以与 VBScript 版本保持一致?
附上示例代码和输出。请注意,C++ 版本不包括来自 VBScript 输出的 Office 更新:
50> Security Update for Microsoft Outlook 2010 (KB3115474) 32-Bit Edition
51> Security Update for Microsoft Office 2010 (KB3114400) 32-Bit Edition
52> Security Update for Microsoft Office 2010 (KB3114869) 32-Bit Edition
57> Security Update for Microsoft Word 2010 (KB3115471) 32-Bit Edition
58> Update for Microsoft Excel 2010 (KB3115476) 32-Bit Edition
59> Definition Update for Microsoft Office 2010 (KB3115475) 32-Bit Edition
VBScript 示例:
Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "MSDN Sample Script"
Set updateSearcher = updateSession.CreateUpdateSearcher()
WScript.Echo "Searching for updates..." & vbCRLF
Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
WScript.Echo "List of applicable items on the machine:"
For I=0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
WScript.Echo I + 1 & "> " & update.Title
Next
VBScript 输出:正在搜索更新...
List of applicatble items on the machine:
1> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2656356)
2> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2604115)
3> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2729452)
4> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2742599)
5> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2789645)
6> Update for Windows 7 (KB2798162)
7> Security Update for Windows 7 (KB2813430)
8> Update for Windows 7 (KB2868116)
9> Security Update for Windows 7 (KB2868626)
10> Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2836943)
11> Security Update for Windows 7 (KB2871997)
12> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2931356)
13> Security Update for Windows 7 (KB2973351)
14> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2937610)
15> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2943357)
16> Security Update for Windows 7 (KB2758857)
17> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2972211)
18> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2973112)
19> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2894844)
20> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2972100)
21> Security Update for Windows 7 (KB2984972)
22> Security Update for Windows 7 (KB2992611)
23> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2978120)
24> Security Update for Windows 7 (KB3003743)
25> Security Update for Windows 7 (KB3011780)
26> Security Update for Windows 7 (KB3004375)
27> Security Update for Windows 7 (KB3033929)
28> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB3037574)
29> Security Update for Windows 7 (KB3061518)
30> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB3023215)
31> Security Update for Windows 7 (KB3031432)
32> Security Update for Windows 7 (KB3072630)
33> Security Update for Windows 7 (KB3060716)
34> Security Update for Windows 7 (KB3071756)
35> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB3074543)
36> Security Update for Windows 7 (KB3042058)
37> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB3097989)
38> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 (KB3122648)
39> Security Update for Windows 7 (KB3126587)
40> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 (KB3127220)
41> Update for Windows 7 (KB3138612)
42> Security Update for Windows 7 (KB3146706)
43> Security Update for Windows 7 (KB3149090)
44> Security Update for Windows 7 (KB3153171)
45> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 (KB3142024)
46> Security Update for Windows 7 (KB3161561)
47> Security Update for Windows 7 (KB3159398)
48> Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 (KB3163245)
49> Security Update for Microsoft OneNote 2010 (KB3114885) 32-Bit Edition
50> Security Update for Microsoft Outlook 2010 (KB3115474) 32-Bit Edition
51> Security Update for Microsoft Office 2010 (KB3114400) 32-Bit Edition
52> Security Update for Microsoft Office 2010 (KB3114869) 32-Bit Edition
53> Security Update for Windows 7 (KB3167679)
54> Security Update for Windows 7 (KB3177725)
55> Security Update for Windows 7 (KB3178034)
56> Cumulative Security Update for Internet Explorer 11 for Windows 7 (KB3175443)
57> Security Update for Microsoft Word 2010 (KB3115471) 32-Bit Edition
58> Update for Microsoft Excel 2010 (KB3115476) 32-Bit Edition
59> Definition Update for Microsoft Office 2010 (KB3115475) 32-Bit Edition
60> Update for Windows 7 (KB3177723)
C++ 示例:
#include "stdafx.h"
#include <Windows.h>
#include <wuapi.h>
int _tmain(int argc, _TCHAR* argv[])
{
IUpdateSearcher* updateSearcher = NULL;
IUpdateSession* updateSession = NULL;
IUpdateCollection* updateList = NULL;
ISearchResult* results = NULL;
IUpdate* updateItem = NULL;
BSTR criteria = NULL;
LONG updateSize = 0;
HRESULT hr;
if((hr = CoInitialize(NULL)) != S_OK) {
exit(-1);
}
if((hr = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER,
IID_IUpdateSession, (LPVOID*)&updateSession)) != S_OK) {
exit(-1);
}
if((hr = updateSession->CreateUpdateSearcher(&updateSearcher)) != S_OK) {
exit(-1);
}
if((hr = updateSearcher->put_ServerSelection(ssWindowsUpdate)) != S_OK) {
exit(-1);
}
criteria = SysAllocString(L"IsInstalled=0 and Type='Software' and IsHidden=0");
hr = updateSearcher->Search(criteria, &results);
if ((hr = updateSearcher->Search(criteria, &results)) == S_OK) {
OutputDebugString(L"[*]Successfully completed search for updates on this host");
} else {
OutputDebugString(L"[-]Failed to search for updates");
}
SysFreeString(criteria);
results->get_Updates(&updateList);
updateList->get_Count(&updateSize);
if(updateSize == 0) {
OutputDebugString(L"[-]No updates available for this host");
CoUninitialize();
exit(0);
}
for(LONG i = 0; i < updateSize; i++) {
BSTR updateName;
updateList->get_Item(i, &updateItem);
updateItem->get_Title(&updateName);
OutputDebugString(updateName);
}
CoUninitialize();
exit(0);
}
C++ 输出:
[*]Successfully completed search for updates on this host
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2656356)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2604115)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2729452)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2742599)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2789645)
Update for Windows 7 (KB2798162)
Security Update for Windows 7 (KB2813430)
Update for Windows 7 (KB2868116)
Security Update for Windows 7 (KB2868626)
Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2836943)
Security Update for Windows 7 (KB2871997)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2931356)
Security Update for Windows 7 (KB2973351)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2937610)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2943357)
Security Update for Windows 7 (KB2758857)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2972211)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2973112)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2894844)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2972100)
Security Update for Windows 7 (KB2984972)
Security Update for Windows 7 (KB2992611)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB2978120)
Security Update for Windows 7 (KB3003743)
Security Update for Windows 7 (KB3011780)
Security Update for Windows 7 (KB3004375)
Security Update for Windows 7 (KB3033929)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB3037574)
Security Update for Windows 7 (KB3061518)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB3023215)
Security Update for Windows 7 (KB3031432)
Security Update for Windows 7 (KB3072630)
Security Update for Windows 7 (KB3060716)
Security Update for Windows 7 (KB3071756)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB3074543)
Security Update for Windows 7 (KB3042058)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 x86 (KB3097989)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 (KB3122648)
Security Update for Windows 7 (KB3126587)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 (KB3127220)
Update for Windows 7 (KB3138612)
Security Update for Windows 7 (KB3146706)
Security Update for Windows 7 (KB3149090)
Security Update for Windows 7 (KB3153171)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 (KB3142024)
Security Update for Windows 7 (KB3161561)
Security Update for Windows 7 (KB3159398)
Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 SP1 (KB3163245)
Security Update for Windows 7 (KB3167679)
Security Update for Windows 7 (KB3177725)
Security Update for Windows 7 (KB3178034)
Cumulative Security Update for Internet Explorer 11 for Windows 7 (KB3175443)
Update for Windows 7 (KB3177723)