简体   繁体   English

如何获得Windows安装的更新

[英]How to get Windows installed updates

I need to get all installed updates on Windows machine. 我需要在Windows机器上获得所有已安装的更新。 I tried WUA API, but the result is differs from what I see in Control Panel - Installed Updates. 我尝试过WUA API,但结果与我在“控制面板 - 已安装的更新”中看到的结果不同。 My code returns 321 update, while in control panel I see 508. Here is my code: 我的代码返回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;

Can anyone please explain how can I get all the updates? 任何人都可以解释我如何获得所有更新?

WUA API only lists updates installed through Windows Updates Services. WUA API仅列出通过Windows Updates Services安装的更新。

To list other kind of updates you may use Windows Installer API , specifically: 要列出其他类型的更新,您可以使用Windows Installer API ,具体如下:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取安装的 Windows SDK 版本? - How to get installed Windows SDK version? 如何获得C ++ Windows服务的安装目录? - How can I get the installed directory for a C++ Windows Service? 如何获取Windows 7中安装的主题列表(主题名称)? - How to get list of themes (Theme name) installed in Windows 7? 使用 wuapi 获取 Windows 系统更新 - Get Windows System updates using wuapi 如何在C ++的所有更新中区分重要的Windows更新 - How to differentiate critical windows updates among all updates in c++ 获取 Windows 中已安装的 Mini Filter 驱动程序列表 - Get a list of installed Mini Filter driver in windows 如何在Windows下运行通过MSYS安装的pkg-config(对于GTK + -3.0),并获取绝对路径 - How to run pkg-config (for GTK+-3.0) installed through MSYS under windows and get Absolute Path 在 Windows 10 上成功安装后,如何让 VSCode 找到 gmp.h? - How can I get VSCode to find gmp.h after it has been successfully installed on Windows 10? 如何从 windows 操作系统中下载但尚未安装的字体文件中获取 IDWriteFont object - How to get the IDWriteFont object from the font file downloaded in windows OS and not installed yet 如何使用非托管C ++获取Windows上已安装字体的列表? - How can I get a list of installed fonts on Windows, using unmanaged C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM