IUpdate :: BundledUpdates究竟检索了什么?

时间:2012-10-10 11:27:59

标签: windows winapi desktop-application windows-update wmic

当我们尝试使用WUA API检索Windows Update信息时,以下是我遵循的过程。但我对IUpdate :: BundledUpdates属性感到困惑。

  1. 创建IUpdateSearcher
  2. 根据搜索条件进行搜索。我提供的搜索条件为“IsHidden = 1或IsInstalled = 1”
  3. 搜索结果将包含IUpdateCollection。
  4. 在IUpdateCollection中使用get_Item,我检索了每个更新(IUpdate)并打印了所需的值(在我的情况下为KB编号)。
  5. 但是在IUpdate中,您有一个BundledUpdate属性,它为IUpdateCollection提供了get_BundledUpdates()方法。当我迭代BundledUpdates的结果时,我没有结果。
  6. 我在检索捆绑更新时遗漏了什么? (OR)我指定的标准是否包含捆绑更新作为IUpdateCollection的第一个结果集的一部分?

    同样在MSDN中,WUA API中的每个接口都缺少示例,有人可以提供任何资源来清楚地解释WUA API中的每个接口的作用吗?

    添加了C ++控制台应用程序的完整源代码:

    #include <wuapi.h>
    #include <iostream>
    #include <wuerror.h>
    
    using namespace std;
    
    
    int main()
    {
    
        HRESULT hr;
        hr = CoInitialize(NULL);
    
        IUpdateSession* iUpdate;
        IUpdateSearcher* searcher;
        ISearchResult* results;
        BSTR criteria = SysAllocString(L"IsInstalled=1 or IsHidden=1 or IsPresent=1");
    
        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);
    
        switch(hr)
        {
        case S_OK:
            wcout<<L"List of applicable items on the machine:"<<endl;
            break;
        case WU_E_LEGACYSERVER:
            wcout<<L"No server selection enabled"<<endl;
            return 0;
        case WU_E_INVALID_CRITERIA:
            wcout<<L"Invalid search criteria"<<endl;
            return 0;
        }
    
        IUpdateCollection * updateList;
        IUpdate *updateItem;
        LONG updateSize;
        BSTR updateName;
    
        results->get_Updates(&updateList);
        updateList->get_Count(&updateSize);
    
        if (updateSize == 0)
        {
            wcout << L"No updates found"<<endl;
        }
    
        for (LONG i = 0; i < updateSize; i++)
        {
            IStringCollection *KBCollection;
            LONG KBCount;
            updateList->get_Item(i, &updateItem);
            updateItem->get_KBArticleIDs(&KBCollection);
            KBCollection->get_Count(&KBCount);
            for(int k=0;k<KBCount;k++)
            {
                BSTR KBValue;
                KBCollection->get_Item(k,&KBValue);
                wcout << L"KB" << KBValue << endl;
            }
    
            //Retrieve the bundled updates
            IUpdateCollection *updtCollection;
            updateItem->get_BundledUpdates(&updtCollection);
            LONG updtBundledCount;
            updtCollection->get_Count(&updtBundledCount);
            for(LONG j=0;j<updtBundledCount;j++)
            {
                cout<<"Bundled KBs" <<endl;
                IUpdate *bundledUpdateItem;
                updtCollection->get_Item(j,&bundledUpdateItem);
                bundledUpdateItem->get_KBArticleIDs(&KBID);
                if(KBID != NULL)
                {
                    LONG KBCount;
                    BSTR KBIDValue;
    
                    KBID->get_Count(&KBCount);
                    for(LONG j=0;j<KBCount;j++)
                    {
    
                        wcout << "KB" <<(KBIDValue) << endl;
                        temp.setKBID(KBIDValue);
                    }
                }
            }
        }
    
        wcout << L"Total KB Count : " << updateSize << endl;
        CoUninitialize();
    
        return 0;
    }
    

1 个答案:

答案 0 :(得分:2)

我只修复你的代码以检索捆绑更新。

#include "stdafx.h"
#include <wuapi.h>
#include <iostream>
#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=1 or IsHidden=1 or IsPresent=1");

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

    switch(hr)
    {
    case S_OK:
        wcout<<L"List of applicable items on the machine:"<<endl;
        break;
    case WU_E_LEGACYSERVER:
        wcout<<L"No server selection enabled"<<endl;
        return 0;
    case WU_E_INVALID_CRITERIA:
        wcout<<L"Invalid search criteria"<<endl;
        return 0;
    }

    IUpdateCollection *updateList;
    IUpdate *updateItem;
    LONG updateSize;
    BSTR updateName;

    results->get_Updates(&updateList);
    updateList->get_Count(&updateSize);

    if (updateSize == 0)
    {
        wcout << L"No updates found"<<endl;
    }

    for (LONG i = 0; i < updateSize; i++)
    {
        IStringCollection *KBCollection;
        LONG KBCount;
        updateList->get_Item(i,&updateItem);
        updateItem->get_Title(&updateName);
        wcout<<i+1<<" - "<<updateName<<endl;

        IUpdateCollection *updtCollection;
        LONG updtBundledCount;        
        //Retrieve the bundled updates
        updateItem->get_BundledUpdates(&updtCollection);
        hr = updtCollection->get_Count(&updtBundledCount);
        if ((updtBundledCount>0) && (hr ==S_OK))
        {
            wcout << L"Bundled Updates " <<(updtBundledCount) << endl;
            for(LONG j=0;j<updtBundledCount;j++)
            {
                IUpdate *bundledUpdateItem;
                BSTR bundledName;
                updtCollection->get_Item(j,&bundledUpdateItem);   
                bundledUpdateItem->get_Title(&bundledName);
                wcout<<L"    "<<j+1<<" - "<<bundledName<<endl;
            }
        }
    }
    ::CoUninitialize();
    wcin.get();
    return 0;
}