Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何获取Windows安装更新_C++_Windows_Winapi_Windows Update - Fatal编程技术网

C++ 如何获取Windows安装更新

C++ 如何获取Windows安装更新,c++,windows,winapi,windows-update,C++,Windows,Winapi,Windows Update,我需要在Windows计算机上获取所有已安装的更新。我尝试了WUA API,但结果与我在“控制面板安装的更新”中看到的不同。我的代码返回321更新,而在控制面板中我看到508。这是我的密码: IUpdateSearcher* updateSearcher = NULL; IUpdateSession* updateSession = NULL; IUpdateCollection* updateList = NULL; ISearchResult* results = NULL; IUpdate

我需要在Windows计算机上获取所有已安装的更新。我尝试了WUA API,但结果与我在“控制面板安装的更新”中看到的不同。我的代码返回321更新,而在控制面板中我看到508。这是我的密码:

IUpdateSearcher* updateSearcher = NULL;
IUpdateSession* updateSession = NULL;
IUpdateCollection* updateList = NULL;
ISearchResult* results = NULL;
IUpdate* updateItem = NULL;
BSTR criteria = NULL;
LONG updateSize = 0;
HRESULT hr;

if ((hr = CoInitialize(NULL)) != S_OK)
{
    return -1;
}
if ((hr = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID*)&updateSession)) != S_OK)
{
    return -1;
}
if ((hr = updateSession->CreateUpdateSearcher(&updateSearcher)) != S_OK)
{
    return -1;
}
if ((hr = updateSearcher->put_ServerSelection(ssWindowsUpdate)) != S_OK)
{
    return -1;
}

criteria = SysAllocString(L"IsInstalled=1 or IsInstalled=0 or IsHidden=1 or IsPresent=1");
if ((hr = updateSearcher->Search(criteria, &results)) == S_OK)
{
    std::wcout << L"[*]Successfully completed search for updates on this host" << std::endl;
}
else
{
    std::wcout << L"[-]Failed to search for updates" << std::endl;
    return -1;
}

results->get_Updates(&updateList);
updateList->get_Count(&updateSize);
if (updateSize == 0)
{
    std::wcout << L"[-]No updates available for this host" << std::endl;
    CoUninitialize();
    return 0;
}
std::set<std::wstring> KBs;
for (LONG i = 0; i < updateSize; i++)
{
    IStringCollection *KBCollection;
    LONG KBsSize = 0;
    updateList->get_Item(i, &updateItem);
    updateItem->get_KBArticleIDs(&KBCollection);
    KBCollection->get_Count(&KBsSize);

    for (LONG i = 0; i < KBsSize; i++)
    {
        BSTR KBValue;
        KBCollection->get_Item(i, &KBValue);
        std::wstring ws(KBValue, SysStringLen(KBValue));
        KBs.insert(ws);
    }
}


if ((hr = updateSearcher->put_ServerSelection(ssOthers)) != S_OK)
{
    return -1;
}
BSTR serviceID = SysAllocString(L"7971f918-a847-4430-9279-4a52d1efe18d");

if ((hr = updateSearcher->put_ServiceID(serviceID)) != S_OK)
{
    return -1;
}

hr = updateSearcher->Search(criteria, &results);
if ((hr = updateSearcher->Search(criteria, &results)) == S_OK)
{
    std::wcout << L"[*]Successfully completed search for updates on this host" << std::endl;
}
else
{
    std::wcout << L"[-]Failed to search for updates" << std::endl;
}

results->get_Updates(&updateList);
updateList->get_Count(&updateSize);
if (updateSize == 0)
{
    std::wcout << L"[-]No updates available for this host" << std::endl;
    CoUninitialize();
    return 0;
}
for (LONG i = 0; i < updateSize; i++)
{
    IStringCollection *KBCollection;
    LONG KBsSize = 0;
    updateList->get_Item(i, &updateItem);
    updateItem->get_KBArticleIDs(&KBCollection);
    KBCollection->get_Count(&KBsSize);

    for (LONG i = 0; i < KBsSize; i++)
    {
        BSTR KBValue;
        KBCollection->get_Item(i, &KBValue);
        KBs.insert(KBValue);
    }
}

SysFreeString(criteria);
SysFreeString(serviceID);

CoUninitialize();

std::wcout << KBs.size() << std::endl;
IUpdateSearcher*updatesarcher=NULL;
IUpdateSession*updateSession=NULL;
IUpdateCollection*updateList=NULL;
ISearchResult*结果=NULL;
IUpdate*updateItem=NULL;
BSTR标准=空;
长updateSize=0;
HRESULT-hr;
如果((hr=CoInitialize(NULL))!=S_正常)
{
返回-1;
}
if((hr=CoCreateInstance(CLSID\u UpdateSession,NULL,CLSCTX\u INPROC\u SERVER,IID\u iuupdatesession,(LPVOID*)&UpdateSession))!=S\u OK)
{
返回-1;
}
如果((hr=updateSession->CreateUpdateSearcher(&updateSearcher))!=S\u确定)
{
返回-1;
}
如果((hr=updatesarcher->put_服务器选择(ssWindowsUpdate))!=S_确定)
{
返回-1;
}
标准=SysAllocString(L“IsInstalled=1或IsInstalled=0或IsHidden=1或IsPresent=1”);
如果((hr=updatesarcher->Search(条件和结果))==S\u确定)
{
std::wcout get_项(i和KBValue);
std::wstring ws(KBValue,SysStringLen(KBValue));
KBs.插入(ws);
}
}
如果((hr=updatesarcher->put_服务器选择(ssOthers))!=S_确定)
{
返回-1;
}
BSTR serviceID=SysAllocString(L“7971f918-a847-4430-9279-4a52d1efe18d”);
如果((hr=updatesarcher->put\u ServiceID(ServiceID))!=S\u确定)
{
返回-1;
}
hr=更新搜索器->搜索(条件和结果);
如果((hr=updatesarcher->Search(条件和结果))==S\u确定)
{
std::wcout get_项(i和KBValue);
KBs.插入(KBValue);
}
}
SysFreeString(标准);
SysFreeString(serviceID);
coninitialize();

WUA API仅列出通过Windows更新服务安装的更新

要列出您可能使用的其他类型的更新,具体如下:


谢谢。所以我的代码应该只返回控制面板的Microsoft Windows部分的更新-程序和功能-已安装的更新,对吗?@rudolfninja它不仅限于Windows。其他Microsoft产品和某些第三方组件(如驱动程序)的更新也可以通过Windows Update安装。谢谢。我抓到你了。还有一个问题。为什么我的输出不同于wmic qfe get hotfixid输出?WMI输出的列表与“控制面板-程序和功能-安装的更新”的“Microsoft Windows”部分中的列表完全相同。顺便说一句,您提到的功能有助于从“控制面板”获取其他更新,但不是Microsoft Windows部分中的更新。现在,我发现从该部分获取所有更新的唯一方法是WMI查询。我的密码有什么错误?@rudolfninja我不知道。可以在调用
updatesarcher->Search()
时尝试使用筛选条件。