获取指向IUpdate2接口的指针

时间:2013-03-07 09:06:54

标签: c++ com window windows-update

我有以下代码段,

IUpdateSession *iUpdate;
IUpdateSearcher *updateSearcher;
ISearchResult* pISearchResults;
IUpdateCollection* pIUpdateCollection;
IStringCollection *pIStrCollCVEs;
IUpdate2 *pIUpdate;
long lUpdateCount;

...

CoCreateInstance(
            CLSID_UpdateSession,
            NULL,
            CLSCTX_INPROC_SERVER,
            IID_IUpdateSession,
            (LPVOID*)&iUpdate
            );

iUpdate->CreateUpdateSearcher(&updateSearcher);

printf("\n Searching updates");

updateSearcher->Search(_bstr_t(_T("IsInstalled = 0")), &pISearchResults);
printf("\n Following updates found..\n");

pISearchResults->get_Updates(&pIUpdateCollection);
pIUpdateCollection->get_Count(&lUpdateCount);

LONG lCount;
BSTR buff;
while (0 != lUpdateCount)
{
    pIUpdateCollection->get_Item(lUpdateCount, &pIUpdate);

    pIUpdate->get_CveIDs(&pIStrCollCVEs);

    pIStrCollCVEs->get_Count(&lCount);

    pIUpdate->get_Title(&buff);
    printf("TITLE : %s \n", buff);
    while(0 != lCount)
    {
        pIStrCollCVEs ->get_Item(lCount, &buff);
        _bstr_t b(buff);

        printf("CVEID = %s \n", buff);

        lCount --;
    }

    printf("\n");
    lUpdateCount --;
}


::CoUninitialize();
getchar();

错误: 错误C2664:'IUpdateCollection :: get_Item':无法将参数2从'IUpdate2 * *'转换为'IUpdate * *'

@ Line43

如何获取指向IUpdate2接口的指针

1 个答案:

答案 0 :(得分:0)

您的get_Item()成员需要IUpdate接口指针;不是IUpdate2接口指针。

注意:此代码绝对是谜语,包含错误,错误做法和内存泄漏。他们:

  • 从未发布的接口指针
  • BSTR永远不会被释放。
  • 从未检查的HRESULT。
  • 无效索引到从零开始的集合

仅举几例。无论如何以下内容应解决您的接口不匹配问题。剩下的这个动物园我留给你了:

while (0 != lUpdateCount)
{
    IUpdate* pIUpd = NULL;
    HRESULT hr = pIUpdateCollection->get_Item(lUpdateCount, &pIUpd);
    if (SUCCEEDED(hr) && pIUpd)
    {
        hr = pIUpd->QueryInterface(__uuidof(pIUpdate), (LPVOID*)&pIUpdate);
        pIUpd->Release();

        if (SUCCEEDED(hr) && pIUpdate != NULL)
        {
            pIUpdate->get_CveIDs(&pIStrCollCVEs);
            pIStrCollCVEs->get_Count(&lCount);

            pIUpdate->get_Title(&buff);
            printf("TITLE : %s \n", buff);
            while(0 != lCount)
            {
                pIStrCollCVEs ->get_Item(lCount, &buff);
                _bstr_t b(buff, false);
                printf("CVEID = %s \n", buff);
                lCount --;
            }
        }
    }

    printf("\n");
    lUpdateCount--;
}