#include#include #include #include
当前位置:   article > 正文

VS 2019 MFC 通过WMI技术读取计算机CPU序列号,硬盘序列号,BIOS序列号,主板序列号,系统名称以及网卡MAC地址_mfc 读取cpu 序列号

mfc 读取cpu 序列号

 经行了简单的封装,江功能全部封装到了类文件。

 封装类文件H头文件

  1. #ifndef _H_GETSYSTEMHARDWAREID
  2. #define _H_GETSYSTEMHARDWAREID
  3. #include"targetver.h"
  4. #include<wbemcli.h>
  5. #include<string.h>
  6. #include <iostream>
  7. #include <Windows.h>
  8. #include <comdef.h>
  9. #include <Wbemidl.h>
  10. #include "wincred.h"
  11. #pragma comment(lib, "credui.lib")
  12. #pragma comment(lib,"wbemuuid.lib")
  13. using namespace std;
  14. #pragma once
  15. class GetSysTemHardWareID
  16. {
  17. public:
  18. virtual ~GetSysTemHardWareID();
  19. GetSysTemHardWareID(wchar_t* WMI_Namespace = L"ROOT\\CIMV2");
  20. public:
  21. void Get_CpuId(CString& dest);
  22. void Get_DiskDriveId(CString& dest);
  23. void Get_MacAddress(CString& dest);
  24. void Get_OsName(CString& dest);
  25. void Get_BaseBoardId(CString &dest);
  26. void Get_BiosSerialId(CString &dest);
  27. HRESULT hres;
  28. private:
  29. IWbemServices* pSvc;
  30. IWbemLocator* pLoc;
  31. bool ExecQueryValue(char* strQueryLanguage, char* strQuery, IEnumWbemClassObject** pEnumerator);
  32. private:
  33. protected:
  34. };
  35. #endif // !_H_GETSYSTEMHARDWAREID

