如何从wuapi.h使用IUpdate5

时间:2018-07-11 10:23:23

标签: c++ wuapi

我想使用IUpdate5接口,但是无论如何我都无法实例化它。我当前正在使用IUpdate,它可以正常工作,但是如何使用这些接口的版本控制版本,例如IUpdateSession3,IUpdateSearcher3,IUpdate4,IUpdate5等。我尝试了从IUpdate *到IUpdate5 *的直接转换,但出现了这样的错误。 / p>

    //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。”

1 个答案:

答案 0 :(得分:0)

您曾经尝试过QueryInterface吗?当然,CComQIPtr<IUpdate5> update(ref);会更好。