3

尝试使用 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)
4

1 回答 1

0

这是你的问题:

if((hr = updateSearcher->put_ServerSelection(ssWindowsUpdate)) != S_OK) {
    exit(-1);
}

这指示 WUAPI 明确使用 Windows 更新,其中仅包括 Windows 本身的更新,而不包括其他 Microsoft 软件的更新。VBScript 代码不这样做,因此它使用配置为默认值的任何服务。在您的计算机上,这似乎是 Microsoft 更新。

我已经在测试机器上尝试了您的代码(也配置为默认为 Microsoft Update),并确认删除对 put_ServerSelection() 的调用解决了问题。代码是以 32 位还是 64 位运行似乎没有任何区别。

如果您想显式搜索 Microsoft 更新而不考虑计算机的配置,您可以通过显式指定服务 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 可以在标题为Opt-In to Microsoft Update的 MSDN 文章中的示例代码中找到。

(并不总是需要首先选择加入 Microsoft Update 才能使用 API 执行 Microsoft Update 扫描。在我的测试中,Windows 7 机器不需要选择加入,但 Windows 10 机器需要。这可能会有所不同,具体取决于情况。)

于 2016-09-11T02:27:11.093 回答