1

次のコード スニペットがあります。

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 インターフェイスへのポインターを取得する方法、

4

1 に答える 1

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--;
}
于 2013-03-07T10:50:56.363 に答える