源文件:
#include "stdafx.h"
#include "hotfix.h"
int _tmain(int argc, _TCHAR* argv[])
{
//WmiInit();
Hotfix h[200];
int counts=Gethotfix(h);
std::cout < < "Hotfix Begin:" <
头文件:
#ifndef _WIN32_DCOM
#define _WIN32_DCOM
#endif
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include "Comdef.h"
#pragma comment(lib,"Wbemuuid.lib")
HRESULT test;
struct Hotfix
{
_bstr_t csname;
_bstr_t Description;
_bstr_t FixComments;
_bstr_t HotFixID;
_bstr_t FixCaption;
_bstr_t InstallDate;
_bstr_t InstalledBy;
_bstr_t InstalledOn;
_bstr_t HotFixName;
_bstr_t ServicePackInEffect;
_bstr_t Fixstatus;
_bstr_t Lastpachtime;
} Myhotfix;
ULONG retcnt =1;
int Gethotfix(Hotfix hot[])
{
int i =0;
HRESULT hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
if ( FAILED( hr ) )
{
std::cerr < < "COM initialization failed" << std::endl;
return -1;
}
hr = CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL );
if ( FAILED( hr ) )
{
std::cerr << "Security initialization failed" << std::endl;
return -1;
}
int result = 0;
{
CComPtr< IWbemLocator > locator;
hr = CoCreateInstance( CLSID_WbemAdministrativeLocator, NULL,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, reinterpret_cast< void** >( &locator ) );
if ( FAILED( hr ) )
{
std::cerr < < "Instantiation of IWbemLocator failed" << std::endl;
return -1;
}
// 链接当前wmi服务
CComPtr< IWbemServices > service;
hr = locator->ConnectServer( L"root\\cimv2", NULL, NULL, NULL,
WBEM_FLAG_CONNECT_USE_MAX_WAIT,
NULL, NULL, &service );
if ( SUCCEEDED( hr ) )
{
CComPtr< IEnumWbemClassObject > enumerator;
// 查询数据库获得补丁信息
hr = service->ExecQuery( L"WQL", L"Select * from Win32_QuickFixEngineering",WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &enumerator );
while ( SUCCEEDED( hr ) && retcnt >0)
{
CComPtr< IWbemClassObject > pHotfix;
//枚举当前系统补丁信息
hr = enumerator->Next( WBEM_INFINITE, 1L, reinterpret_cast( &pHotfix), &retcnt );
if ( SUCCEEDED( hr ) && retcnt >0 )
{
_variant_t var_val;
test = pHotfix->Get( L"CSName", 0, &var_val, NULL, NULL );
if ( SUCCEEDED( test ) && var_val.vt !=VT_NULL )
{
_bstr_t csname = var_val;
hot[i].csname = csname;
}
test = pHotfix->Get( L"HotFixID", 0, &var_val, NULL, NULL );
if ( SUCCEEDED( test ) && var_val.vt !=VT_NULL )
{
_bstr_t HotFixID = var_val;
hot[i].HotFixID = HotFixID;
}
test = pHotfix->Get( L"InstalledOn", 0, &var_val, NULL, NULL );
if ( SUCCEEDED( test ) && var_val.vt !=VT_NULL )
{
_bstr_t InstalledOn = var_val;
hot[i].InstalledOn =InstalledOn;
}
i++;
}
}
}
}
CoUninitialize();
return i;
}