Windows HotFix Check Via C/C++ Update

Main File(hotfixtest.cpp)[Fix vista Filetime]:

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
// hotfixtest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "hotfix.h"
int _tmain(int argc, _TCHAR* argv[])
{
	std::cout < < "Hotfix Begin:" <<std::endl;
	std::cout << "----------------------------------------------------------------" <<std::endl;
	Hotfix h[500];
	COleDateTime lastfix;
	_bstr_t lastdate;
	int counts=Gethotfix(h,lastfix);
	if (counts == -1)
	{
		std::cout <<"Error while connet to wmi service"<<std::endl;
	}
	else
	{
		int j =0;
		while ( h[j].csname.length() !=0 && h[j].HotFixID.length() !=0)
		{	
			std::cout << "HotFixID: " <<h[j].HotFixID<<std::endl;
			std::cout << "Hotfix InstalledOn: " <<h[j].InstalledOn<< std::endl;
			//std::cout << "Hotfix Name: " <<h[j].HotFixName<< std::endl;
			//std::cout << "----------------------------------------------------------------" <<std::endl;
			j++;
		}
		std::cout << "======================"<< std::endl;
		lastdate = ColeTime2Bstr(lastfix);
		std::cout << "Hotfix  last pached on:" << lastdate<<"     All patchs:"<<j<< std::endl;
	}
	int endno;
	std::cin>>endno;
	return 0;
}


Header File(hotfixdetect.cpp):

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// hotfixdetect.cpp : 定义控制台应用程序的入口点。
//
#define  _AFXDLL
#include "stdafx.h"
#include "afxdisp.h"
#ifndef _WIN32_DCOM
#define _WIN32_DCOM
#endif
#include <objbase .h>
#include <atlbase .h>
#include <iostream>
#include <wbemidl .h>
#include <comutil .h>
#include "Comdef.h"
#include </comutil><comutil .h>
#pragma comment(lib,"Wbemuuid.lib")
 
#define LENGTH 16
 
HRESULT test;
 
struct  Hotfix									//系统补丁结构
{
		_bstr_t csname;						//主机名称
		_bstr_t Description;				//描述符
		_bstr_t FixComments;			//字段可能为空
		_bstr_t HotFixID;					//补丁ID显示为补丁名称	
		_bstr_t FixCaption;					//
		_bstr_t InstallDate;					//
		_bstr_t InstalledBy;				//安装用户
		_bstr_t InstalledOn;				//补丁安装日期
		_bstr_t HotFixName;				//补丁名称多数情况下为空
		_bstr_t ServicePackInEffect;	//影响sp版本信息
		_bstr_t Fixstatus;					//补丁状态,字段通常为空
}  Myhotfix;
struct  NetShare 
{
	_bstr_t AccessMask;
	_bstr_t AllowMaximum;
	_bstr_t Caption;
	_bstr_t Description;
	_bstr_t InstallDate;
	_bstr_t MaximumAllowed;
	_bstr_t Name;
	_bstr_t Path;
	_bstr_t Status;
	_bstr_t Type;
} MyNetShare;
ULONG retcnt =1;
ULONG shareret =1;
COleDateTime LastPatchdate(1970,1,1,0,0,0);
 
//////////////////////////////////////////////////////////////////////////////////
//字符串转换函数,转换字符串为64位整形
//针对Vista 返回日期为16位的Filetime格式
//将16位字符串转换为Int64,返回日期目前未考虑大写字母
//////////////////////////////////////////////////////////////////////////////////
COleDateTime StringtoInt64(char *str)
{
	INT64 result = 0;
	while(*str)
	{
		result = result < < 4;
		if(*str <= '9')
		{
			result += *str-'0';
		}
		else 
		{
			//*str =toupper(*str);
			result += *str - ('a'-10); 
		}
		str++;
	}
	return result;
}
//////////////////////////////////////////////////////////////////////////////////
//修改系统补丁日期格式
//通常情况下返回安装日志可能值为:0,2010/02/05,2010-02-05之类
//Vista返回安装日期为20100205或者是十六位的filetime格式
//////////////////////////////////////////////////////////////////////////////////
COleDateTime ChangeTimeType (Hotfix bBuffer)
{
	char* tmpSbuffer = bBuffer.InstalledOn;
	if (bBuffer.InstalledOn.length() ==0)  //返回日期长度为0
	{
		COleDateTime newTime(1970,1,1,0,0,0);
		return newTime;
	}
	if (bBuffer.InstalledOn.length() ==8 && !(NULL != strchr(tmpSbuffer,'/') || NULL !=strchr(tmpSbuffer,'-'))) //返回日期长度为8,并且在字符串中没有找到/或者-
	{
		int nTime = atoi(tmpSbuffer);
		int year = nTime/10000;
		int month = (nTime-year*10000)/100;
		int day = nTime-year*10000-month*100;
 
		COleDateTime newTime(year,month,day,0,0,0);
		return newTime;
	}
	if (bBuffer.InstalledOn.length() == 16) //返回日期长度为16
	{
		FILETIME FixFileTime;
		INT64 intBuffer = StringtoInt64(tmpSbuffer);
		DWORD high =( intBuffer>>32);
		DWORD low = intBuffer& 0x0FFFFFFFF;
		FixFileTime.dwHighDateTime=high;
		FixFileTime.dwLowDateTime =low;
			COleVariant ct1(FixFileTime);
		ct1.ChangeType(VT_DATE);
		COleDateTime otime1 = ct1;
		return otime1;
	}
	if (NULL != strchr(tmpSbuffer,'/') || NULL !=strchr(tmpSbuffer,'-') ) //默认日期格式
	{
		COleVariant vtime1(bBuffer.InstalledOn);
		vtime1.ChangeType(VT_DATE);
		COleDateTime otime1 = vtime1;
		return otime1;
	}
 
}
 
