diff --git a/IWbemClassObject/main.cpp b/IWbemClassObject/main.cpp index 3582f53..ed0f078 100644 --- a/IWbemClassObject/main.cpp +++ b/IWbemClassObject/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #pragma comment(lib, "wbemuuid.lib") #include "query.cpp" @@ -7,8 +8,8 @@ using namespace std; int main(int argc, char** argv) { - -// Najde ldisky a zobrazi jejich SW serial no. + Sleep(1000); + // Najde ldisky a zobrazi jejich SW serial no. const char* drive_names[] = {"A:\\", "B:\\", "C:\\", "D:\\", "E:\\", "F:\\", "G:\\", "H:\\", "I:\\", "J:\\", "K:\\", "L:\\", "M:\\", "N:\\", "O:\\", "P:\\", "Q:\\", "R:\\", "S:\\", "T:\\", "U:\\", "V:\\", "W:\\", "X:\\", "Y:\\", "Z:\\"}; DWORD drives = GetLogicalDrives(); @@ -31,36 +32,123 @@ int main(int argc, char** argv) { } cout << endl; -// Najde hardware serial no a vyrobce vsech komponentu - cout << "--Win32_Processor--" << endl << "ProcessorID: " << GetHWIDs("Win32_Processor", L"ProcessorID") << endl; - cout << "UniqueID: " << GetHWIDs("Win32_Processor", L"UniqueID") << endl; - cout << "PNPDeviceID: " << GetHWIDs("Win32_Processor", L"PNPDeviceID") << endl; - cout << "Manufacturer: " << GetHWIDs("Win32_Processor", L"Manufacturer") << endl << endl; + // Najde hardware serial no a vyrobce vsech komponentu + cout << "--Win32_Processor--" << endl; + GetHWIDs("Win32_Processor", L"UniqueID"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tUniqueID: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_Processor", L"PNPDeviceID"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tPNPDeviceID: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_Processor", L"Manufacturer"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tManufacturer: " << arr[i] << endl; + } + EmptyRtVars(); + cout << endl; - cout << "--Win32_OnBoardDevice--" << endl << "SerialNumber: " << hex << GetHWIDs("Win32_OnBoardDevice", L"SerialNumber") << endl; - cout << "Manufacturer: " << GetHWIDs("Win32_OnBoardDevice", L"Manufacturer") << endl << endl; + cout << "--Win32_OnBoardDevice--" << endl; + GetHWIDs("Win32_OnBoardDevice", L"SerialNumber"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tSerialNumber: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_OnBoardDevice", L"Manufacturer"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tManufacturer: " << arr[i] << endl; + } + EmptyRtVars(); + cout << endl; - cout << "--Win32_BaseBoard--" << endl << "SerialNumber: " << GetHWIDs("Win32_BaseBoard", L"SerialNumber") << endl; - cout << "Manufacturer: " << GetHWIDs("Win32_BaseBoard", L"Manufacturer") << endl << endl; + cout << "--Win32_BaseBoard--" << endl; + GetHWIDs("Win32_BaseBoard", L"SerialNumber"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tSerialNumber: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_BaseBoard", L"Manufacturer"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tManufacturer: " << arr[i] << endl; + } + EmptyRtVars(); + cout << endl; - cout << "--Win32_BIOS--" << endl << "SerialNumber: " << GetHWIDs("Win32_BIOS", L"SerialNumber") << endl; - cout << "Manufacturer: " << GetHWIDs("Win32_BIOS", L"Manufacturer") << endl << endl; + cout << "--Win32_BIOS--" << endl; + GetHWIDs("Win32_BIOS", L"SerialNumber"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tSerialNumber: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_BIOS", L"Manufacturer"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tManufacturer: " << arr[i] << endl; + } + EmptyRtVars(); + cout << endl; - cout << "--Win32_NetworkAdapter--" << endl << "MACAddress: " << GetHWIDs("Win32_NetworkAdapter", L"MACAddress") << endl; - cout << "PNPDeviceID: " << GetHWIDs("Win32_NetworkAdapter", L"PNPDeviceID") << endl; - cout << "Manufacturer: " << GetHWIDs("Win32_NetworkAdapter", L"Manufacturer") << endl << endl; + cout << "--Win32_NetworkAdapter--" << endl; + GetHWIDs("Win32_NetworkAdapter", L"MACAddress"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i+1 << "]\tMACAddress: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_NetworkAdapter", L"Manufacturer"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i+1 << "]\tManufacturer: " << arr[i] << endl; + } + EmptyRtVars(); + cout << endl; + + cout << "--Win32_DiskDrive--" << endl; + GetHWIDs("Win32_DiskDrive", L"SerialNumber"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tSerialNumber: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_DiskDrive", L"PNPDeviceID"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tPNPDeviceID: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_DiskDrive", L"Manufacturer"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tManufacturer: " << arr[i] << endl; + } + EmptyRtVars(); + cout << endl; - cout << "--Win32_DiskDrive--" << endl << "SerialNumber: " << GetHWIDs("Win32_DiskDrive", L"SerialNumber") << endl; - cout << "PNPDeviceID: " << GetHWIDs("Win32_DiskDrive", L"PNPDeviceID") << endl; - cout << "Manufacturer: " << GetHWIDs("Win32_DiskDrive", L"Manufacturer") << endl << endl; + cout << "--Win32_VideoController--" << endl; + GetHWIDs("Win32_VideoController", L"PNPDeviceID"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tPNPDeviceID: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_VideoController", L"Name"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tName: " << arr[i] << endl; + } + EmptyRtVars(); + cout << endl; - cout << "--Win32_VideoController--" << endl << "PNPDeviceID: " << GetHWIDs("Win32_VideoController", L"PNPDeviceID") << endl; - cout << "Name: " << GetHWIDs("Win32_VideoController", L"Name") << endl << endl; + cout << "--Win32_ComputerSystemProduct--" << endl; + GetHWIDs("Win32_ComputerSystemProduct", L"UUID"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tUUID: " << arr[i] << endl; + } + EmptyRtVars(); + GetHWIDs("Win32_ComputerSystemProduct", L"IdentifyingNumber"); + for (ULONG i = 0; i < arrcount; i++) { + cout << "[" << dec << i + 1 << "]\tIdentifyingNumber: " << arr[i] << endl; + } + EmptyRtVars(); + cout << endl; - cout << "--Win32_ComputerSystemProduct--" << endl << "UUID: " << GetHWIDs("Win32_ComputerSystemProduct", L"UUID") << endl; - cout << "IdentifyingNumber: " << GetHWIDs("Win32_ComputerSystemProduct", L"IdentifyingNumber") << endl << endl << endl; - cout << "Press any key to close this window.." << endl; + cout << endl << "Press enter to close this window.." << endl; cin.ignore(); return 0; diff --git a/IWbemClassObject/query.cpp b/IWbemClassObject/query.cpp index 4a11975..947e5ad 100644 --- a/IWbemClassObject/query.cpp +++ b/IWbemClassObject/query.cpp @@ -1,10 +1,23 @@ -std::string GetHWIDs(std::string table, LPCWSTR column) { +// Promene pro vysledek +std::vector arr; +ULONG arrcount; + +// Funkce pro vynulovani promenych pro vysledek, proc nepouzivm objekty hhahahahhah pomoc +int EmptyRtVars(void) { + arr.clear(); + arrcount = 0; + + return 0; +} + +// https://learn.microsoft.com/en-us/windows/win32/api/wbemcli/nn-wbemcli-ienumwbemclassobject +int GetHWIDs(std::string table, LPCWSTR column) { std::string retvalue = ""; HRESULT hr; hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); if (FAILED(hr)) { - return retvalue; + return 1; } IWbemLocator* pIWbemLocator = NULL; @@ -12,7 +25,7 @@ std::string GetHWIDs(std::string table, LPCWSTR column) { __uuidof(IWbemLocator), (LPVOID*)&pIWbemLocator); if (FAILED(hr)) { CoUninitialize(); - return retvalue; + return 1; } BSTR bstrServer = SysAllocString(L"\\\\.\\root\\cimv2"); @@ -22,43 +35,68 @@ std::string GetHWIDs(std::string table, LPCWSTR column) { pIWbemLocator->Release(); SysFreeString(bstrServer); CoUninitialize(); - return retvalue; + return 1; } hr = CoSetProxyBlanket(pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_DEFAULT); BSTR bstrWQL = SysAllocString(L"WQL"); - //mrdka zasrana POMOC datatypy s vyterem prdele + + // Poskladani WQL Query z parametru funkce a prevod datatypu std::string wqlquery = "select * from " + table; std::wstring wide_string = std::wstring(wqlquery.begin(), wqlquery.end()); const wchar_t* result = wide_string.c_str(); + BSTR bstrPath = SysAllocString(result); IEnumWbemClassObject* pEnum; hr = pIWbemServices->ExecQuery(bstrWQL, bstrPath, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum); + // Enumerace objektu/radku ULONG uReturned; VARIANT var; - IWbemClassObject* pObj = NULL; + IWbemClassObject* pObj[100]; if (!FAILED(hr)) { - hr = pEnum->Next(WBEM_INFINITE, 1, &pObj, &uReturned); + hr = pEnum->Next(WBEM_INFINITE, 100, pObj, &uReturned); } + // Ziskani hodnot vlastnosti/sloupce if (!FAILED(hr) && uReturned) { - hr = pObj->Get(column, 0, &var, NULL, NULL); - if (SUCCEEDED(hr)) { - char str[MAX_PATH]; - WideCharToMultiByte(CP_ACP, 0, var.bstrVal, -1, str, sizeof(str), NULL, NULL); - retvalue = str; + for (ULONG n = 0; n < uReturned; n++) { + hr = pObj[n]->Get(column, 0, &var, NULL, NULL); + if (SUCCEEDED(hr)) { + char str[MAX_PATH]; + WideCharToMultiByte(CP_ACP, 0, var.bstrVal, -1, str, sizeof(str), NULL, NULL); + retvalue = str; + + if (V_VT(&var) == VT_BSTR) { + arr.push_back(retvalue); + pObj[n]->Release(); + //return uReturned; + } + else { + arr.push_back("Empty"); + pObj[n]->Release(); + //return uReturned; + } + } + else { + return 1; // Nepodarilo se ziskat hodnoty + } } } + else { + return 1; // Nepodarilo se enumerovat objekty/radky + } - pEnum->Release(); - SysFreeString(bstrPath); - SysFreeString(bstrWQL); - pIWbemServices->Release(); - pIWbemLocator->Release(); - SysFreeString(bstrServer); - CoUninitialize(); - return retvalue; + arrcount = uReturned; + + //pEnum->Release(); + //SysFreeString(bstrPath); + //SysFreeString(bstrWQL); + //pIWbemServices->Release(); + //pIWbemLocator->Release(); + //SysFreeString(bstrServer); + //CoUninitialize(); + return 0; } \ No newline at end of file