diff --git a/CMakeLists.txt b/CMakeLists.txt index d91d298e9..23c98ec5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -327,6 +327,7 @@ if(WIN_MSYS) src/detection/disk/disk_windows.cpp src/detection/cpuUsage/cpuUsage_windows.c src/detection/cpuUsage/cpuUsage_nowait_windows.cpp + src/detection/memory/memory_windows.cpp src/util/windows/wmi.cpp # Shared @@ -336,7 +337,6 @@ if(WIN_MSYS) # TODO src/detection/media/media_linux.c src/detection/font/font_linux.c - src/detection/memory/memory_linux.c src/detection/temps/temps_linux.c ) endif() diff --git a/src/detection/memory/memory_windows.cpp b/src/detection/memory/memory_windows.cpp new file mode 100644 index 000000000..91a42ef3c --- /dev/null +++ b/src/detection/memory/memory_windows.cpp @@ -0,0 +1,67 @@ +extern "C" { +#include "memory.h" +} +#include "util/windows/wmi.hpp" + +void detectRam(FFMemoryStorage* ram) +{ + IEnumWbemClassObject* pEnumerator = ffQueryWmi(L"SELECT TotalVisibleMemorySize, FreePhysicalMemory FROM Win32_OperatingSystem", &ram->error); + if(!pEnumerator) + return; + + IWbemClassObject *pclsObj = NULL; + ULONG uReturn = 0; + + if(FAILED(pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn)) || uReturn == 0) + { + ffStrbufInitS(&ram->error, "No WMI result returned"); + pEnumerator->Release(); + return; + } + + //KB + ffGetWmiObjUnsigned(pclsObj, L"TotalVisibleMemorySize", &ram->bytesTotal); + uint64_t bytesFree; + ffGetWmiObjUnsigned(pclsObj, L"FreePhysicalMemory", &bytesFree); + ram->bytesUsed = ram->bytesTotal - bytesFree; + + pclsObj->Release(); + pEnumerator->Release(); + + ram->bytesTotal *= 1024; + ram->bytesUsed *= 1024; +} + +void detectSwap(FFMemoryStorage* swap) +{ + IEnumWbemClassObject* pEnumerator = ffQueryWmi(L"SELECT AllocatedBaseSize, CurrentUsage FROM Win32_PageFileUsage", &swap->error); + if(!pEnumerator) + return; + + IWbemClassObject *pclsObj = NULL; + ULONG uReturn = 0; + + if(FAILED(pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn)) || uReturn == 0) + { + ffStrbufInitS(&swap->error, "No WMI result returned"); + pEnumerator->Release(); + return; + } + + //MB + ffGetWmiObjUnsigned(pclsObj, L"AllocatedBaseSize", &swap->bytesTotal); + ffGetWmiObjUnsigned(pclsObj, L"CurrentUsage", &swap->bytesUsed); + + pclsObj->Release(); + pEnumerator->Release(); + + swap->bytesTotal *= 1024 * 1024; + swap->bytesUsed *= 1024 * 1024; +} + +extern "C" +void ffDetectMemoryImpl(FFMemoryResult* memory) +{ + detectRam(&memory->ram); + detectSwap(&memory->swap); +}