我想使用IUpdate5接口,但我找不到实例化它的方法。我目前正在使用IUpdate,它运行得很好,但我如何使用这些接口的版本,如IUpdateSession3、IUpdateSearcher3、IUupdate4、IUpdate5等。我尝试从IUpdate*直接转换为IUpdate5*,但我遇到了这样的错误。
//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");
}
它在if(auto-res=update->get_RebootRequired(&RebootRequired((行抛出内存错误。错误为"0xC0000005:读取位置0x00000008的访问冲突。">
你试过QueryInterface
吗?当然使用CComQIPtr<IUpdate5> update(ref);
会更好。
IUpdate2可以这样声明以调用其虚拟函数
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);