How to use IUpdate5 from wuapi.h

0

I want to use IUpdate5 interface but I could not find anyway to instantiate it. I am currently using IUpdate, it works flawlessly, but how can I use versioned versions of those interfaces such as IUpdateSession3, IUpdateSearcher3, IUpdate4, IUpdate5 etc. I tried direct cast from IUpdate* to IUpdate5* but i am getting error like this.

    //Initialize com components
CoInitialize(NULL);
CComQIPtr<IUpdateSession3> session;
if (auto res = session.CoCreateInstance(CLSID_UpdateSession)) {
    qDebug() << "Failed " << res;
}

IUpdateSearcher* casted;
if (session->CreateUpdateSearcher(&casted)) {
    qDebug() << "CreateUpdateSearcher failed";
}
IUpdateSearcher3* searcher = (IUpdateSearcher3*)casted;


CComQIPtr<ISearchResult> result;
CComBSTR criteria = "IsInstalled=1 and Type='Software'";
if (auto res = searcher->Search(criteria, &result)) {
    qDebug() << "Error " << res;
}

CComQIPtr<IUpdateCollection> updates;
if (auto res = result->get_Updates(&updates)) {
    qDebug() << "get_Updates fail " << res;
}

LONG count;
if (auto res = updates->get_Count(&count)) {
    qDebug() << "Updates count " << res;
}

CoCreateInstance(CLSID_UpdateSearcher, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSearcher3, (LPVOID*)&searcher);

for (auto i = 0; i < count; ++i) {

    IUpdate* ref;
    if (auto res = updates->get_Item(i, &ref)) {
        qDebug() << "get_Item error " << res;
    }
    IUpdate5* update = (IUpdate5*)ref;

    VARIANT_BOOL rebootRequired;
    if (auto res = update->get_RebootRequired(&rebootRequired)) {
        qDebug() << "Reboot required error" << res;
    }

    qDebug() << "Reboot required is " << (rebootRequired == VARIANT_TRUE ? "yes" : "no");

}

It throws a memory error at if(auto res = update->get_RebootRequired(&rebootRequired)) line. The error is "0xC0000005: Access violation reading location 0x00000008."

c++
wuapi
asked on Stack Overflow Jul 11, 2018 by arnes

1 Answer

0

Have you ever tried a QueryInterface? Would be much better with CComQIPtr<IUpdate5> update(ref); of course.

answered on Stack Overflow Jul 11, 2018 by Dmitrii

User contributions licensed under CC BY-SA 3.0