//////////////////////////////////////////////////////////////////////////////////
//Coletime2_bstr 时间转为字符串
//将Coletime转换为_bstr_t格式
//////////////////////////////////////////////////////////////////////////////////
_bstr_t ColeTime2Bstr (COleDateTime vtime)
{
	SYSTEMTIME   systime;			//时间类型转换
	VariantTimeToSystemTime(vtime,   &systime);   
	CTime   tm(systime); 
	CString szTime=tm.Format("%Y-%m-%d %H:%M:%S");
	BSTR bstrText = szTime.AllocSysString();	
	return bstrText;
	//SysFreeString(bstrText); // 用完释放
}
 
//////////////////////////////////////////////////////////////////////////////////
//获取当前系统补丁信息
//返回值:-1为错误,如果执行正确返回系统补丁数目
//////////////////////////////////////////////////////////////////////////////////
int Gethotfix(Hotfix hot[],COleDateTime &last)
{
	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;		
 
					}
					test = pHotfix->Get( L"Name", 0, &var_val, NULL, NULL );
					if ( SUCCEEDED( test ) &&  var_val.vt ==VT_BSTR )  
					{
						_bstr_t Fixname = var_val;
						hot[i].HotFixName =Fixname;						
					}
					//更新最后的补丁时间
					COleDateTime ootime= ChangeTimeType(hot[i]);
					if (ootime > LastPatchdate)
					{
						LastPatchdate = ootime;
						last =LastPatchdate;
 
					}
				}
			i++;
			}
		}
		else
		{
			//std::cerr < < "Couldn't connect to service" << std::endl;
			return -1;
		}
	}
	CoUninitialize();
	return i;
}
//////////////////////////////////////////////////////////////////////////////////
//获取当前系统共享信息
//返回值:-1为错误,如果执行正确返回共享数目
//////////////////////////////////////////////////////////////////////////////////
int GetNetShare( NetShare myshares[]) 
{
	int j =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_Share",
													WBEM_FLAG_RETURN_IMMEDIATELY, 
													NULL, 
													&enumerator );
			while ( SUCCEEDED( hr ) && shareret >0  )
			{
				CComPtr< IWbemClassObject > pNetShare;
				//枚举当前系统共享
				hr = enumerator->Next( WBEM_INFINITE, 
														1L,
														reinterpret_cast</iwbemclassobject><iwbemclassobject **>( &pNetShare), 
														&shareret );
				if ( SUCCEEDED( hr ) && shareret >0 )
				{
					_variant_t var_val;
					test = pNetShare->Get( L"Path", 0, &var_val, NULL, NULL );
					if ( SUCCEEDED( test ) && var_val.vt !=VT_NULL )  
					{
						_bstr_t SharePath = var_val;
						myshares[j].Path = SharePath;						
					}
					test = pNetShare->Get( L"Name", 0, &var_val, NULL, NULL );
					if ( SUCCEEDED( test ) && var_val.vt !=VT_NULL )  
					{
						_bstr_t ShareName = var_val;
						myshares[j].Name = ShareName;						
					}
					j++;
				}			
 
			}
		}
		else
		{
			//std::cerr < < "Couldn't connect to service" << std::endl;
			return -1;
		}
	}
	CoUninitialize();
	return j;
}

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

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

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

You may also like

2条评论

发表评论

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