⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 该小程序能枚举包括cup信息, bios信息, 内存
💻 CPP
字号:
#ifndef _WIN32_DCOM
#define _WIN32_DCOM
#endif

#include <windows.h>
#include <objbase.h>
#include <atlbase.h>
#include <iostream>
#include <wbemidl.h>
#include <comutil.h>


int main( int argc, char** argv )
{
	HRESULT hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
	if ( FAILED( hr ) )
	{
		std::cerr << "COM initialization failed" << std::endl;
		return -1;
	}

	// setup process-wide security context
	hr = CoInitializeSecurity( NULL, // we're not a server
							   -1, // we're not a server
							   NULL, // dito
							   NULL, // reserved
							   RPC_C_AUTHN_LEVEL_DEFAULT, // let DCOM decide
							   RPC_C_IMP_LEVEL_IMPERSONATE,
							   NULL,
							   EOAC_NONE,
							   NULL );
	if ( FAILED( hr ) )
	{
		std::cerr << "Security initialization failed" << std::endl;
		return -1;
	}

	int result = 0;
	// we're going to use CComPtr<>s, whose lifetime must end BEFORE CoUnitialize is called
	{
		// connect to WMI
		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;
		}

		// connect to local service with current credentials
		CComPtr< IWbemServices > service;
		hr = locator->ConnectServer( L"root\\cimv2", NULL, NULL, NULL,
									 WBEM_FLAG_CONNECT_USE_MAX_WAIT,
									 NULL, NULL, &service );
		if ( SUCCEEDED( hr ) )
		{
			// execute a query
			CComPtr< IEnumWbemClassObject > enumerator;
			hr = service->ExecQuery( L"WQL", L"SELECT * FROM Win32_Processor",
								     WBEM_FLAG_FORWARD_ONLY, NULL, &enumerator );
			if ( SUCCEEDED( hr ) )
			{
				// read the first instance from the enumeration (only one on single processor machines)
				CComPtr< IWbemClassObject > processor = NULL;
				ULONG retcnt;
				hr = enumerator->Next( WBEM_INFINITE, 1L, reinterpret_cast<IWbemClassObject**>( &processor ), &retcnt );
				if ( SUCCEEDED( hr ) )
				{
					if ( retcnt > 0 )
					{
						// we have a processor installed :)
						// now extract a property value
						_variant_t var_val;
						hr = processor->Get( L"Name", 0, &var_val, NULL, NULL );
						if ( SUCCEEDED( hr ) )
						{
							_bstr_t str = var_val;
							std::cout << "Processor name: " << str << std::endl;
						}
						else
						{
							std::cerr << "IWbemClassObject::Get failed" << std::endl;
							result = -1;
						}
					}
					else
					{
						std::cout << "Enumeration empty" << std::endl;
					}
				}
				else
				{
					std::cerr << "Error in iterating through enumeration" << std::cerr;
					result = -1;
				}
			}
			else
			{
				std::cerr << "Query failed" << std::endl;
				result = -1;
			}
		}
		else
		{
			std::cerr << "Couldn't connect to service" << std::endl;
			result = -1;
		}
	}
	CoUninitialize();

	return result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -