码迷,mamicode.com
首页 > 编程语言 > 详细

C/C++通过WMI和系统API函数获取获取系统硬件配置信息

时间:2016-10-10 20:08:23      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

转载:http://www.cnblogs.com/renyuan/archive/2012/12/29/2838716.html

转载:http://blog.csdn.net/jhqin/article/details/5548656

转载:http://blog.csdn.net/dongdan_002/article/details/42460343

头文件WMIInfo.h

 1 /*************************************************************************************************
 2 *
 3 * File Name    : WMIInof.h
 4 *
 5 * Created      : 2016/10/10
 6 *
 7 * Author       : weidongdong
 8 *
 9 * Model        : 
10 *
11 * Description  : [[CN]] 获取系统硬件配置信息 [[CN]]                                                  
12 *
13 **************************************************************************************************/
14 #ifndef _WMIINFO_H_
15 #define _WMIINFO_H_
16  
17 #include <WbemIdl.h>  
18 #pragma comment(lib,"WbemUuid.lib")
19 
20 class CWmiInfo
21 {
22 public:
23     CWmiInfo();
24     ~CWmiInfo();
25 
26 public:  
27     HRESULT InitWmi();    //初始化WMI  
28     HRESULT ReleaseWmi(); //释放 
29 
30     /*获取一个类成员
31     *@param [in ] ClassName   Example: "Win32_Processor"
32     *@param [in ] ClassMember Example: "SerialNumber"
33     *@param [out] chRetValue 
34     *@param return TRUE success; false fail
35 
36     Example:
37     CString strRetValue; 
38     GetSingleItemInfo(_T("Win32_Processor"),_T("Caption"),strRetValue); 
39     */
40      BOOL GetSingleItemInfo(CString ClassName,CString ClassMember,CString &chRetValue);
41 
42      /*获取一个类的多个成员
43     *@param [in ] ClassName   Example: "Win32_Processor"
44     *@param [in ] ClassMember Example: "SerialNumber"
45     *@param [in ] n   成员个数
46     *@param [out] chRetValue 
47     *@param return TRUE success; false fail
48 
49     Example:
50     CString strRetValue;CString [] strClassMem = {_T("Caption"),_T("CurrentClockSpeed"),_T("DeviceID"),_T("Manufacturer"),_T("Manufacturer")};
51     GetGroupItemInfo(_T("Win32_Processor"),strClassMem,5,strRetValue);
52     */
53      BOOL GetGroupItemInfo(CString ClassName,CString ClassMember[],int n,CString &chRetValue);
54 
55 private:  
56     void VariantToString(const LPVARIANT,CString &) const;//将Variant类型的变量转换为CString
57 
58 private:  
59     IEnumWbemClassObject* m_pEnumClsObj;  
60     IWbemClassObject* m_pWbemClsObj;  
61     IWbemServices* m_pWbemSvc;  
62     IWbemLocator* m_pWbemLoc;  
63 };
64 #endif

