Windows HotFix Check Via C/C++

源文件:

#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;
}
☆版权☆

* 网站名称:obaby@mars
* 网址:https://oba.by/
* 个性:https://oba.by/
* 本文标题: 《Windows HotFix Check Via C/C++》
* 本文链接:https://zhongxiaojie.com/2010/07/1763
* 短链接:https://oba.by/?p=1763
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注