Get Windows System updates using wuapi

Viewed 204

In my application I show if windows machine has updates available which should be the same as "Windows Update" Settings. I am using a code like this, which usually works.

#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);

    //...
}

My problem is that on some machines this code gives result that there are updates available but in "Windows Update" Settings page there are not. I have checked the logs, there are some updates which are marked as "incomplete/invalid" so are not shown in "Windows Update" Settings page but this code gets them for some reason. I think the problem is using incorrect search query. Maybe
"IsInstalled=0 and IsHidden=0"is not enough. Is it possible to know exactly what query "Windows Update" Settings uses to show updates and use the same query in my application?

1 Answers

The solution is to add "IsAssigned=1" to the criteria.

Related