嗨,我正在用 C 構建一個應用程式。我想獲得Windows 機器正在使用的 RAM 百分比。我嘗試了一些代碼,例如:
string getRamUsage()
{
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
DWORDLONG physMemUsed = memInfo.ullTotalPhys - memInfo.ullAvailPhys;
return to_string(physMemUsed);
}
但它只是回傳一些匯編值。我能得到解決方案嗎?
uj5u.com熱心網友回復:
你需要打電話GlobalMemoryStatusEx來獲取資料。
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
// it already contains the percentage
auto memory_load = statex.dwMemoryLoad;
// or calculate it from other field if need more digits.
auto memory_load = 1 - (double)statex.ullAvailPhys / statex.ullTotalPhys;
注意:如果GlobalMemoryStatusEx失敗,您應該檢查的回傳值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/376681.html
