找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 2851|回复: 1

wmi 获取系统信息

[复制链接]

7

主题

11

回帖

218

积分

用户组: 中·技术宅

UID
536
精华
1
威望
16 点
宅币
154 个
贡献
9 次
宅之契约
0 份
在线时间
3 小时
注册时间
2014-11-6
发表于 2014-11-19 21:46:00 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
  1. #ifndef MY_WMI_CLASS_H__
  2. #define MY_WMI_CLASS_H__

  3. /*******************************************************************************
  4. *  @file        
  5. *  @author      def< qq group: 324164944 >
  6. *  @blog        http://www.cnblogs.com/itdef/
  7. *  @brief     
  8. /*******************************************************************************/

  9. #include <Wbemidl.h>


  10. namespace DEF
  11. {
  12. class CWMI
  13. {
  14.         IWbemLocator *pLoc_;  
  15.         IWbemServices *pSvc_;  

  16. public:
  17.         CWMI():pLoc_(NULL),pSvc_(NULL){}
  18.         ~CWMI();
  19.         BOOL QueryInfo(const char* strSQL, const char* strAttr );
  20.         BOOL Initialize();



  21. };


  22. }        //namespace DEF




  23. #endif //MY_WMI_CLASS_H__
复制代码

  1. #include "stdafx.h"

  2. /*******************************************************************************
  3. *  @file        
  4. *  @author      def< qq group: 324164944 >
  5. *  @blog        http://www.cnblogs.com/itdef/
  6. *  @brief     
  7. /*******************************************************************************/

  8. #include "CMyWMIClass.h"
  9. #include <iostream>
  10. #include <comutil.h>

  11. using namespace DEF;
  12. using namespace std;

  13. #pragma comment(lib,"Wbemuuid")
  14. #pragma comment(lib,"comsuppw")


  15. BOOL CWMI::Initialize()
  16. {
  17.         BOOL bRet = FALSE;
  18.         HRESULT hres; //定义COM调用的返回

  19.         try{
  20.                 hres =  CoInitializeEx(0, COINIT_MULTITHREADED);   
  21.                 if (FAILED(hres))  
  22.                 {  
  23.                         throw exception("CoInitializeEx() error.");
  24.                 }  

  25.                 hres =  CoInitializeSecurity(  
  26.                         NULL,   
  27.                         -1,                          // COM authentication  
  28.                         NULL,                        // Authentication services  
  29.                         NULL,                        // Reserved  
  30.                         RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication   
  31.                         RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation   
  32.                         NULL,                        // Authentication info  
  33.                         EOAC_NONE,                   // Additional capabilities   
  34.                         NULL                         // Reserved  
  35.                         );
  36.                 if (FAILED(hres))  
  37.                 {  
  38.                         throw exception("CoInitializeEx() error.");
  39.                 }  

  40.                 hres = CoCreateInstance(  
  41.                         CLSID_WbemLocator,  
  42.                         0,   
  43.                         CLSCTX_INPROC_SERVER,   
  44.                         IID_IWbemLocator, (LPVOID *) &pLoc_);  
  45.                 if (FAILED(hres))  
  46.                 {  
  47.                         throw exception("CoCreateInstance() error.");
  48.                 }

  49.                 // to make IWbemServices calls.  
  50.                 hres = pLoc_->ConnectServer(  
  51.                         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace  
  52.                         NULL,                    // User name. NULL = current user  
  53.                         NULL,                    // User password. NULL = current  
  54.                         0,                       // Locale. NULL indicates current  
  55.                         NULL,                    // Security flags.  
  56.                         0,                       // Authority (e.g. Kerberos)  
  57.                         0,                       // Context object   
  58.                         &pSvc_                    // pointer to IWbemServices proxy  
  59.                         );  

  60.                 if (FAILED(hres))  
  61.                 {  
  62.                         throw exception("ConnectServer() error.");
  63.                 }

  64.                 hres = CoSetProxyBlanket(  
  65.                         pSvc_,                        // Indicates the proxy to set  
  66.                         RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx  
  67.                         RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx  
  68.                         NULL,                        // Server principal name   
  69.                         RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx   
  70.                         RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx  
  71.                         NULL,                        // client identity  
  72.                         EOAC_NONE                    // proxy capabilities   
  73.                         );  

  74.                 if (FAILED(hres))  
  75.                 {  
  76.                         throw exception("CoSetProxyBlanket() error.");
  77.                 }  


  78.                 bRet = TRUE;
  79.         }catch(exception& e)
  80.         {
  81.                 cout << e.what() << endl;
  82.         }
  83.         return bRet;
  84. }

  85. CWMI::~CWMI()
  86. {
  87.         if( NULL != pSvc_)
  88.         {
  89.                 pSvc_->Release();  
  90.                 pSvc_ = NULL;
  91.         }
  92.         if(pLoc_ != NULL )
  93.         {
  94.                 pLoc_->Release();
  95.                 pLoc_ = NULL;
  96.         }
  97.         CoUninitialize();
  98. }


  99. BOOL CWMI::QueryInfo(const char* strSQL, const char* strAttr )
  100. {
  101.         HRESULT hres; //定义COM调用的返回  
  102.         IWbemClassObject* pclsObj = NULL;
  103.         IEnumWbemClassObject* pEnum = NULL;
  104.         BOOL bRet = FALSE;

  105.         if( NULL == strSQL ||
  106.                 NULL == strAttr)
  107.         {
  108.                 return FALSE;
  109.         }

  110.         try{
  111.                 hres = pSvc_->ExecQuery(  
  112.                         bstr_t("WQL"),     
  113.                         bstr_t(strSQL),  
  114.                         WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,   
  115.                         NULL,  
  116.                         &pEnum);  

  117.                 if (FAILED(hres))  
  118.                 {  
  119.                         throw exception("ExecQuery() error.");
  120.                 }

  121.                 ULONG uReturn = 0;
  122.                 while( pEnum )
  123.                 {
  124.                         HRESULT hr = pEnum->Next( WBEM_INFINITE, 1, &pclsObj, &uReturn );
  125.                         if( 0 == uReturn )
  126.                         {
  127.                                 break;
  128.                         }
  129.                         VARIANT vtProp;
  130.                         VariantInit( &vtProp );

  131.                         hr = pclsObj->Get( bstr_t( strAttr ), 0, &vtProp, 0, 0 );
  132.                         if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_BSTR))
  133.                         {
  134.                                 char* str = _com_util::ConvertBSTRToString( vtProp.bstrVal );
  135.                                 //printf( "%s, length = %d \n", str, strlen( str ) );
  136.                                 printf("%s \n",str);
  137.                                 delete [] str;
  138.                         }
  139.                         VariantClear( &vtProp );
  140.                 }


  141.                 bRet = TRUE;
  142.         }catch(exception& e)
  143.         {
  144.                 cout << e.what() << endl;
  145.         }
  146.         if(pclsObj != NULL)
  147.         {
  148.                 pclsObj->Release();
  149.                 pclsObj = NULL;
  150.         }
  151.         if(pEnum != NULL)
  152.         {
  153.                 pEnum->Release();
  154.                 pEnum = NULL;
  155.         }
  156.         return bRet;
  157. }