封装类CPP实现文件

  1. #include "pch.h"
  2. #include "GetSysTemHardWareID.h"
  3. #include <vector>
  4. #include <iostream>
  5. #include <list>
  6. using namespace std;
  7. GetSysTemHardWareID::GetSysTemHardWareID(wchar_t* WMI_Namespace)
  8. {
  9. pSvc = NULL;
  10. pLoc = NULL;
  11. hres = NULL;
  12. hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  13. if (FAILED(hres))
  14. {
  15. printf("Failed to initialize COM library. Error code = 0x");
  16. }
  17. hres = CoInitializeSecurity(
  18. NULL,
  19. -1,
  20. NULL,
  21. NULL,
  22. RPC_C_AUTHN_LEVEL_DEFAULT,
  23. RPC_C_IMP_LEVEL_IMPERSONATE,
  24. NULL,
  25. EOAC_NONE,
  26. NULL
  27. );
  28. if (FAILED(hres))
  29. {
  30. printf("Failed to initialize security. Error code = 0x");
  31. CoUninitialize();
  32. }
  33. hres = CoCreateInstance(
  34. CLSID_WbemLocator,
  35. 0,
  36. CLSCTX_INPROC_SERVER,
  37. IID_IWbemLocator, (LPVOID *) &pLoc);
  38. if (FAILED(hres))
  39. {
  40. printf("Failed to create IWbemLocator object.");
  41. CoUninitialize();
  42. }
  43. hres = pLoc->ConnectServer(
  44. _bstr_t(WMI_Namespace),
  45. NULL,
  46. NULL,
  47. 0,
  48. NULL,
  49. 0,
  50. 0,
  51. &pSvc
  52. );
  53. if (FAILED(hres))
  54. {
  55. printf("Could not connect. Error code = 0x");
  56. pLoc->Release();
  57. CoUninitialize();
  58. }
  59. printf ("Connected to ROOT\\CIMV2 WMI namespace") ;
  60. hres = CoSetProxyBlanket(
  61. pSvc,
  62. RPC_C_AUTHN_WINNT,
  63. RPC_C_AUTHZ_NONE,
  64. NULL,
  65. RPC_C_AUTHN_LEVEL_CALL,
  66. RPC_C_IMP_LEVEL_IMPERSONATE,
  67. NULL,
  68. EOAC_NONE
  69. );
  70. if (FAILED(hres))
  71. {
  72. printf("Could not set proxy blanket. Error code = 0x");
  73. pSvc->Release();
  74. pLoc->Release();
  75. CoUninitialize();
  76. }
  77. }
  78. GetSysTemHardWareID::~GetSysTemHardWareID()
  79. {
  80. pSvc->Release();
  81. pLoc->Release();
  82. CoUninitialize();
  83. hres = NULL;
  84. }
  85. void GetSysTemHardWareID::Get_CpuId(CString& dest)
  86. {
  87. IEnumWbemClassObject* pEnumerator = NULL;
  88. ExecQueryValue("WQL", "SELECT * FROM Win32_Processor", &pEnumerator);
  89. IWbemClassObject* pclsObj = NULL;
  90. ULONG uReturn = 0;
  91. while (pEnumerator)
  92. {
  93. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  94. &pclsObj, &uReturn);
  95. if (0 == uReturn)
  96. {
  97. break;
  98. }
  99. VARIANT vtProp;
  100. vtProp.vt = VT_BSTR;
  101. hr = pclsObj->Get(L"ProcessorId", 0, &vtProp, 0, 0);
  102. printf("ProcessorId:\n%ws", vtProp.bstrVal);
  103. printf("\n");
  104. dest = vtProp.bstrVal;
  105. VariantClear(&vtProp);
  106. pclsObj->Release();
  107. }
  108. pEnumerator->Release();
  109. }
  110. void GetSysTemHardWareID::Get_DiskDriveId(CString& dest)
  111. {
  112. IEnumWbemClassObject* pEnumerator = NULL;
  113. ExecQueryValue("WQL", "SELECT * FROM Win32_DiskDrive WHERE (SerialNumber IS NOT NULL) AND (MediaType LIKE 'Fixed hard disk%')", &pEnumerator);
  114. IWbemClassObject* pclsObj = NULL;
  115. ULONG uReturn = 0;
  116. int i = 0;
  117. CString strN;
  118. while (pEnumerator)
  119. {
  120. i++;
  121. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  122. &pclsObj, &uReturn);
  123. if (0 == uReturn)
  124. {
  125. break;
  126. }
  127. VARIANT vtProp;
  128. vtProp.vt = VT_BSTR;
  129. hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0,0);
  130. strN = strN + vtProp.bstrVal+_T(";");
  131. dest = vtProp.bstrVal;
  132. dest.Replace(_T(" "), _T(""));
  133. strN.Replace(_T(" "),_T(""));
  134. printf("SerialNumber[%d]:\n%ws", i, dest.GetString());
  135. printf("\n");
  136. VariantClear(&vtProp);
  137. pclsObj->Release();
  138. }
  139. pEnumerator->Release();
  140. i = 0;
  141. dest = strN;
  142. }
  143. void GetSysTemHardWareID::Get_MacAddress(CString& dest)
  144. {
  145. IEnumWbemClassObject* pEnumerator = NULL;
  146. ExecQueryValue("WQL", "SELECT * FROM Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%'))", &pEnumerator);
  147. IWbemClassObject* pclsObj = NULL;
  148. ULONG uReturn = 0;
  149. vector<CString> MACAddress;
  150. while (pEnumerator)
  151. {
  152. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  153. &pclsObj, &uReturn);
  154. if (0 == uReturn)
  155. {
  156. break;
  157. }
  158. VARIANT vtProp;
  159. VARIANT vtProp2;
  160. vtProp.vt = VT_BSTR;
  161. vtProp2.vt = VT_BSTR;
  162. hr = pclsObj->Get(L"Description", 0, &vtProp2, 0, 0);
  163. printf("Description:\n%ws", vtProp2.bstrVal);
  164. printf("\n");
  165. dest = vtProp2.bstrVal;
  166. dest += _T(" ; ");
  167. pclsObj->Get(L"MACAddress", 0, &vtProp, 0, 0);
  168. MACAddress.push_back((CString)(vtProp.bstrVal));
  169. printf("MACAddress:\n%ws", vtProp.bstrVal);
  170. printf("\n");
  171. VariantClear(&vtProp);
  172. pclsObj->Release();
  173. break;
  174. }
  175. if (MACAddress.size() >= 1)
  176. {
  177. dest= dest+MACAddress[0];
  178. }
  179. /* else if (MACAddress.size()>1)
  180. {
  181. for (size_t i = 0; i < MACAddress.size(); i++)
  182. {
  183. dest = dest + MACAddress[i];
  184. }
  185. }*/
  186. MACAddress.clear();
  187. pEnumerator->Release();
  188. }
  189. void GetSysTemHardWareID::Get_OsName(CString& dest)
  190. {
  191. IEnumWbemClassObject* pEnumerator=NULL ;
  192. ExecQueryValue("WQL", "SELECT * FROM Win32_OperatingSystem", &pEnumerator);
  193. IWbemClassObject* pclsObj = NULL;
  194. ULONG uReturn = 0;
  195. while (pEnumerator)
  196. {
  197. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  198. &pclsObj, &uReturn);
  199. if (0 == uReturn)
  200. {
  201. break;
  202. }
  203. VARIANT vtProp;
  204. vtProp.vt = VT_BSTR;
  205. hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
  206. printf("OS NAME:\n%ws", vtProp.bstrVal);
  207. printf("\n");
  208. dest = vtProp.bstrVal;
  209. VariantClear(&vtProp);
  210. pclsObj->Release();
  211. break;
  212. }
  213. pEnumerator->Release();
  214. }
  215. void GetSysTemHardWareID::Get_BaseBoardId(CString &dest)
  216. {
  217. IEnumWbemClassObject* pEnumerator = NULL;
  218. ExecQueryValue("WQL", "SELECT * FROM Win32_BaseBoard WHERE (SerialNumber IS NOT NULL)", &pEnumerator);
  219. IWbemClassObject* pclsObj = NULL;
  220. ULONG uReturn = 0;
  221. while (pEnumerator)
  222. {
  223. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  224. &pclsObj, &uReturn);
  225. if (0 == uReturn)
  226. {
  227. break;
  228. }
  229. VARIANT vtProp;
  230. vtProp.vt = VT_BSTR;
  231. hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
  232. printf("Board SerialNumber:\n%ws", vtProp.bstrVal);
  233. printf("\n");
  234. dest += _T("BaseBoardId:");
  235. dest += vtProp.bstrVal;
  236. VariantClear(&vtProp);
  237. pclsObj->Release();
  238. break;
  239. }
  240. pEnumerator->Release();
  241. }
  242. void GetSysTemHardWareID::Get_BiosSerialId(CString& dest)
  243. {
  244. IEnumWbemClassObject* pEnumerator = NULL;
  245. ExecQueryValue("WQL", "SELECT * FROM Win32_BIOS WHERE SerialNumber IS NOT NULL", &pEnumerator);
  246. IWbemClassObject* pclsObj = NULL;
  247. ULONG uReturn = 0;
  248. while (pEnumerator)
  249. {
  250. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  251. &pclsObj, &uReturn);
  252. if (0 == uReturn)
  253. {
  254. break;
  255. }
  256. VARIANT vtProp;
  257. vtProp.vt = VT_BSTR;
  258. hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
  259. printf("BIOS SerialNumber:\n%ws", vtProp.bstrVal);
  260. printf("\n");
  261. dest += _T("BIOSSerialId:");
  262. dest += vtProp.bstrVal;
  263. VariantClear(&vtProp);
  264. pclsObj->Release();
  265. }
  266. pEnumerator->Release();
  267. }
  268. bool GetSysTemHardWareID::ExecQueryValue(char* strQueryLanguage, char* strQuery, IEnumWbemClassObject** pEnumerator)
  269. {
  270. HRESULT hre = pSvc->ExecQuery(
  271. bstr_t(strQueryLanguage),
  272. bstr_t(strQuery),
  273. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  274. NULL,
  275. pEnumerator);
  276. if (FAILED(hres))
  277. {
  278. printf("Query for operating system name failed.");
  279. pSvc->Release();
  280. pLoc->Release();
  281. CoUninitialize();
  282. return FALSE;
  283. }
  284. return true;
  285. }

重要说明:本例程参考《MSDN Library for Visual Studio 2008 SP1 简体中文》类库文件说明,如下为《MSDN Library for Visual Studio 2008 SP1 简体中文》中对WMI技术的说明,在如下说明的第6步和第7步为读取PC机系统名称例程。

  1. #define _WIN32_DCOM
  2. #include <iostream>
  3. using namespace std;
  4. #include <comdef.h>
  5. #include <Wbemidl.h>
  6. # pragma comment(lib, "wbemuuid.lib")
  7. int main(int argc, char **argv)
  8. {
  9. HRESULT hres;
  10. // Step 1: --------------------------------------------------
  11. // Initialize COM. ------------------------------------------
  12. hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  13. if (FAILED(hres))
  14. {
  15. cout << "Failed to initialize COM library. Error code = 0x"
  16. << hex << hres << endl;
  17. return 1; // Program has failed.
  18. }
  19. // Step 2: --------------------------------------------------
  20. // Set general COM security levels --------------------------
  21. // Note: If you are using Windows 2000, you need to specify -
  22. // the default authentication credentials for a user by using
  23. // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
  24. // parameter of CoInitializeSecurity ------------------------
  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. cout << "Failed to initialize security. Error code = 0x"
  39. << hex << hres << endl;
  40. CoUninitialize();
  41. return 1; // Program has failed.
  42. }
  43. // Step 3: ---------------------------------------------------
  44. // Obtain the initial locator to WMI -------------------------
  45. IWbemLocator *pLoc = NULL;
  46. hres = CoCreateInstance(
  47. CLSID_WbemLocator,
  48. 0,
  49. CLSCTX_INPROC_SERVER,
  50. IID_IWbemLocator, (LPVOID *) &pLoc);
  51. if (FAILED(hres))
  52. {
  53. cout << "Failed to create IWbemLocator object."
  54. << " Err code = 0x"
  55. << hex << hres << endl;
  56. CoUninitialize();
  57. return 1; // Program has failed.
  58. }
  59. // Step 4: -----------------------------------------------------
  60. // Connect to WMI through the IWbemLocator::ConnectServer method
  61. IWbemServices *pSvc = NULL;
  62. // Connect to the root\cimv2 namespace with
  63. // the current user and obtain pointer pSvc
  64. // to make IWbemServices calls.
  65. hres = pLoc->ConnectServer(
  66. _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
  67. NULL, // User name. NULL = current user
  68. NULL, // User password. NULL = current
  69. 0, // Locale. NULL indicates current
  70. NULL, // Security flags.
  71. 0, // Authority (e.g. Kerberos)
  72. 0, // Context object
  73. &pSvc // pointer to IWbemServices proxy
  74. );
  75. if (FAILED(hres))
  76. {
  77. cout << "Could not connect. Error code = 0x"
  78. << hex << hres << endl;
  79. pLoc->Release();
  80. CoUninitialize();
  81. return 1; // Program has failed.
  82. }
  83. cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
  84. // Step 5: --------------------------------------------------
  85. // Set security levels on the proxy -------------------------
  86. hres = CoSetProxyBlanket(
  87. pSvc, // Indicates the proxy to set
  88. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  89. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  90. NULL, // Server principal name
  91. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  92. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  93. NULL, // client identity
  94. EOAC_NONE // proxy capabilities
  95. );
  96. if (FAILED(hres))
  97. {
  98. cout << "Could not set proxy blanket. Error code = 0x"
  99. << hex << hres << endl;
  100. pSvc->Release();
  101. pLoc->Release();
  102. CoUninitialize();
  103. return 1; // Program has failed.
  104. }
  105. // Step 6: --------------------------------------------------
  106. // Use the IWbemServices pointer to make requests of WMI ----
  107. // For example, get the name of the operating system
  108. IEnumWbemClassObject* pEnumerator = NULL;
  109. hres = pSvc->ExecQuery(
  110. bstr_t("WQL"),
  111. bstr_t("SELECT * FROM Win32_OperatingSystem"),
  112. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  113. NULL,
  114. &pEnumerator);
  115. if (FAILED(hres))
  116. {
  117. cout << "Query for operating system name failed."
  118. << " Error code = 0x"
  119. << hex << hres << endl;
  120. pSvc->Release();
  121. pLoc->Release();
  122. CoUninitialize();
  123. return 1; // Program has failed.
  124. }
  125. // Step 7: -------------------------------------------------
  126. // Get the data from the query in step 6 -------------------
  127. IWbemClassObject *pclsObj;
  128. ULONG uReturn = 0;
  129. while (pEnumerator)
  130. {
  131. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  132. &pclsObj, &uReturn);
  133. if(0 == uReturn)
  134. {
  135. break;
  136. }
  137. VARIANT vtProp;
  138. // Get the value of the Name property
  139. hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
  140. wcout << " OS Name : " << vtProp.bstrVal << endl;
  141. VariantClear(&vtProp);
  142. }
  143. // Cleanup
  144. // ========
  145. pSvc->Release();
  146. pLoc->Release();
  147. pEnumerator->Release();
  148. pclsObj->Release();
  149. CoUninitialize();
  150. return 0; // Program successfully completed.
  151. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/程序安全守护者/article/detail/61772
推荐阅读
相关标签