• windows下WMI使用C++查询用户硬件信息


    最近需要做下用户信息统计,发现WMI真是个好东西,同时觉得COM这东西,还真不能少>_<!!

    下面是源码:

    /********************************************************************
    	created:	2014/02/18
    	author:		baesky
    	
    	purpose:	Gater user infomation
    *********************************************************************/
    #include <comdef.h>
    #include <Wbemidl.h>
    
    # pragma comment(lib, "wbemuuid.lib")
    
    struct UserInfo
    {
    	/** your spec*/
    };
    
    /** video card and OS */
    bool GetUserInfo(UserInfo& Info)
    {
    	HRESULT hres;
    
    	hres =  CoInitialize( NULL );
    	if (FAILED(hres))
    	{
    		return false;                 
    	}
    
    	hres =  CoInitializeSecurity(
    		NULL, 
    		-1,                          
    		NULL,                        
    		NULL,                        
    		RPC_C_AUTHN_LEVEL_DEFAULT,   
    		RPC_C_IMP_LEVEL_IMPERSONATE, 
    		NULL,                        
    		EOAC_NONE,                   
    		NULL                         
    		);
    
    
    	if (FAILED(hres))
    	{
    		CoUninitialize();
    		return false;                   
    	}
    
    	IWbemLocator *pLoc = NULL;
    
    	hres = CoCreateInstance(
    		CLSID_WbemLocator,             
    		0, 
    		CLSCTX_INPROC_SERVER, 
    		IID_IWbemLocator, (LPVOID *) &pLoc);
    
    	if (FAILED(hres))
    	{
    		CoUninitialize();
    		return false;                 
    	}
    
    	IWbemServices *pSvc = NULL;
    
    	hres = pLoc->ConnectServer(
    		_bstr_t(L"ROOT\CIMV2"), 
    		NULL,                    
    		NULL,                    
    		0,                       
    		NULL,                    
    		0,                       
    		0,                       
    		&pSvc                    
    		);
    
    	if (FAILED(hres))
    	{
    		pLoc->Release();     
    		CoUninitialize();
    		return false;               
    	}
    
    	hres = CoSetProxyBlanket(
    		pSvc,                        
    		RPC_C_AUTHN_WINNT,           
    		RPC_C_AUTHZ_NONE,            
    		NULL,                        
    		RPC_C_AUTHN_LEVEL_CALL,      
    		RPC_C_IMP_LEVEL_IMPERSONATE, 
    		NULL,                        
    		EOAC_NONE                    
    		);
    
    	if (FAILED(hres))
    	{
    		pSvc->Release();
    		pLoc->Release();     
    		CoUninitialize();
    		return false;               
    	}
    
    	IEnumWbemClassObject* pEnumerator = NULL;
    	hres = pSvc->ExecQuery(
    		bstr_t("WQL"), 
    		bstr_t("SELECT * FROM Win32_OperatingSystem"),
    		WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
    		NULL,
    		&pEnumerator);
    
    	if (FAILED(hres))
    	{
    		pSvc->Release();
    		pLoc->Release();
    		CoUninitialize();
    		return false;               
    	}
    
    	IWbemClassObject *pclsObj;
    	ULONG uReturn = 0;
    
    	while (pEnumerator)
    	{
    		HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
    			&pclsObj, &uReturn);
    
    		if(0 == uReturn)
    		{
    			break;
    		}
    
    		VARIANT vtProp;
    
    		hr = pclsObj->Get(L"Caption", 0, &vtProp, 0, 0);  //os
    		//vtProp.bstrVal is what you need.
    
    		//VariantClear(&vtProp);
    		//hr = pclsObj->Get(L"CSDVersion", 0, &vtProp, 0, 0); // example "sp3"
    		//VariantClear(&vtProp);
    		//hr = pclsObj->Get(L"OSArchitecture", 0, &vtProp, 0, 0); // example x64
    		//VariantClear(&vtProp);
    
    		pclsObj->Release();
    	}
    	
    	pEnumerator->Release();
    	pEnumerator=NULL;
    	hres = pSvc->ExecQuery(
    		bstr_t("WQL"), 
    		bstr_t("SELECT * FROM Win32_VideoController"),
    		WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
    		NULL,
    		&pEnumerator);
    
    	if (FAILED(hres))
    	{
    		pSvc->Release();
    		pLoc->Release();
    		CoUninitialize();
    		return false;               
    	}
    
    	while (pEnumerator)
    	{
    		HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
    			&pclsObj, &uReturn);
    
    		if(0 == uReturn)
    		{
    			break;
    		}
    
    		VARIANT vtProp;
    
    		hr = pclsObj->Get(L"Caption", 0, &vtProp, 0, 0);  //video desc
                    /** 
                      .,,,,,,,,,,,,,,,,,,,,,,your logic
                     */
    		VariantClear(&vtProp);
    		pclsObj->Release();
    	}
    
    	pEnumerator->Release();
    	pEnumerator=NULL;
    	hres = pSvc->ExecQuery(
    		bstr_t("WQL"), 
    		bstr_t("SELECT * FROM Win32_Processor"),
    		WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
    		NULL,
    		&pEnumerator);
    
    	if (FAILED(hres))
    	{
    		pSvc->Release();
    		pLoc->Release();
    		CoUninitialize();
    		return false;               
    	}
    
    	while (pEnumerator)
    	{
    		HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
    			&pclsObj, &uReturn);
    
    		if(0 == uReturn)
    		{
    			break;
    		}
    
    		VARIANT vtProp;
    
    		hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);  //video desc
    		 /** 
                      .,,,,,,,,,,,,,,,,,,,,,,your logic
                     */
    		VariantClear(&vtProp);
    		pclsObj->Release();
    	}
    	
    	pSvc->Release();
    	pLoc->Release();
    	pEnumerator->Release();
    	
    	CoUninitialize();
    
    	return true;
    }                         
    

      

  • 相关阅读:
    OpenGL(九) 三维混色和深度缓存设置
    自由度(degree of freedom)
    自由度(degree of freedom)
    非参贝叶斯(Bayesian Non-parameter)初步
    非参贝叶斯(Bayesian Non-parameter)初步
    一个GCC4.6.3的奇妙问题的糊涂解决方案
    Rational Rose--简介
    android 国际化
    日志文件C++ 时间 文件 行数
    看原理图之UART
  • 原文地址:https://www.cnblogs.com/Baesky/p/WMI_CPP.html
Copyright © 2020-2023  润新知