WUA API学习笔记 (二)

来源:互联网 发布:日货清单 国人必知 编辑:程序博客网 时间:2024/06/05 09:52

参考代码:

CODE:

//
//(2) 获取操作系统补丁信息(部分代码已经舍去)
//    使用Windows Update Agent API实现
//    使用离线检测的形式
BOOL GetSystemDefects(struct defects      *system_defects)
{
    int res = NO_ERROR;
    HRESULT ret;
    int flag = 1;
    struct defects *p;
    try
    {
        IUpdateSession   *Session = NULL;
        ret = CoInitialize(NULL);
        if (FAILED(ret))
        {
            Log("GetSystemDefects():Initializes the COM Failed.");
            throw -1;
        }
        
        ret = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER,
            IID_IUpdateSession  , (LPVOID*)&Session);
        if ((Session == NULL) || FAILED(ret))
        {
            //return -1;
            throw -2;
        }
        
        IUpdateSearcher  *Searcher = NULL;
        ret = Session->CreateUpdateSearcher(&Searcher);
        
        if (FAILED(ret) || (Searcher == NULL))
        {
            Session->Release();
            //return -1;
            throw -3;
        }
        
        Searcher->put_Online(VARIANT_FALSE);    //离线查询
    //    Searcher->put_Online(VARIANT_TRUE);    //在线查询
        ISearchResult   *SearchResult = NULL;
        ret = Searcher->Search(_bstr_t("IsInstalled = 0 and Type = 'Software'"), &SearchResult);
        
        if (FAILED(ret))
        {
            Searcher->Release();
            Session->Release();
            //return -1;
            throw -4;
        }
        
        IUpdateCollection *Collection;
        ret = SearchResult->get_Updates(&Collection);
        if (FAILED(ret) || Collection == NULL)
        {
            Log("//////////////////////////////////////////////////////////////////////////");
            Log("GetSystemDefects():failed to call ISearchResult::Updates!");
            Log("//////////////////////////////////////////////////////////////////////////");
            //return 0;
            throw -5;
        }
        
        long Colnum;
        long  i = 0;
        long  j = 0;

        Collection->get_Count(&Colnum);
        
        if (Colnum < 0)
        {
            //system_defects = NULL;
            //printf("There are no appliable update./n");
        }
        else
        {
            //printf("Total update count:%d/n", Colnum);
        }

        for (i = 0; i < Colnum; i++)
        {
            IUpdate   *Update;
            ret = Collection->get_Item(i, &Update);
            if (FAILED(ret) || Update == NULL)
            {
                Log("Collection->get_Item(i, &Update)");
                throw -6;
            }
        
            BSTR Title = NULL;
            ret = Update->get_Title(&Title);

            //安全等级
            //Critical Important Moderate Low
            BSTR SecLevel = NULL;
            ret = Update->get_MsrcSeverity(&SecLevel);

            //Download Url
            //
            IUpdateDownloadContentCollection *DownloadUrlCol = NULL;

            //获取安全公告号
            IStringCollection *SBID = NULL;//安全公告号
            ret = Update->get_SecurityBulletinIDs(&SBID);
            BSTR SB = NULL;
            if (SUCCEEDED(ret) && SBID != NULL)
            {
                long SBCount;
                ret = SBID->get_Count(&SBCount);
                SBID->get_Item(0, &SB);
            }
            
            
            //获取补丁号
            IStringCollection *KBArticles = NULL;
            ret = Update->get_KBArticleIDs(&KBArticles);
            BSTR KB;
            if (SUCCEEDED(ret) && KBArticles != NULL)
            {
                long KbCount;
                ret = KBArticles->get_Count(&KbCount);
                KBArticles->get_Item(0, &KB);
            }
    

            //Description
            //
            BSTR Description = NULL;
            ret = Update->get_Description(&Description);

            //
            //ReleaseNote
            BSTR ReleaseNote = NULL;
            ret = Update->get_ReleaseNotes(&ReleaseNote);
            
            //
            //More information
            IStringCollection *MoreInfo;
            ret = Update->get_MoreInfoUrls(&MoreInfo);
            BSTR MoreInfoUrl;
            if (SUCCEEDED(ret) && MoreInfo != NULL)
            {
                long MICount;
                ret = MoreInfo->get_Count(&MICount);
                MoreInfo->get_Item(0, &MoreInfoUrl);    
            }

            // 有安全公告号,才显示
            if (SB != NULL)
            {
                
                wchar_t buffer[max_size];
                memset(buffer, '/0', max_size);

                //first record
                if (flag)    
                {
                    //Title
                    char *Ttemp = _bstr_t(Title);
                    //sprintf(buffer, "%s", temp);
                    memcpy(system_defects->defects_name, Ttemp, strlen(Ttemp));
                    
                    //Security Bulletin
                    memset(buffer, '/0', max_size);
                    swprintf(buffer, L"%s", SB);
                //    wprintf(L"%s/n", buffer);
                    memcpy(system_defects->defects_id, buffer, avg_size);
                    
                    //Security Level
                    memset(buffer, '/0', max_size);
                    swprintf(buffer, L"%s", SecLevel);
                //    wprintf(L"%s/n", buffer);
                    memcpy(system_defects->defects_level, buffer, avg_size);
                    
                    //Description
                    char *Dtemp = _bstr_t(Description);
                    memcpy(system_defects->defects_desc, Dtemp, strlen(Dtemp));
                    
                    //KB
                    memset(buffer, '/0', max_size);
                    swprintf(buffer, L"KB%s", KB);
                    //wprintf(L"%s/n", buffer);
                    memcpy(system_defects->patch_name, buffer, avg_size);

                    //MoreInforUrl
                    memset(buffer, '/0', max_size);
                    swprintf(buffer, L"%s", MoreInfoUrl);
                    //wprintf(L"%s/n", buffer);
                    memcpy(system_defects->MoreInfoUrl, buffer, avg_size);
                    
                    system_defects->next = NULL;
                    flag = 0;
                }
                else
                {
                    //...
                    //...
                }
            }
        
        }
        
        Session->Release();
        Searcher->Release();
        SearchResult->Release();

        CoUninitialize();
    }catch(int err)
    {
        res = err;    
        printf("Error:%d, ret = %x/n", res, ret);
    }

    Log("Get system defects successed.");
    return 1;
}

注:对BSTR的转换成char*的方法,网上的不太实用,没有成功,这里我使用_bstr_t类转换成功。

原创粉丝点击