无法使用C ++中的WUA API检索Windows Update信息 [英] Unable to retrieve Windows Update information with WUA API in C++

查看:227
本文介绍了无法使用C ++中的WUA API检索Windows Update信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



在探索了各种方法之后,我发现Windows Update Agent API是从任何Windows操作系统收集Windows更新信息的更好方法发布Windows 2000 SP3。



我用C ++创建了一个win32控制台应用程序,用下面提到的方法收集windows更新信息。



1.初始化COM。

2.获取Windows Update会话。

3.创建更新搜索器(使用IUpdateSearcher界面)。

4.使用搜索条件IsInstalled = 1或IsHidden = 1或IsPresent = 1执行搜索。在这里,我得到软件和驱动程序类型的结果。

5.浏览结果。

6.分配给对象的清理内存和未初始化COM。



以下是我在使用管理员优先级测试控制台应用程序时观察到的结果:

1.在Windows XP 64位中,WUA API与WMIC QFE GET相比,返回的结果较少

2.在Windows Vista中,WUA API未返回任何结果。

3.在Windows 7,2008中, WUA API返回的结果比WMIC QFE GET更多。



与使用WMI查询Win32_QuickFixEngineering类相比,我采用了使用WUA API的方法,因为后者提供使用基于组件的服务更新的更新条目。



您能否回答以下问题:

1.是否有任何错误在我的方法中检索更新信息?

2.请您指出一些关于使用WUA API的好文章/信息?

3.在编程时是否需要注意任何特定的设置操作系统风味?

4.是否有分别针对32位和64位机器的任何特定构建过程?

5.应该强制启用Windows Update服务运行WUA API代码?



我复制了我正在使用的示例代码。编写此代码是为了收集已安装更新的KB编号:



  #include   <   Windows.h  >  
#include < iostream >
#include < atlbase.h >
#include < Wuapi.h >
#include < span class =code-preprocessor> < wuerror.h >
#include < 列表 >
#include < fstream >
#include atlbase.h
#include atlstr.h
#include comutil.h
# include < MsXml.h >

使用 namespace std;


int main()
{
尝试
{
HRESULT hr;
hr = CoInitialize(NULL);

IUpdateSession * iUpdate;
IUpdateSearcher *搜索者;
ISearchResult *结果;
BSTR标准= SysAllocString(L IsInstalled = 1或IsHidden = 1或IsPresent = 1);

hr = CoCreateInstance(CLSID_UpdateSession,NULL,CLSCTX_INPROC_SERVER,IID_IUpdateSession,(LPVOID *)& iUpdate);
hr = iUpdate-> CreateUpdateSearcher(& searcher);

wcout<< L 正在搜索更新......<< endl;
hr =搜索者 - >搜索(标准,&结果);
SysFreeString(标准);

switch (hr)
{
case S_OK:
wcout<< L 机器上适用项目的列表:< ;< ENDL;
break ;
case WU_E_LEGACYSERVER:
wcout<< L 未启用服务器选择<< endl;
return 0 ;
case WU_E_INVALID_CRITERIA:
wcout<< L 无效的搜索条件<< endl;
return 0 ;
}

IUpdateCollection * updateList;
IUpdate * updateItem;
LONG updateSize;
LONG totalKB = 0 ;
results-> get_Updates(& updateList);
updateList-> get_Count(& updateSize);

if (updateSize == 0
{
wcout<< L 未找到更新<< endl;
}

ofstream outputFile;
outputFile.open( C:\\test.txt,ios: :出);

for (LONG i = 0 ; i< updateSize; i ++)
{
IStringCollection * KBCollection;
BSTR updateName;
LONG KBCount;
updateList-> get_Item(i,& updateItem);


updateList-> get_Item(i,& updateItem);
updateItem-> get_Title(& updateName);
USES_CONVERSION;
outputFile<< W2A(CString(updateName))<< ---;
updateItem-> get_KBArticleIDs(& KBCollection);
KBCollection-> get_Count(& KBCount);
for int i = 0 ; i< KBCount; i ++)
{
BSTR KBValue;
totalKB + = 1 ;
KBCollection-> get_Item(i,& KBValue);
USES_CONVERSION;
outputFile<< W2A(CString( KB))<< W2A(CString(KBValue))<< ENDL;
}

IUpdateCollection * updtCollection;
LONG updtBundledCount;

// 检索捆绑的更新
outputFile<< ; W2A(CString( \t捆绑更新:));
updateItem-> get_BundledUpdates(& updtCollection);
hr = updtCollection-> get_Count(& updtBundledCount);
if ((updtBundledCount> 0)&&(hr == S_OK))
{
// wcout<< L捆绑更新<<(updtBundledCount)<< endl;
for (LONG j = 0 ; j< updtBundledCount; j ++ )
{
IUpdate * bundledUpdateItem;

updtCollection-> get_Item(j,& bundledUpdateItem);

bundledUpdateItem-> get_Title(& updateName);
USES_CONVERSION;
outputFile<< W2A(CString( \t))<< W2A(CString(updateName))<< - ;

updateItem-> get_KBArticleIDs(& KBCollection);
KBCollection-> get_Count(& KBCount);
for int i = 0 ; i< KBCount; i ++)
{
BSTR KBValue;
totalKB + = 1 ;
KBCollection-> get_Item(i,& KBValue);
outputFile<< W2A(CString( KB))<< W2A(CString(KBValue))<< ENDL;
}
// wcout<< L<< j + 1< < - << bundledName<< endl;
}

}
}
wcout<< 总KB:<<总KB << ENDL;
outputFile.close();
:: CoUninitialize();
}
catch const std :: exception& ex)
{
cout<< ex.what();
:: CoUninitialize();
}
return 0 ;
}

