WMI Based System Share Detect Via C/C++

Cpp File:

// hotfixtest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "hotfix.h"

int _tmain(int argc, _TCHAR* argv[])
{
           //系统共享检测代码
	NetShare Myshare[50];
	int shareno = GetNetShare(Myshare);
	std::cout < < "----------------------------------------------------------------" <>k;
	return 0;
}


Headerfile hotfix.h:

// hotfixdetect.cpp : 定义控制台应用程序的入口点。
//
#ifndef _WIN32_DCOM
#define _WIN32_DCOM
#endif
#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
#include 
#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( &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
* 网址:https://lang.ma/
* 个性:https://oba.by/
* 本文标题: 《WMI Based System Share Detect Via C/C++》
* 本文链接:https://www.danteng.me/2010/07/1769
* 短链接:https://oba.by/?p=1769
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

发表回复

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