WMI Based System Share Detect Via C/C++

Cpp File:

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
// hotfixtest.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include "hotfix.h"
 
int _tmain(int argc, _TCHAR* argv[])
{
           //系统共享检测代码
	NetShare Myshare[50];
	int shareno = GetNetShare(Myshare);
	std::cout < < "----------------------------------------------------------------" <<std::endl;
	std::cout << "Net share check Begin:" <<std::endl;
	std::cout <<"Net share Numbers:"<<shareno<<std::endl;
	std::cout << "----------------------------------------------------------------" <<std::endl;
	std::cout << "---Name--------------Path-------------------------------" <<std::endl;
	int k =0;
	while (Myshare[k].Name.length() !=0)
	{	
		std::cout <<  Myshare[k].Name<<"    "<<Myshare[k].Path<< std::endl;
		k++;
	}
	std::cout << "----------------------------------------------------------------" <<std::endl;
	std::cin>>k;
	return 0;
}


Headerfile hotfix.h:

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
// hotfixdetect.cpp : 定义控制台应用程序的入口点。
//
#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"
//#include "time.h"
#pragma comment(lib,"Wbemuuid.lib")
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;
//////////////////////////////////////////////////////////////////////////////////
//获取当前系统共享信息
//返回值:-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 **>( &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

本文标题: 《WMI Based System Share Detect Via C/C++》

本文链接地址: http://www.h4ck.org.cn/2010/07/wmi-based-system-share-detect-via-cc/

You may also like

发表评论

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