WMI 获取操作系统名称和版本
SysInfo.cpp
1 #pragma once 2 #include <string> 3 #include <iostream> 4 #include <windows.h> 5 #include <iphlpapi.h> 6 #include <comdef.h> 7 #include <Wbemidl.h> 8 #include <math.h> 9 10 class SysInfo 11 { 12 public: 13 SysInfo(); 14 virtual ~SysInfo(); 15 16 bool init_wmi(); 17 void uninit_wmi(); 18 19 20 double get_memory_size(); 21 std::string get_os_name(); 22 std::string get_host_name(); 23 int get_file_flag(std::string file_name); 24 std::string get_area(std::string ip); 25 std::string wstring_to_string(wchar_t* data); 26 27 28 protected: 29 private: 30 bool m_init_wmi = false; 31 32 IWbemServices *pSvc = NULL; 33 IWbemLocator *pLoc = NULL; 34 35 HRESULT hres = NULL; 36 }; 37
Sysinfo.cpp
1 #include "SysInfo.h" 2 3 4 5 #pragma comment(lib, "wbemuuid.lib") 6 7 #pragma comment(lib, "iphlpapi.lib") 8 9 #pragma warning(disable: 4996) // avoid GetVersionEx to be warned 10 11 12 #define GBYTES 1073741824 13 #define MBYTES 1048576 14 #define KBYTES 1024 15 #define DKBYTES 1024.0 16 17 SysInfo::SysInfo() 18 { 19 m_init_wmi = init_wmi(); 20 } 21 22 SysInfo::~SysInfo() 23 { 24 uninit_wmi(); 25 } 26 27 bool SysInfo::init_wmi() 28 { 29 30 hres = CoInitializeEx(0, COINIT_MULTITHREADED); 31 if (FAILED(hres)) 32 { 33 return false; 34 } 35 36 hres = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); 37 if (FAILED(hres)) 38 { 39 CoUninitialize(); 40 return false; 41 } 42 43 44 hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc); 45 if (FAILED(hres)) 46 { 47 CoUninitialize(); 48 return false; 49 } 50 51 52 hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc); 53 if (FAILED(hres)) 54 { 55 pLoc->Release(); 56 CoUninitialize(); 57 return false; 58 } 59 60 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); 61 if (FAILED(hres)) 62 { 63 pSvc->Release(); 64 pLoc->Release(); 65 CoUninitialize(); 66 return false; 67 } 68 return true; 69 } 70 71 void SysInfo::uninit_wmi() 72 { 73 74 pSvc->Release(); 75 pLoc->Release(); 76 CoUninitialize(); 77 } 78 79 80 //Win32_PhysicalMemory 81 82 double SysInfo::get_memory_size() 83 { 84 85 double mem_size = 0; 86 87 IEnumWbemClassObject* pEnumerator = NULL; 88 hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_PhysicalMemory") 89 , WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 90 if (FAILED(hres)) 91 { 92 pSvc->Release(); 93 pLoc->Release(); 94 CoUninitialize(); 95 return -1; 96 } 97 98 IWbemClassObject *pclsObj = NULL; 99 ULONG uReturn = 0; 100 while (pEnumerator) 101 { 102 HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 103 if (0 == uReturn) 104 { 105 break; 106 } 107 108 VARIANT vtProp; 109 hr = pclsObj->Get(L"Capacity", 0, &vtProp, 0, 0); 110 111 112 113 std::string data = wstring_to_string(vtProp.bstrVal); 114 115 mem_size += ::atof(data.c_str()); 116 VariantClear(&vtProp); 117 pclsObj->Release(); 118 } 119 pEnumerator->Release(); 120 121 double mem_total = mem_size / 1024 / 1024 / 1024; 122 123 return floor(mem_total * 100) / 100; 124 } 125 126 127 128 std::string SysInfo::get_os_name() 129 { 130 std::string res_data; 131 IEnumWbemClassObject* pEnumerator = NULL; 132 hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM win32_operatingsystem") 133 , WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 134 if (FAILED(hres)) 135 { 136 pSvc->Release(); 137 pLoc->Release(); 138 CoUninitialize(); 139 return res_data; 140 } 141 142 IWbemClassObject *pclsObj = NULL; 143 ULONG uReturn = 0; 144 while (pEnumerator) 145 { 146 HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 147 if (0 == uReturn) 148 { 149 break; 150 } 151 152 VARIANT vtProp; 153 hr = pclsObj->Get(L"name", 0, &vtProp, 0, 0); 154 std::string data = wstring_to_string(vtProp.bstrVal); 155 data = data.substr(0, data.find('\|')); 156 157 hr = pclsObj->Get(L"Version", 0, &vtProp, 0, 0); 158 res_data = wstring_to_string(vtProp.bstrVal); 159 std::string temp = "["; 160 temp += res_data; 161 temp += "] "; 162 temp += data; 163 res_data = temp; 164 //printf("%s\n", data.c_str()); 165 166 VariantClear(&vtProp); 167 pclsObj->Release(); 168 } 169 pEnumerator->Release(); 170 171 return res_data; 172 173 } 174 175 176 177 178 std::string SysInfo::get_host_name() 179 { 180 std::string host_name; 181 182 char buf[MAX_PATH] = { 0 }; 183 DWORD length = MAX_PATH; 184 if (::GetComputerNameA(buf, &length)) { 185 host_name = buf; 186 } 187 return host_name; 188 } 189 190 191 int SysInfo::get_file_flag(std::string file_name) 192 { 193 return 0; 194 } 195 196 std::string SysInfo::get_area(std::string ip) 197 { 198 std::string area; 199 200 return area; 201 } 202 203 std::string SysInfo::wstring_to_string(wchar_t* data) 204 { 205 std::string res_data; 206 int iSize; 207 char* pszMultiByte; 208 iSize = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL); 209 char* pCapacity = (char*)malloc(iSize * sizeof(char)); 210 pCapacity[0] = 0; 211 WideCharToMultiByte(CP_ACP, 0, data, -1, pCapacity, iSize, NULL, NULL); 212 res_data = pCapacity; 213 free(pCapacity); 214 return res_data; 215 } 216 217
PS:会笑的人,运气通常都会比别人好。
posted on 2020-09-15 18:48 thinkinc999 阅读(796) 评论(0) 编辑 收藏 举报