Windows HotFix Check Via C/C++

源文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include "hotfix.h"
 
int _tmain(int argc, _TCHAR* argv[])
{
	//WmiInit();
	Hotfix h[200];
	int counts=Gethotfix(h);
	std::cout < < "Hotfix Begin:" <<std::endl;
	std::cout << counts<< std::endl;
	std::cout << "----------------------------------------------------------------" <<std::endl;
	int j =0;
	while ( h[j].csname.length() !=0)
	{	
		std::cout << "HotFixID: " <<h[j].HotFixID<<std::endl;
		std::cout << "Hotfix InstalledOn: " <<h[j].InstalledOn<< std::endl;
		std::cout << "----------------------------------------------------------------" <<std::endl;
		j++;
	}
	std::cout << "======================"<< std::endl;
	std::cout << "Hotfix  last pached on:" << h[j-1].InstalledOn<< std::endl;
	return 0;
}


头文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef _WIN32_DCOM
#define _WIN32_DCOM
#endif
#include "stdafx.h"
#include <windows .h>
#include <objbase .h>
#include <atlbase .h>
#include <iostream>
#include <wbemidl .h>
#include <comutil .h>
#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<iwbemclassobject **>( &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;
}</iwbemclassobject></comutil></wbemidl></iostream></atlbase></objbase></windows>

原创文章,转载请注明: 转载自 obaby@mars

本文标题: 《Windows HotFix Check Via C/C++》

本文链接地址: http://www.h4ck.org.cn/2010/07/windows-hotfix-check-via-cc/

You may also like

发表评论

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