复制代码


测试代码

  1. //

  2. #include "stdafx.h"

  3. /*******************************************************************************
  4. *  @file        
  5. *  @author      def< qq group: 324164944 >
  6. *  @blog        http://www.cnblogs.com/itdef/
  7. *  @brief     
  8. /*******************************************************************************/

  9. #include <iostream>
  10. #include "CMyWMIClass.h"

  11. using namespace std;

  12. int _tmain(int argc, _TCHAR* argv[])
  13. {
  14.                 DEF::CWMI a;
  15.                 a.Initialize();

  16.                 cout << endl << "Caption ";
  17.                 a.QueryInfo("SELECT * FROM Win32_OperatingSystem","Caption");

  18.                 cout << "Manufacturer ";
  19.                 a.QueryInfo("SELECT * FROM Win32_OperatingSystem","Manufacturer");

  20.                 cout << "CSName ";
  21.                 a.QueryInfo("SELECT * FROM Win32_OperatingSystem","CSName");


  22.                 cout << "PNPDeviceID ";
  23.                 a.QueryInfo("SELECT * FROM Win32_DiskDrive", "PNPDeviceID");

  24.                 cout << "Disk SerialNumber ";
  25.                 a.QueryInfo("SELECT * FROM Win32_PhysicalMedia", "SerialNumber");

  26.                 cout << "TotalVisibleMemorySize ";
  27.                 a.QueryInfo("SELECT * FROM Win32_OperatingSystem","TotalVisibleMemorySize");

  28.                 cout << "FreePhysicalMemory ";
  29.                 a.QueryInfo("SELECT * FROM Win32_OperatingSystem","FreePhysicalMemory");


  30.                 cout << "SerialNumber ";
  31.                 a.QueryInfo("SELECT * FROM Win32_DiskDrive","SerialNumber");



  32. //                  cout << "Process Caption  ";
  33. //                  a.QueryInfo("SELECT * FROM Win32_Process","Caption");
  34. //  
  35. //                  cout << "Process Handle  ";
  36. //                  a.QueryInfo("SELECT * FROM Win32_Process","Handle");
  37. //  

  38.                 cout << "Process ExecutablePath  ";
  39.                 a.QueryInfo("SELECT * FROM Win32_Process","ExecutablePath");




  40.         return 0;
  41. }
复制代码
wmmi.png
回复

使用道具 举报

1111

主题

1651

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
244
威望
743 点
宅币
24235 个
贡献
46222 次
宅之契约
0 份
在线时间
2296 小时
注册时间
2014-1-26
发表于 2014-11-19 21:58:05 | 显示全部楼层
我觉得代码应该再加一些注释,解释你做每一步是为了个啥。
这样更容易看懂,甚至不用看代码就能知道代码是做啥的,啥时候能用。
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-20 02:57 , Processed in 0.040797 second(s), 38 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表