Reputation: 1282
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."
Upvotes: 1
Views: 299
Reputation: 1
IUpdate2 can be declared like this to call its virtual function
IUpdate* pUpdate = nullptr;
hr = pUpdateCollection->get_Item(i, &pUpdate);
IUpdate2* pUpdate2 = nullptr;
hr = pUpdate->QueryInterface(IID_IUpdate2, reinterpret_cast<LPVOID*>(&pUpdate2));
BSTR title = nullptr;
hr = pUpdate2->get_Title(&title);
Upvotes: 0
Reputation: 519
Have you ever tried a QueryInterface
? Would be much better with CComQIPtr<IUpdate5> update(ref);
of course.
Upvotes: 1