解决方案

您好,



以下网址可能会有所帮助。

http ://msdn.microsoft.com/en-us/library/windows/desktop/aa386907(v = vs.85).aspx [ ^ ]



#我还创建了一个软件,显示和导出Windows Update的历史记录,

并认为WUA API不够用。

所以,我使用WUA,WMI和WSH的组合。

主要使用WSH,WUA和WMI补充。



#抱歉我的英语不好。

Hi Everyone,

After exploring the various ways, I have found "Windows Update Agent API" is the better way to collect windows update information from any Windows OS post Windows 2000 SP3.

I have created a win32 console application in C++ to collect the windows update info with below mentioned approach.

1.Initialize COM.
2.Get Handle to Windows Update Session.
3.Create an Update Searcher (Used IUpdateSearcher interface).
4.Perform search with the search criteria "IsInstalled=1 or IsHidden=1 or IsPresent=1". Here I am getting the results of both type "Software" and "Driver".
5.Iterate through results.
6.Cleanup memory allocated to objects and Uninitialize COM.

Following are the results I have observed while testing the console application with Administrator previleges:
1.In Windows XP 64 bit, WUA API is returning less results when compared to "WMIC QFE GET"
2.In Windows Vista, WUA API is not returning any results.
3.In Windows 7, 2008, WUA API is returning more results than WMIC QFE GET.

I have taken the approach of using WUA API when compared to querying the "Win32_QuickFixEngineering" class with WMI because the latter gives the update entries that are updated with Component Based Servicing.

Could you please answer the following queries:
1.Are there any errors in my approach to retrieve the update information?
2.Could you please point to some good articles/information about using WUA API?
3.Are there any specific settings that need to be taken care in programming based on OS flavour?
4.Is there any specific build process with respect to 32 bit and 64 bit machines separately?
5.Should "Windows Update" service be mandatory enabled to run the WUA API code?

I have copied the sample code I am using. This code is written to collect the KB Numbers of installed updates:

#include <Windows.h>
#include <iostream>
#include <atlbase.h>
#include <Wuapi.h>
#include <wuerror.h>
#include <list>
#include <fstream>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
#include <MsXml.h>

using namespace std;


int main()
{
try
{
    HRESULT hr;
    hr = CoInitialize(NULL);

    IUpdateSession* iUpdate;
    IUpdateSearcher* searcher;
    ISearchResult* results;
    BSTR criteria = SysAllocString(L"IsInstalled=1 or IsHidden=1 or IsPresent=1");

    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;
    IUpdate *updateItem;
    LONG updateSize;
    LONG totalKB=0;
    results->get_Updates(&updateList);
    updateList->get_Count(&updateSize);

    if (updateSize == 0)
    {
        wcout << L"No updates found"<<endl;
    }

	ofstream outputFile;
	outputFile.open("C:\\test.txt",ios::out);

    for (LONG i = 0; i < updateSize; i++)
    {
        IStringCollection *KBCollection;
		BSTR updateName;
        LONG KBCount;
        updateList->get_Item(i,&updateItem);


		updateList->get_Item(i, &updateItem);
		updateItem->get_Title(&updateName);
		USES_CONVERSION;
		outputFile << W2A(CString(updateName)) << " --- ";
		updateItem->get_KBArticleIDs(&KBCollection);
		KBCollection->get_Count(&KBCount);
		for(int i=0;i<KBCount;i++)
		{
			BSTR KBValue;
			totalKB += 1;
			KBCollection->get_Item(i,&KBValue);
			USES_CONVERSION;
			outputFile <<  W2A(CString("KB")) << W2A(CString(KBValue)) << endl;
		}

        IUpdateCollection *updtCollection;
        LONG updtBundledCount;        
		
        //Retrieve the bundled updates
		outputFile << W2A(CString("\t Bundled Updates : "));
        updateItem->get_BundledUpdates(&updtCollection);
        hr = updtCollection->get_Count(&updtBundledCount);
        if ((updtBundledCount>0) && (hr ==S_OK))
        {
            //wcout << L"Bundled Updates " <<(updtBundledCount) << endl;
            for(LONG j=0;j<updtBundledCount;j++)
            {
                IUpdate *bundledUpdateItem;
                
                updtCollection->get_Item(j,&bundledUpdateItem);   
				
				bundledUpdateItem->get_Title(&updateName);
				USES_CONVERSION;
				outputFile <<  W2A(CString("\t")) << W2A(CString(updateName)) << " - ";
                
				updateItem->get_KBArticleIDs(&KBCollection);
				KBCollection->get_Count(&KBCount);
				for(int i=0;i<KBCount;i++)
				{
					BSTR KBValue;
					totalKB += 1;
					KBCollection->get_Item(i,&KBValue);
					outputFile <<  W2A(CString("KB")) << W2A(CString(KBValue)) << endl;
				}
                //wcout<<L"    "<<j+1<<" - "<<bundledName<<endl;
            }

        }
    }
	wcout << "Total KBs : " << totalKB << endl;
	outputFile.close();
    ::CoUninitialize();
	}
	catch( const std::exception & ex )
	{
		cout << ex.what();
		::CoUninitialize();
	}
    return 0;
}

解决方案

Hi,

The below URL may be helpful.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa386907(v=vs.85).aspx[^]

# I have also created a softwaret that shows and exports the history of Windows Update,
and think that WUA API is not sufficient.
So, I use the combination WUA, WMI, and WSH.
Using WSH mainly, WUA and WMI supplementary.

# sorry for my poor English.


这篇关于无法使用C ++中的WUA API检索Windows Update信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