简体   繁体   中英

How to get Windows installed updates

I need to get all installed updates on Windows machine. I tried WUA API, but the result is differs from what I see in Control Panel - Installed Updates. My code returns 321 update, while in control panel I see 508. Here is my code:

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)
{
    return -1;
}
if ((hr = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID*)&updateSession)) != S_OK)
{
    return -1;
}
if ((hr = updateSession->CreateUpdateSearcher(&updateSearcher)) != S_OK)
{
    return -1;
}
if ((hr = updateSearcher->put_ServerSelection(ssWindowsUpdate)) != S_OK)
{
    return -1;
}

criteria = SysAllocString(L"IsInstalled=1 or IsInstalled=0 or IsHidden=1 or IsPresent=1");
if ((hr = updateSearcher->Search(criteria, &results)) == S_OK)
{
    std::wcout << L"[*]Successfully completed search for updates on this host" << std::endl;
}
else
{
    std::wcout << L"[-]Failed to search for updates" << std::endl;
    return -1;
}

results->get_Updates(&updateList);
updateList->get_Count(&updateSize);
if (updateSize == 0)
{
    std::wcout << L"[-]No updates available for this host" << std::endl;
    CoUninitialize();
    return 0;
}
std::set<std::wstring> KBs;
for (LONG i = 0; i < updateSize; i++)
{
    IStringCollection *KBCollection;
    LONG KBsSize = 0;
    updateList->get_Item(i, &updateItem);
    updateItem->get_KBArticleIDs(&KBCollection);
    KBCollection->get_Count(&KBsSize);

    for (LONG i = 0; i < KBsSize; i++)
    {
        BSTR KBValue;
        KBCollection->get_Item(i, &KBValue);
        std::wstring ws(KBValue, SysStringLen(KBValue));
        KBs.insert(ws);
    }
}


if ((hr = updateSearcher->put_ServerSelection(ssOthers)) != S_OK)
{
    return -1;
}
BSTR serviceID = SysAllocString(L"7971f918-a847-4430-9279-4a52d1efe18d");

if ((hr = updateSearcher->put_ServiceID(serviceID)) != S_OK)
{
    return -1;
}

hr = updateSearcher->Search(criteria, &results);
if ((hr = updateSearcher->Search(criteria, &results)) == S_OK)
{
    std::wcout << L"[*]Successfully completed search for updates on this host" << std::endl;
}
else
{
    std::wcout << L"[-]Failed to search for updates" << std::endl;
}

results->get_Updates(&updateList);
updateList->get_Count(&updateSize);
if (updateSize == 0)
{
    std::wcout << L"[-]No updates available for this host" << std::endl;
    CoUninitialize();
    return 0;
}
for (LONG i = 0; i < updateSize; i++)
{
    IStringCollection *KBCollection;
    LONG KBsSize = 0;
    updateList->get_Item(i, &updateItem);
    updateItem->get_KBArticleIDs(&KBCollection);
    KBCollection->get_Count(&KBsSize);

    for (LONG i = 0; i < KBsSize; i++)
    {
        BSTR KBValue;
        KBCollection->get_Item(i, &KBValue);
        KBs.insert(KBValue);
    }
}

SysFreeString(criteria);
SysFreeString(serviceID);

CoUninitialize();

std::wcout << KBs.size() << std::endl;

Can anyone please explain how can I get all the updates?

WUA API only lists updates installed through Windows Updates Services.

To list other kind of updates you may use Windows Installer API , specifically:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM