通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

转载:https://www.cnblogs.com/tlduck/p/5132738.html

  1 #define _WIN32_DCOM
  2 
  3 #include<iostream>
  4 #include<fstream>
  5 #include<string>
  6 #include "direct.h"
  7 #include <tchar.h>
  8 #include <time.h>
  9 #include <comdef.h>
 10 #include <Wbemidl.h>
 11 #include <conio.h>
 12 #include "atlstr.h"
 13 #include "atlbase.h"
 14 //#include "TcpCtl.h"
 15 //#include "winsock2.h"
 16 //#include "InitDll.h"
 17 using namespace std;
 18 
 19 # pragma comment(lib, "wbemuuid.lib")
 20 # pragma comment(lib, "ws2_32.lib")
 21 
 22 //通过WMI获取主板号
 23 BOOL ManageWMIBord(char bord[])
 24 {
 25     HRESULT hres;
 26     // Step 1:  初始化COM 
 27     //hres = CoInitializeEx(0, COINIT_MULTITHREADED); //网上的代码都是使用这行语句进行初始化,但是我在实际使用中,发现也可以采用下面的语句进行初始化
 28     hres = CoInitialize(0);
 29 
 30     //网上的代码是没有注释下面这个判断的,但是实际使用中发现,如果之前已经初始化成功了,在第二次初始化的时候,下面的代码就会导致返回false,所以,实际使用中我就注释掉了
 31     //if (FAILED(hres))
 32     //{
 33     // cout << "Failed to initialize COM library. Error code = 0x" 
 34     // << hex << hres << endl;
 35     // return false; // Program has failed.
 36     //}
 37 
 38 
 39     // Step 2:  设置COM的安全认证级别  
 40 
 41     //在实际使用过程中,我发现如果这一步不注释掉的话,程序总是返回false,注释掉之后程序反而可以正常运行,原因未知
 42 
 43     // Note: If you are using Windows 2000, you need to specify -
 44     // the default authentication credentials for a user by using
 45     // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
 46     // parameter of CoInitializeSecurity ------------------------
 47     ////hres = CoInitializeSecurity(
 48     //// NULL, 
 49     //// -1, // COM authentication
 50     //// NULL, // Authentication services
 51     //// NULL, // Reserved
 52     //// RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication 
 53     //// RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation 
 54     //// NULL, // Authentication info
 55     //// EOAC_NONE, // Additional capabilities 
 56     //// NULL // Reserved
 57     //// );
 58     //// 
 59     ////if (FAILED(hres))
 60     ////{
 61     //// cout << "Failed to initialize security. Error code = 0x" 
 62     //// << hex << hres << endl;
 63     //// CoUninitialize();
 64     //// return false; // Program has failed.
 65     ////}
 66 
 67     // Step 3:  获得WMI连接COM接口  
 68     IWbemLocator *pLoc = NULL;
 69     hres = CoCreateInstance(
 70         CLSID_WbemLocator,
 71         0,
 72         CLSCTX_INPROC_SERVER,
 73         IID_IWbemLocator, (LPVOID *)&pLoc);
 74 
 75     if (FAILED(hres))
 76     {
 77         cout << "Failed to create IWbemLocator object."
 78             << " Err code = 0x"
 79             << hex << hres << endl;
 80         CoUninitialize();
 81         return false; // Program has failed.
 82     }
 83 
 84 
 85     // Step 4:  通过连接接口连接WMI的内核对象名"ROOT//CIMV2"  
 86     IWbemServices *pSvc = NULL;
 87 
 88     hres = pLoc->ConnectServer(
 89 
 90         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
 91         NULL, // User name. NULL = current user
 92         NULL, // User password. NULL = current
 93         0, // Locale. NULL indicates current
 94         NULL, // Security flags.
 95         0, // Authority (e.g. Kerberos)
 96         0, // Context object 
 97         &pSvc // pointer to IWbemServices proxy
 98         );
 99 
100     if (FAILED(hres))
101     {
102         cout << "Could not connect. Error code = 0x"
103             << hex << hres << endl;
104         pLoc->Release();
105         CoUninitialize();
106         return false; // Program has failed.
107     }
108     //cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
109 
110     // Step 5:  设置请求代理的安全级别   
111     hres = CoSetProxyBlanket(
112         pSvc, // Indicates the proxy to set
113         RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
114         RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
115         NULL, // Server principal name 
116         RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx 
117         RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
118         NULL, // client identity
119         EOAC_NONE // proxy capabilities 
120         );
121     if (FAILED(hres))
122     {
123         cout << "Could not set proxy blanket. Error code = 0x"
124             << hex << hres << endl;
125         pSvc->Release();
126         pLoc->Release();
127         CoUninitialize();
128         return false; // Program has failed.
129     }
130     // Step 6: 通过请求代理来向WMI发送请求----
131     // For example, get the name of the operating system
132     IEnumWbemClassObject* pEnumerator = NULL;
133     hres = pSvc->ExecQuery(
134         bstr_t("WQL"),
135         //bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = ‘TRUE‘"), 
136         bstr_t("SELECT * FROM Win32_BaseBoard"),//只需要通过修改这里的查询语句,就可以实现对MAC地址等其他信息的查询
137         WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
138         NULL,
139         &pEnumerator);
140 
141     if (FAILED(hres))
142     {
143         cout << "Query for Network Adapter Configuration failed."
144             << " Error code = 0x"
145             << hex << hres << endl;
146         pSvc->Release();
147         pLoc->Release();
148         CoUninitialize();
149         return false; // Program has failed.
150     }
151 
152 
153     // Step 7:  循环枚举所有的结果对象
154 
155     IWbemClassObject *pclsObj;
156     pclsObj = NULL;
157     ULONG uReturn = 0;
158 
159     while (pEnumerator)
160     {
161         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
162             &pclsObj, &uReturn);
163         if (0 == uReturn)
164         {
165             break;
166         }
167         VARIANT vtProp;
168         VariantInit(&vtProp);
169 
170         hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);//查询不同的硬件信息,除了修改上面的查询语句,这里的字段也要修改
171 
172         if (!FAILED(hr))
173         {
174             CW2A tmpstr1(vtProp.bstrVal);
175             strcpy_s(bord, 200, tmpstr1);//这里的200是可调的,自己根据实际情况设置,但是肯定不能大于bord的长度
176             //cout << "BordSN:" << sn << endl;
177 
178         }
179 
180         VariantClear(&vtProp);
181     }//end while
182 
183     // 释放资源  
184     pSvc->Release();
185     pLoc->Release();
186     pEnumerator->Release();
187     pclsObj->Release();
188     CoUninitialize();
189     return true; // Program successfully completed.
190 
191 }
192 
193 int main()
194 {
195     char bord[300];
196     ManageWMIBord(bord);
197     cout << bord << "\n";
198     system("pause");
199     return 0;
200 }

 

运行结果:

通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

 

通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号

上一篇:[Contract] Solidity 判断 mapping 值的存在


下一篇:使用HBuilderX实现打包vue项目成app