WUA API学习笔记 (二)
来源:互联网 发布:日货清单 国人必知 编辑:程序博客网 时间:2024/06/05 09:52
参考代码:
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类转换成功。
- WUA API学习笔记 (二)
- WUA API学习笔记
- WUA API学习笔记
- WUA API学习笔记
- WUA API国内外相关参考资料
- Windows API 多线程-学习笔记(二)
- JAVA API学习笔记二 java.lang.*
- Activiti学习笔记二 核心API
- 百度地图API学习笔记(二)
- tensorflow学习笔记(二):机器学习必备API
- [WUA APIs]Using the Windows Update Agent API
- Hadoop学习笔记二(新旧版本API的区别)
- 黑马学习笔记_常用API总结(二)
- JAVA SE 8 学习笔记(二)Stream API
- geometry-api-java 学习笔记(二)点 Point
- Zookeeper学习笔记(二)通过Java API调用Zookeeper
- web api 学习二
- WINSOCK API 学习笔记
- 几个数据库案例分析
- java编码
- 项目测试经验交流
- MVC WebCast学习笔记
- WUA API学习笔记
- WUA API学习笔记 (二)
- 解决因改变oracle数据库的IP地址引起的“ORA-12541:TNS:无监听程序”
- Ubuntu中增加程序快捷方式的方法
- PHP程序员常用面试题
- 项目进展
- 共享内存区一定要使用偏移量,而非指针!
- MS SQL SQL TIPS
- Java未解决的问题1
- 开始研究遥感开源软件otb