关于用WUA API获取系统已安装补丁

ai_at_china 2012-06-08 01:59:45
我用下面代码获取系统已安装补丁:
BOOL GetSystemDefects(struct defects *system_defects)
{
int res = NO_ERROR;
HRESULT ret;
int flag = 1;
struct defects *p;
try
{
IUpdateSession *Session = NULL;
ret = CoInitialize(NULL);
if (FAILED(ret))
{
Log("GetSystemDefects():Initializes the COM Failed.");
throw -1;
}

ret = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER,
IID_IUpdateSession , (LPVOID*)&Session);
if ((Session == NULL) || FAILED(ret))
{
//return -1;
throw -2;
}

IUpdateSearcher *Searcher = NULL;
ret = Session->CreateUpdateSearcher(&Searcher);

if (FAILED(ret) || (Searcher == NULL))
{
Session->Release();
//return -1;
throw -3;
}

Searcher->put_Online(VARIANT_FALSE); //离线查询
// Searcher->put_Online(VARIANT_TRUE); //在线查询
ISearchResult *SearchResult = NULL;
ret = Searcher->Search(_bstr_t("IsInstalled = 0 and Type = 'Software'"), &SearchResult);

if (FAILED(ret))
{
Searcher->Release();
Session->Release();
//return -1;
throw -4;
}

IUpdateCollection *Collection;
ret = SearchResult->get_Updates(&Collection);
if (FAILED(ret) || Collection == NULL)
{
Log("//////////////////////////////////////////////////////////////////////////");
Log("GetSystemDefects():failed to call ISearchResult::Updates!");
Log("//////////////////////////////////////////////////////////////////////////");
//return 0;
throw -5;
}

long Colnum;
long i = 0;
long j = 0;

Collection->get_Count(&Colnum);

if (Colnum < 0)
{
//system_defects = NULL;
//printf("There are no appliable update./n");
}
else
{
//printf("Total update count:%d/n", Colnum);
}

for (i = 0; i < Colnum; i++)
{
IUpdate *Update;
ret = Collection->get_Item(i, &Update);
if (FAILED(ret) || Update == NULL)
{
Log("Collection->get_Item(i, &Update)");
throw -6;
}

BSTR Title = NULL;
ret = Update->get_Title(&Title);

//安全等级
//Critical Important Moderate Low
BSTR SecLevel = NULL;
ret = Update->get_MsrcSeverity(&SecLevel);

//Download Url
//
IUpdateDownloadContentCollection *DownloadUrlCol = NULL;

//获取安全公告号
IStringCollection *SBID = NULL;//安全公告号
ret = Update->get_SecurityBulletinIDs(&SBID);
BSTR SB = NULL;
if (SUCCEEDED(ret) && SBID != NULL)
{
long SBCount;
ret = SBID->get_Count(&SBCount);
SBID->get_Item(0, &SB);
}


//获取补丁号
IStringCollection *KBArticles = NULL;
ret = Update->get_KBArticleIDs(&KBArticles);
BSTR KB;
if (SUCCEEDED(ret) && KBArticles != NULL)
{
long KbCount;
ret = KBArticles->get_Count(&KbCount);
KBArticles->get_Item(0, &KB);
}


//Description
//
BSTR Description = NULL;
ret = Update->get_Description(&Description);

//
//ReleaseNote
BSTR ReleaseNote = NULL;
ret = Update->get_ReleaseNotes(&ReleaseNote);

//
//More information
IStringCollection *MoreInfo;
ret = Update->get_MoreInfoUrls(&MoreInfo);
BSTR MoreInfoUrl;
if (SUCCEEDED(ret) && MoreInfo != NULL)
{
long MICount;
ret = MoreInfo->get_Count(&MICount);
MoreInfo->get_Item(0, &MoreInfoUrl);
}

// 有安全公告号,才显示
if (SB != NULL)
{

wchar_t buffer[max_size];
memset(buffer, '/0', max_size);

//first record
if (flag)
{
//Title
char *Ttemp = _bstr_t(Title);
//sprintf(buffer, "%s", temp);
memcpy(system_defects->defects_name, Ttemp, strlen(Ttemp));

//Security Bulletin
memset(buffer, '/0', max_size);
swprintf(buffer, L"%s", SB);
// wprintf(L"%s/n", buffer);
memcpy(system_defects->defects_id, buffer, avg_size);

//Security Level
memset(buffer, '/0', max_size);
swprintf(buffer, L"%s", SecLevel);
// wprintf(L"%s/n", buffer);
memcpy(system_defects->defects_level, buffer, avg_size);

//Description
char *Dtemp = _bstr_t(Description);
memcpy(system_defects->defects_desc, Dtemp, strlen(Dtemp));

//KB
memset(buffer, '/0', max_size);
swprintf(buffer, L"KB%s", KB);
//wprintf(L"%s/n", buffer);
memcpy(system_defects->patch_name, buffer, avg_size);

//MoreInforUrl
memset(buffer, '/0', max_size);
swprintf(buffer, L"%s", MoreInfoUrl);
//wprintf(L"%s/n", buffer);
memcpy(system_defects->MoreInfoUrl, buffer, avg_size);

system_defects->next = NULL;
flag = 0;
}
else
{
//...
//...
}
}

}

Session->Release();
Searcher->Release();
SearchResult->Release();

CoUninitialize();
}catch(int err)
{
res = err;
printf("Error:%d, ret = %x/n", res, ret);
}

Log("Get system defects successed.");
return 1;
}

在win 7 64 位系统下,代码执行没问他,但扫描到的补丁数目一直是0,可以肯定的是系统已经打了一些补丁了,各位帮忙看看是哪的问他,是不是wsus服务什么方面的?如果是,怎么配置啊?先多谢了!
...全文
391 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sistwl 2012-07-16
  • 打赏
  • 举报
回复
查询条件是IsInstalled = 0 and Type = 'Software',你这查找的是没有安装的补丁,如果要查找已安装的补丁,改为IsInstalled = 1
ai_at_china 2012-06-12
  • 打赏
  • 举报
回复
怎么没人帮忙啊,求高手!
ai_at_china 2012-06-08
  • 打赏
  • 举报
回复
自己先顶,大家帮忙啊,多谢啦!

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