实现文件WMIInfo.cpp

  1 #include "stdafx.h"
  2 #include "WmiInfo.h"  
  3 
  4 CWmiInfo::CWmiInfo(void)  
  5 {  
  6     m_pWbemSvc=NULL;  
  7     m_pWbemLoc=NULL;  
  8     m_pEnumClsObj = NULL;  
  9 }  
 10 
 11 CWmiInfo::~CWmiInfo(void)  
 12 {  
 13     m_pWbemSvc=NULL;  
 14     m_pWbemLoc=NULL;  
 15     m_pEnumClsObj = NULL;  
 16 }  
 17 
 18 HRESULT CWmiInfo::InitWmi()  
 19 {  
 20     HRESULT hr;  
 21 
 22     //一、初始化COM组件  
 23     //初始化COM  
 24     hr=::CoInitializeEx(0,COINIT_MULTITHREADED);  
 25     if (SUCCEEDED(hr) || RPC_E_CHANGED_MODE == hr)  
 26     {  
 27         //设置进程的安全级别,(调用COM组件时在初始化COM之后要调用CoInitializeSecurity设置进程安全级别,否则会被系统识别为病毒)  
 28         hr=CoInitializeSecurity(NULL,  
 29             -1,  
 30             NULL,                     
 31             NULL,  
 32             RPC_C_AUTHN_LEVEL_PKT,  
 33             RPC_C_IMP_LEVEL_IMPERSONATE,  
 34             NULL,  
 35             EOAC_NONE,  
 36             NULL);  
 37         //VERIFY(SUCCEEDED(hr));  
 38 
 39         //二、创建一个WMI命名空间连接  
 40         //创建一个CLSID_WbemLocator对象  
 41         hr=CoCreateInstance(CLSID_WbemLocator,  
 42             0,  
 43             CLSCTX_INPROC_SERVER,  
 44             IID_IWbemLocator,  
 45             (LPVOID*)&m_pWbemLoc);  
 46 //        VERIFY(SUCCEEDED(hr));  
 47 
 48         //使用m_pWbemLoc连接到"root\cimv2"并设置m_pWbemSvc的指针  
 49         hr=m_pWbemLoc->ConnectServer(CComBSTR(L"ROOT\\CIMV2"),  
 50             NULL,  
 51             NULL,  
 52             0,  
 53             NULL,  
 54             0,  
 55             0,  
 56             &m_pWbemSvc);  
 57 //        VERIFY(SUCCEEDED(hr));  
 58 
 59         //三、设置WMI连接的安全性  
 60         hr=CoSetProxyBlanket(m_pWbemSvc,  
 61             RPC_C_AUTHN_WINNT,  
 62             RPC_C_AUTHZ_NONE,  
 63             NULL,  
 64             RPC_C_AUTHN_LEVEL_CALL,  
 65             RPC_C_IMP_LEVEL_IMPERSONATE,  
 66             NULL,  
 67             EOAC_NONE);  
 68 //        VERIFY(SUCCEEDED(hr));  
 69 
 70     }  
 71     return(hr);  
 72 }  
 73 
 74 HRESULT CWmiInfo::ReleaseWmi()  
 75 {  
 76     HRESULT hr;  
 77 
 78     if (NULL != m_pWbemSvc)  
 79     {  
 80         hr=m_pWbemSvc->Release();  
 81     }  
 82     if (NULL != m_pWbemLoc)  
 83     {  
 84         hr=m_pWbemLoc->Release();  
 85     }  
 86     if (NULL != m_pEnumClsObj)  
 87     {  
 88         hr=m_pEnumClsObj->Release();  
 89     }  
 90 
 91     ::CoUninitialize();  
 92 
 93     return(hr);  
 94 }  
 95 
 96 BOOL CWmiInfo::GetSingleItemInfo(CString ClassName,CString ClassMember,CString &chRetValue)  
 97 {  
 98     USES_CONVERSION;  
 99 
100     CComBSTR query("SELECT * FROM ");  
101     VARIANT vtProp;  
102     ULONG uReturn;  
103     HRESULT hr;  
104     BOOL bRet = FALSE;  
105 
106     if (NULL != m_pWbemSvc)  
107     {  
108         //查询类ClassName中的所有字段,保存到m_pEnumClsObj中  
109         query+=CComBSTR(ClassName);  
110         hr=m_pWbemSvc->ExecQuery(CComBSTR("WQL"),query,WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY,  
111             0,&m_pEnumClsObj);  
112         if (SUCCEEDED(hr))  
113         {  
114             //初始化vtProp值  
115             VariantInit(&vtProp);  
116             uReturn=0;  
117 
118             //返回从当前位置起的第一个对象到m_pWbemClsObj中  
119             hr=m_pEnumClsObj->Next(WBEM_INFINITE,1,&m_pWbemClsObj,&uReturn);  
120             if(SUCCEEDED(hr)&&uReturn>0)   
121             {  
122                 //从m_pWbemClsObj中找出ClassMember标识的成员属性值,并保存到vtProp变量中  
123                 hr=m_pWbemClsObj->Get(CComBSTR(ClassMember),0,&vtProp,0,0);  
124                 if (SUCCEEDED(hr))  
125                 {  
126                     VariantToString(&vtProp,chRetValue);  
127                     VariantClear(&vtProp);//清空vtProp  
128                     bRet = TRUE;  
129                 }  
130             }  
131         }  
132     }  
133     if(NULL != m_pEnumClsObj)   
134     {  
135         hr=m_pEnumClsObj->Release();  
136         m_pEnumClsObj = NULL;  
137     }  
138     if(NULL != m_pWbemClsObj)   
139     {  
140         hr=m_pWbemClsObj->Release();  
141         m_pWbemClsObj = NULL;  
142     }  
143     return bRet;  
144 }  
145 
146 BOOL CWmiInfo::GetGroupItemInfo(CString ClassName,CString ClassMember[],int n,CString &chRetValue)  
147 {  
148     USES_CONVERSION;  
149 
150     CComBSTR query("SELECT * FROM ");  
151     CString result,info;  
152     VARIANT vtProp;  
153     ULONG uReturn;  
154     HRESULT hr;  
155     int i;  
156     BOOL bRet = FALSE;  
157     if (NULL  != m_pWbemSvc)  
158     {  
159         query+=CComBSTR(ClassName);  
160         hr=m_pWbemSvc->ExecQuery(CComBSTR("WQL"),query,WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY,0,&m_pEnumClsObj);  
161         if (SUCCEEDED(hr))  
162         {  
163             VariantInit(&vtProp); //初始化vtProp变量  
164             if(m_pEnumClsObj)    
165             {  
166                 Sleep(10);  
167                 uReturn=0;  
168                 hr=m_pEnumClsObj->Next(WBEM_INFINITE,1,&m_pWbemClsObj,&uReturn);  
169                 if (SUCCEEDED(hr) &&uReturn>0)  
170                 {  
171                     for(i=0;i<n;++i)  
172                     {  
173                         hr=m_pWbemClsObj->Get(CComBSTR(ClassMember[i]),0,&vtProp,0,0);  
174                         if (SUCCEEDED(hr))  
175                         {  
176                             VariantToString(&vtProp,info);  
177                             chRetValue+=info+_T("\t");  
178                             VariantClear(&vtProp);  
179                             bRet = TRUE;  
180                         }  
181                     }  
182                     chRetValue+=_T("\r\n");  
183                 }  
184             }  
185         }  
186     }  
187 
188     if(NULL != m_pEnumClsObj)  
189     {  
190         hr=m_pEnumClsObj->Release();  
191         m_pEnumClsObj=NULL;  
192     }  
193     if(NULL != m_pWbemClsObj)  
194     {  
195         hr=m_pWbemClsObj->Release();  
196         m_pWbemClsObj=NULL;  
197     }  
198     return bRet;  
199 }  
200 
201 void CWmiInfo::VariantToString(const LPVARIANT pVar,CString &chRetValue) const  
202 {  
203     USES_CONVERSION;  
204 
205     CComBSTR HUGEP* pBstr;  
206     BYTE HUGEP* pBuf;  
207     LONG low,high,i;  
208     HRESULT hr;  
209 
210     switch(pVar->vt)  
211     {  
212     case VT_BSTR:  
213         {  
214             chRetValue=W2T(pVar->bstrVal);  
215         }  
216         break;  
217     case VT_BOOL:  
218         {  
219             if(VARIANT_TRUE==pVar->boolVal)   
220                 chRetValue="";  
221             else  
222                 chRetValue="";  
223         }  
224         break;  
225     case VT_I4:  
226         {  
227             chRetValue.Format(_T("%d"),pVar->lVal);  
228         }  
229         break;  
230     case VT_UI1:  
231         {  
232             chRetValue.Format(_T("%d"),pVar->bVal);  
233         }  
234         break;  
235     case VT_UI4:  
236         {  
237             chRetValue.Format(_T("%d"),pVar->ulVal);  
238         }  
239         break;  
240 
241     case VT_BSTR|VT_ARRAY:  
242         {  
243             hr=SafeArrayAccessData(pVar->parray,(void HUGEP**)&pBstr);  
244             hr=SafeArrayUnaccessData(pVar->parray);  
245             chRetValue=W2T(pBstr->m_str);  
246         }  
247         break;  
248 
249     case VT_I4|VT_ARRAY:  
250         {  
251             SafeArrayGetLBound(pVar->parray,1,&low);   
252             SafeArrayGetUBound(pVar->parray,1,&high);  
253 
254             hr=SafeArrayAccessData(pVar->parray,(void HUGEP**)&pBuf);  
255             hr=SafeArrayUnaccessData(pVar->parray);  
256             CString strTmp;  
257             high=min(high,MAX_PATH*2-1);  
258             for(i=low;i<=high;++i)  
259             {  
260                 strTmp.Format(_T("%02X"),pBuf[i]);  
261                 chRetValue+=strTmp;  
262             }  
263         }  
264         break;  
265     default:  
266         break;  
267     }  
268 }

