Fetching hardware info using WQL
PUBLISHED
FILE_ID: FETCHING HARDWARE INFO USING WQL

Fetching hardware info using WQL

Willian Frantz
Feb 22, 2026
PerformanceOptimizationWeb VitalsMonitoring

Fetch hardware info using WQL

hardwares.cpp

1#include <windows.h> 2#include <wbemidl.h> 3#include <comdef.h> 4#include <iostream> 5#include <string> 6#include <iomanip> 7 8#pragma comment(lib, "wbemuuid.lib") 9 10void PrintWMIProperty(IWbemClassObject* pclsObj, const wchar_t* propName) { 11 VARIANT vtProp; 12 if (SUCCEEDED(pclsObj->Get(propName, 0, &vtProp, nullptr, nullptr))) { 13 if (vtProp.vt == VT_BSTR && vtProp.bstrVal) { 14 std::wcout << propName << L": " << vtProp.bstrVal << std::endl; 15 } 16 else if (vtProp.vt == VT_I4) { 17 std::wcout << propName << L": " << vtProp.lVal << std::endl; 18 } 19 else if (vtProp.vt == VT_UI4) { 20 std::wcout << propName << L": " << vtProp.ulVal << std::endl; 21 } 22 else if (vtProp.vt == VT_BOOL) { 23 std::wcout << propName << L": " << (vtProp.boolVal ? L"Yes" : L"No") << std::endl; 24 } 25 VariantClear(&vtProp); 26 } 27} 28 29void QueryWMI(const wchar_t* wqlQuery) { 30 HRESULT hres; 31 32 hres = CoInitializeEx(nullptr, COINIT_MULTITHREADED); 33 if (FAILED(hres)) return; 34 35 hres = CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_DEFAULT, 36 RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE, nullptr); 37 if (FAILED(hres)) { 38 CoUninitialize(); 39 return; 40 } 41 42 IWbemLocator* pLoc = nullptr; 43 hres = CoCreateInstance(CLSID_WbemLocator, nullptr, CLSCTX_INPROC_SERVER, 44 IID_IWbemLocator, (LPVOID*)&pLoc); 45 if (FAILED(hres)) { 46 CoUninitialize(); 47 return; 48 } 49 50 IWbemServices* pSvc = nullptr; 51 hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), nullptr, nullptr, nullptr, 0, nullptr, nullptr, &pSvc); 52 if (FAILED(hres)) { 53 pLoc->Release(); 54 CoUninitialize(); 55 return; 56 } 57 58 hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, 59 RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE); 60 if (FAILED(hres)) { 61 pSvc->Release(); 62 pLoc->Release(); 63 CoUninitialize(); 64 return; 65 } 66 67 IEnumWbemClassObject* pEnumerator = nullptr; 68 hres = pSvc->ExecQuery(_bstr_t(L"WQL"), _bstr_t(wqlQuery), 69 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 70 nullptr, &pEnumerator); 71 if (FAILED(hres)) { 72 pSvc->Release(); 73 pLoc->Release(); 74 CoUninitialize(); 75 return; 76 } 77 78 IWbemClassObject* pclsObj = nullptr; 79 ULONG uReturn = 0; 80 int index = 1; 81 82 while (pEnumerator) { 83 hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 84 if (uReturn == 0) break; 85 86 std::wcout << L"\n" << L"[" << index++ << L"]" << std::endl; 87 88 // Print common useful properties (adjust per class) 89 PrintWMIProperty(pclsObj, L"Name"); 90 PrintWMIProperty(pclsObj, L"Description"); 91 PrintWMIProperty(pclsObj, L"Manufacturer"); 92 PrintWMIProperty(pclsObj, L"Model"); 93 PrintWMIProperty(pclsObj, L"DeviceID"); 94 PrintWMIProperty(pclsObj, L"PNPDeviceID"); 95 PrintWMIProperty(pclsObj, L"MaxClockSpeed"); 96 PrintWMIProperty(pclsObj, L"NumberOfCores"); 97 PrintWMIProperty(pclsObj, L"Capacity"); 98 PrintWMIProperty(pclsObj, L"Size"); 99 PrintWMIProperty(pclsObj, L"InterfaceType"); 100 PrintWMIProperty(pclsObj, L"BusType"); 101 102 pclsObj->Release(); 103 } 104 105 pEnumerator->Release(); 106 pSvc->Release(); 107 pLoc->Release(); 108 CoUninitialize(); 109} 110 111int main() { 112 SetConsoleOutputCP(CP_UTF8); 113 114 std::wcout << L"=== CPU (Win32_Processor) ===\n"; 115 QueryWMI(L"SELECT * FROM Win32_Processor"); 116 117 std::wcout << L"\n=== GPU / Video Controllers (Win32_VideoController) ===\n"; 118 QueryWMI(L"SELECT * FROM Win32_VideoController"); 119 120 std::wcout << L"\n=== Motherboard / Base Board (Win32_BaseBoard) ===\n"; 121 QueryWMI(L"SELECT * FROM Win32_BaseBoard"); 122 123 std::wcout << L"\n=== RAM / Physical Memory (Win32_PhysicalMemory) ===\n"; 124 QueryWMI(L"SELECT * FROM Win32_PhysicalMemory"); 125 126 std::wcout << L"\n=== Storage Drives (Win32_DiskDrive) ===\n"; 127 QueryWMI(L"SELECT * FROM Win32_DiskDrive"); 128 129 std::wcout << L"\n=== PCIe / PCI Devices (Win32_PnPEntity filtered by PCI) ===\n"; 130 131 QueryWMI(L"SELECT * FROM Win32_PnPEntity WHERE DeviceID LIKE 'PCI\\\\%'"); 132 133 std::wcout << L"\n=== System Slots (PCIe slots etc.) (Win32_SystemSlot) ===\n"; 134 QueryWMI(L"SELECT * FROM Win32_SystemSlot"); 135 136 std::wcout << L"\nPress Enter to exit...\n"; 137 std::cin.get(); 138 return 0; 139}
END_OF_FILE • ARTICLE_COMPLETE