-
WUA API 参考代码 - [program reference]
2009-04-25
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://dy0ng.blogbus.com/logs/38471350.html
接上一篇,日,blogbus发文章还有长度限制,逼得我分成两篇文章。
参考代码:
CODE:
//
//(2) 获取操作系统补丁信息(部分代码已经舍去)
// 使用Windows Update Agent API实现
// 使用离线检测的形式
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;
}注:对BSTR的转换成char*的方法,网上的不太实用,没有成功,这里我使用_bstr_t类转换成功。
收藏到:Del.icio.us
评论
我的邮箱:lsmart_ab@sina.com
我的QQ:44100164