使用 wuapi 获取 Windows 系统更新

Get Windows System updates using wuapi

在我的应用程序中,我显示 windows 机器是否有可用的更新,这应该与 "Windows Update" 设置相同。我正在使用这样的代码,它通常有效。

#include "stdafx.h"
#include <wuapi.h>
#include <iostream>
#include <ATLComTime.h>
#include <wuerror.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr;
    hr = CoInitialize(NULL);

    IUpdateSession* iUpdate;
    IUpdateSearcher* searcher;
    ISearchResult* results;
    BSTR criteria = SysAllocString(L"IsInstalled=0 and IsHidden=0");

    hr = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID*)&iUpdate);
    hr = iUpdate->CreateUpdateSearcher(&searcher);

    wcout << L"Searching for updates ..."<<endl;
    hr = searcher->Search(criteria, &results); 
    SysFreeString(criteria);

    //...
}

我的问题是,在某些机器上,此代码给出的结果是有可用更新,但在 "Windows Update" 设置页面中没有。我检查了日志,有一些更新标记为 "incomplete/invalid",因此未显示在 "Windows Update" 设置页面中,但由于某种原因此代码获取了它们。我认为问题在于使用了不正确的搜索查询。也许
"IsInstalled=0 and IsHidden=0"还不够。是否可以确切地知道 "Windows Update" 设置使用什么查询来显示更新并在我的应用程序中使用相同的查询?

解决方法是在条件中添加"IsAssigned=1"。