调用

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include "WMIInfo.h"
 4 
 5 int main()
 6 {
 7     CWmiInfo WMI;
 8     WMI.InitWmi();
 9 
10     // 网卡原生MAC地址
11     CString strNetwork;
12     WMI.GetSingleItemInfo(L"Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE ‘ROOT%‘))",L"PNPDeviceID",strNetwork);
13     if (!strNetwork.IsEmpty())
14     {
15         wcout << "网卡原生MAC地址: "<< strNetwork.GetBuffer() << endl;
16     }
17     
18     // 硬盘序列号
19     CString strDiskDrive;
20     WMI.GetSingleItemInfo(L"Win32_DiskDrive WHERE (SerialNumber IS NOT NULL) AND (MediaType LIKE ‘Fixed hard disk%‘)",L"SerialNumber",strDiskDrive);
21 
22     if (!strDiskDrive.IsEmpty())
23     {
24     wcout << "硬盘序列号 : " << strDiskDrive.GetBuffer() << endl;
25     }
26 
27     // 主板序列号 
28     CString strBaseBoard;
29     WMI.GetSingleItemInfo(L"Win32_BaseBoard WHERE (SerialNumber IS NOT NULL)",L"SerialNumber",strBaseBoard);
30 
31     if (!strBaseBoard.IsEmpty())
32     {
33         wcout << "主板序列号 : " << strBaseBoard.GetBuffer() << endl;
34     }
35 
36     // 处理器ID  
37     CString strProcessorID;
38     WMI.GetSingleItemInfo(L"Win32_Processor WHERE (ProcessorId IS NOT NULL)",L"ProcessorId",strProcessorID);
39 
40     if (!strProcessorID.IsEmpty())
41     {
42         wcout << "处理器ID : " << strProcessorID.GetBuffer() << endl;
43     }
44 
45     // BIOS序列号
46     CString strBIOS;
47     WMI.GetSingleItemInfo(L"Win32_BIOS WHERE (SerialNumber IS NOT NULL)",L"SerialNumber",strBIOS);
48 
49     if (!strBIOS.IsEmpty())
50     {
51         wcout << "BIOS序列号 : " << strBIOS.GetBuffer() << endl;
52     }
53 
54     // 主板型号
55     CString strBaseBoardType;
56     WMI.GetSingleItemInfo(L"Win32_BaseBoard WHERE (Product IS NOT NULL)",L"Product",strBaseBoardType);
57 
58     if (!strBaseBoardType.IsEmpty())
59     {
60         wcout << "主板型号 : " << strBaseBoardType.GetBuffer() << endl;
61     }
62 
63     // 网卡当前MAC地址
64     CString strCurrentNetwork;
65     WMI.GetSingleItemInfo(L"Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE ‘ROOT%‘))",L"MACAddress",strCurrentNetwork);
66 
67     if (!strCurrentNetwork.IsEmpty())
68     {
69         wcout << "网卡当前MAC地址: " << strCurrentNetwork.GetBuffer() << endl;
70     }
71     WMI.ReleaseWmi();
72 
73     getchar();
74     return 0;
75 }

 

C/C++通过WMI和系统API函数获取获取系统硬件配置信息

标签:

原文地址:http://www.cnblogs.com/chechen/p/5946752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!