Memory: add support for Windows

This commit is contained in:
李通洲
2022-10-07 20:12:48 +08:00
parent a57e334d73
commit 0a55071f88
2 changed files with 68 additions and 1 deletions
+1 -1
View File
@@ -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()
+67
View File
@@ -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);
}