Windows Update Agent纯win32 API

时间:2012-10-18 07:11:56

标签: c++ winapi windows-update

我正在开发示例代码以获取有关Windows更新监控的信息。 我碰到了Windows Update Agent API。链接:http://msdn.microsoft.com/en-us/library/windows/desktop/aa387099(v=vs.85).aspx

但是我无法找到任何win32的API。我只找到C#/。NET接口。 有没有相应的win32 API?

具体来说,我想找出一个Windows更新/补丁的“发布日期”。 期待任何建议和指导。

  • Srivathsa

1 个答案:

答案 0 :(得分:10)

WUA API包含一组可以在C ++应用中使用的COM接口,请尝试使用这些IUpdateSearcherIUpdateSessionIUpdate

检查此示例c ++应用程序,该应用程序检索更新和发布日期。

#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=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;
    DATE retdate;

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

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

    for (LONG i = 0; i < updateSize; i++)
    {
        updateList->get_Item(i,&updateItem);
        updateItem->get_Title(&updateName);
        updateItem->get_LastDeploymentChangeTime(&retdate);
        COleDateTime odt;
        odt.m_dt=retdate;
        wcout<<i+1<<" - "<<updateName<<"  Release Date "<< (LPCTSTR)odt.Format(_T("%A, %B %d, %Y"))<<endl;
    }
    ::CoUninitialize();
    wcin.get();
    return 0;
}