首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-09-08 
关于用WUA API获取系统已安装补丁我用下面代码获取系统已安装补丁:BOOL GetSystemDefects(struct defects*

关于用WUA 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;
}

在win 7 64 位系统下,代码执行没问他,但扫描到的补丁数目一直是0,可以肯定的是系统已经打了一些补丁了,各位帮忙看看是哪的问他,是不是wsus服务什么方面的?如果是,怎么配置啊?先多谢了!

[解决办法]
查询条件是IsInstalled = 0 and Type = 'Software',你这查找的是没有安装的补丁,如果要查找已安装的补丁,改为IsInstalled = 1

热点排行