尝试使用wuapi查询Windows 7 Enterprise SP1以获取更新时,附加的VBScript示例返回的结果与附加的C ++示例不同。 VBScript包含Microsoft Office的更新,这就是我想要的。在我的研究过程中,我发现了暴露SearchScope的IUpdateSearcher3接口,这似乎是我需要使用的东西。但是,我在创建界面时遇到了困难。我的问题是,为什么两个版本的结果不同,如何使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)
答案 0 :(得分:0)
这是你的问题:
if((hr = updateSearcher->put_ServerSelection(ssWindowsUpdate)) != S_OK) {
exit(-1);
}
指示WUAPI明确使用Windows Update,其中仅包括Windows本身的更新,而不包括其他Microsoft软件的更新。 VBScript代码不会执行此操作,因此它使用配置为默认值的任何服务。在您的计算机上,这似乎是Microsoft Update。
我已经在测试计算机上尝试了您的代码(也配置为默认为Microsoft Update),并确认删除对put_ServerSelection()的调用可以解决问题。代码是以32位还是64位运行似乎没什么区别。
如果要显式搜索Microsoft Update而不管计算机的配置方式如何,您可以通过显式指定服务ID来执行此操作。在企业环境中,这还将确保客户端直接搜索Microsoft Update而不是本地WSUS服务器:
if((hr = updateSearcher->put_ServerSelection(ssOthers)) != S_OK) {
exit(-1);
}
BSTR serviceID = SysAllocString(L"7971f918-a847-4430-9279-4a52d1efe18d");
if ((hr = updateSearcher->put_ServiceID(serviceID)) != S_OK)
{
exit(-1);
}
SysFreeString(serviceID);
上面显示的Microsoft Update的GUID可以在MSDN文章标题为Opt-In to Microsoft Update的示例代码中找到。
(并不总是首先选择加入Microsoft Update以便使用API执行Microsoft Update扫描。在我的测试中,Windows 7计算机不需要选择加入,但是Windows 10计算机确实如此。因情况而异。)