我可能只是失明了,但我在这里看不到任何错误(我现在已经看了好几天了......)
我正在尝试使用Visual Studio中的以下代码从Windows Update界面获取修补程序优先级(严重性):
#include "stdafx.h"
#include <wuapi.h>
#include <iostream>
#include <ATLComTime.h>
#include <wuerror.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
hr = CoInitialize(NULL);
IUpdateSession* iUpdate;
IUpdateSearcher* searcher;
ISearchResult* results;
BSTR criteria = SysAllocString(L"IsInstalled=0");
hr = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID*)&iUpdate);
hr = iUpdate->CreateUpdateSearcher(&searcher);
wcout << L"Searching for updates ..."<<endl;
hr = searcher->Search(criteria, &results);
SysFreeString(criteria);
switch(hr)
{
case S_OK:
wcout<<L"List of applicable items on the machine:"<<endl;
break;
case WU_E_LEGACYSERVER:
wcout<<L"No server selection enabled"<<endl;
return 0;
case WU_E_INVALID_CRITERIA:
wcout<<L"Invalid search criteria"<<endl;
return 0;
}
IUpdateCollection *updateList;
IUpdateCollection *bundledUpdates;
IUpdate *updateItem;
IUpdate *bundledUpdateItem;
LONG updateSize;
LONG bundledUpdateSize;
BSTR updateName;
BSTR severity;
results->get_Updates(&updateList);
updateList->get_Count(&updateSize);
if (updateSize == 0)
{
wcout << L"No updates found"<<endl;
}
for (LONG i = 0; i < updateSize; i++)
{
updateList->get_Item(i,&updateItem);
updateItem->get_Title(&updateName);
severity = NULL;
updateItem->get_MsrcSeverity(&severity);
if (severity != NULL)
{
wcout << L"update severity: " << severity << endl;
}
wcout<<i+1<<" - " << updateName << endl;
// bundled updates
updateItem->get_BundledUpdates(&bundledUpdates);
bundledUpdates->get_Count(&bundledUpdateSize);
if (bundledUpdateSize != 0)
{
// iterate through bundled updates
for (LONG ii = 0; ii < bundledUpdateSize; ii++)
{
bundledUpdates->get_Item(ii, &bundledUpdateItem);
severity = NULL;
bundledUpdateItem->get_MsrcSeverity(&severity);
if (severity != NULL)
{
wcout << L" bundled update severity: " << severity << endl;
}
}
}
}
::CoUninitialize();
wcin.get();
return 0;
}
所以这就是问题所在:updateItem->get_MsrcSeverity(&severity);
没有返回任何内容。如果我用HRESULT
捕获结果代码,它总是返回S_OK
。
链接到MSDN IUpdate MsrcSeverity:http://msdn.microsoft.com/en-us/library/windows/desktop/aa386906(v=vs.85).aspx
你能看到我明显做错了或者get_MsrcSeverity
功能目前是否已被破坏?
@EDIT:更改代码以按照建议迭代“BundledUpdates”。
@ EDIT2:代码现在输出updateItem
和bundledUpdatesItem
的严重性值,但它总是NULL
。
我也知道有一个重要的更新等待我的电脑 - 关于控制面板中的Windows Update。它是KB2858725。
@ EDIT3:好的,事实证明,KB2858725没有安全更新,因此没有Microsoft的严重等级。但是,Microsoft Windows Update现在如何将更新分类为“重要”和“可选”,就像您可以在控制面板的更新中看到它一样?
感谢您的任何提示! // Markus
答案 0 :(得分:3)
我几个小时以来一直在努力解决同样的问题...我终于想通了微软似乎只在某些更新上设置了MsrcSeverity值。对于一般的Windows更新,它通常为null。对于大多数安全更新,它设置为以下之一:
似乎从未使用过“未指定”值,尽管它已在MSDN(http://msdn.microsoft.com/en-us/library/microsoft.updateservices.administration.msrcseverity(v=vs.85).aspx)中记录。
我写了一个小的C#程序来列出所有可用的更新及其报告的严重性。它需要参考WUApiLib:
using System;
using WUApiLib;
namespace EnumerateUpdates
{
class Program
{
static void Main(string[] args)
{
var session = new UpdateSession();
var searcher = session.CreateUpdateSearcher();
searcher.Online = false;
// Find all updates that have not yet been installed
var result = searcher.Search("IsInstalled=0 And IsHidden=0");
foreach (dynamic update in result.Updates)
{
Console.WriteLine(update.Title + ": " + update.Description);
Console.WriteLine("Severity is " + update.MsrcSeverity);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
希望这有助于某人!
答案 1 :(得分:1)
在列表中查找捆绑的更新。捆绑更新没有this页面上描述的某些属性或方法。
说明
如果BundledUpdates属性包含一个IUpdateCollection 更新的属性和方法可能只在 捆绑更新,例如,DownloadContents或CopyFromCache。
您正在处理的更新是捆绑更新。从此捆绑包中提取单个更新,它将具有您要查找的属性。
IUpdate
接口有一个get_BundledUpdates
方法,如果此集合的大小大于0,则会获得IUpdateCollection
,这是捆绑更新。