Skip to content

Commit

Permalink
Windows: Ensure returned value type is checked (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianxhokaxhiu authored Apr 17, 2024
1 parent 9ba5dfe commit 3d234f4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/windows/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std::string Battery::getSerialNumber() const { return "<unknwon>"; }
std::string Battery::getTechnology() const { return "<unknwon>"; }

// _____________________________________________________________________________________________________________________
uint32_t Battery::getEnergyFull() const { return 0; }
uint32_t Battery::getEnergyFull() const { return 1; }

// _____________________________________________________________________________________________________________________
uint32_t Battery::energyNow() const { return 0; }
Expand Down Expand Up @@ -64,15 +64,15 @@ std::vector<Battery> getAllBatteries() {
VARIANT vt_prop;
HRESULT hr;
hr = obj->Get(L"Name", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
battery._model = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"FullChargeCapacity", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_I4)) {
battery._energyFull = vt_prop.uintVal;
}
hr = obj->Get(L"DeviceID", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
battery._serialNumber = utils::wstring_to_std_string(vt_prop.bstrVal);
}
VariantClear(&vt_prop);
Expand Down
10 changes: 5 additions & 5 deletions src/windows/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ std::vector<CPU> getAllCPUs() {
VARIANT vt_prop;
HRESULT hr;
hr = obj->Get(L"Name", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
cpu._modelName = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"Manufacturer", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
cpu._vendor = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"NumberOfCores", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_I4)) {
cpu._numPhysicalCores = vt_prop.intVal;
}
hr = obj->Get(L"NumberOfLogicalProcessors", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_I4)) {
cpu._numLogicalCores = vt_prop.intVal;
}
hr = obj->Get(L"MaxClockSpeed", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_I4)) {
cpu._maxClockSpeed_MHz = vt_prop.uintVal;
cpu._regularClockSpeed_MHz = vt_prop.uintVal;
}
Expand Down
13 changes: 6 additions & 7 deletions src/windows/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,20 @@ std::vector<Disk> getAllDisks() {
VARIANT vt_prop;
HRESULT hr;
hr = obj->Get(L"Model", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
disk._model = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"Manufacturer", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
disk._vendor = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"SerialNumber", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (vt_prop.bstrVal) // the value may empty
disk._serialNumber = utils::wstring_to_std_string(vt_prop.bstrVal);
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
disk._serialNumber = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"Size", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
disk._size_Bytes = static_cast<int64_t>(vt_prop.ullVal);
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
disk._size_Bytes = std::stoll(utils::wstring_to_std_string(vt_prop.bstrVal));
}
VariantClear(&vt_prop);
obj->Release();
Expand Down
10 changes: 5 additions & 5 deletions src/windows/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ std::vector<GPU> getAllGPUs() {
VARIANT vt_prop;
HRESULT hr;
hr = obj->Get(L"Name", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
gpu._name = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"AdapterCompatibility", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
gpu._vendor = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"DriverVersion", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
gpu._driverVersion = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"AdapterRam", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_I4)) {
gpu._memory_Bytes = vt_prop.uintVal;
}
hr = obj->Get(L"PNPDeviceID", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
std::string ret = utils::wstring_to_std_string(vt_prop.bstrVal);
if (utils::starts_with(ret, "PCI\\")) {
utils::replaceOnce(ret, "PCI\\", "");
Expand Down
8 changes: 4 additions & 4 deletions src/windows/mainboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ MainBoard::MainBoard() {
VARIANT vt_prop;
HRESULT hr;
hr = obj->Get(L"Manufacturer", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
_vendor = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"Product", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
_name = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"Version", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
_version = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"SerialNumber", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
_serialNumber = utils::wstring_to_std_string(vt_prop.bstrVal);
}
VariantClear(&vt_prop);
Expand Down
8 changes: 4 additions & 4 deletions src/windows/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ OS::OS() {
VARIANT vt_prop;
HRESULT hr;
hr = obj->Get(L"Caption", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
_name = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"OSArchitecture", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
_64bit = utils::wstring_to_std_string(vt_prop.bstrVal).find("64") != std::string::npos;
_32bit = !_64bit;
}
hr = obj->Get(L"BuildNumber", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
_version = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"Version", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
_kernel = utils::wstring_to_std_string(vt_prop.bstrVal);
}
VariantClear(&vt_prop);
Expand Down
12 changes: 6 additions & 6 deletions src/windows/ram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ Memory::Memory() {
Memory::Module module;
module.id = id++;
hr = obj->Get(L"Manufacturer", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
module.vendor = utils::wstring_to_std_string(vt_prop.bstrVal);
}
hr = obj->Get(L"partNumber", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
module.model = utils::wstring_to_std_string(vt_prop.bstrVal);
// TODO: One expects an actual name of the RAM but wmi does not provide such a property...
// The "Name"-property of WMI returns "PhysicalMemory".
module.name = std::string(module.vendor + " " + module.model);
}
hr = obj->Get(L"Capacity", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
module.total_Bytes = std::stoll(utils::wstring_to_std_string(vt_prop.bstrVal));
}
hr = obj->Get(L"ConfiguredClockSpeed", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
module.frequency_Hz = static_cast<int64_t>(vt_prop.ulVal) * 1000 * 1000;
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_I4)) {
module.frequency_Hz = static_cast<int64_t>(vt_prop.intVal) * 1000 * 1000;
}
hr = obj->Get(L"SerialNumber", 0, &vt_prop, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (SUCCEEDED(hr) && (V_VT(&vt_prop) == VT_BSTR)) {
module.serial_number = utils::wstring_to_std_string(vt_prop.bstrVal);
}
VariantClear(&vt_prop);
Expand Down

0 comments on commit 3d234f4

Please sign in to comment.