windows 获取 RAM 系统大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5553665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get RAM system size
提问by Octopus
I would like to know how can I get the size of my RAM through C++ (on Windows 7).
我想知道如何通过 C++(在 Windows 7 上)获取 RAM 的大小。
回答by Nawaz
Use GetPhysicallyInstalledSystemMemory
to retrieve the amount of RAM that is physically installedon the computer.
使用GetPhysicallyInstalledSystemMemory
来检索的RAM量物理安装在计算机上。
(Note that this requires Windows Vista SP1 or later. The function is not available on earlier versions of the Windows operating system.)
(请注意,这需要 Windows Vista SP1 或更高版本。该功能在早期版本的 Windows 操作系统上不可用。)
The remarkson MSDN say:
MSDN上的评论说:
The GetPhysicallyInstalledSystemMemory function retrieves the amount of physically installed RAM from the computer's SMBIOS firmware tables. This can differ from the amount reported by the GlobalMemoryStatusEx function, which sets the ullTotalPhys member of the MEMORYSTATUSEX structure to the amount of physical memory that is available for the operating system to use. The amount of memory available to the operating system can be less than the amount of memory physically installed in the computer because the BIOS and some drivers may reserve memory as I/O regions for memory-mapped devices, making the memory unavailable to the operating system and applications.
The amount of physical memory retrieved by the GetPhysicallyInstalledSystemMemory function must be equal to or greater than the amount reported by the GlobalMemoryStatusEx function;if it is less, the SMBIOS data is malformed and the function fails with ERROR_INVALID_DATA. Malformed SMBIOS data may indicate a problem with the user's computer.
GetPhysicallyInstalledSystemMemory 函数从计算机的 SMBIOS 固件表中检索物理安装的 RAM 量。 这可能与 GlobalMemoryStatusEx 函数报告的数量不同,后者将 MEMORYSTATUSEX 结构的 ullTotalPhys 成员设置为可供操作系统使用的物理内存量。操作系统可用的内存量可能小于计算机中物理安装的内存量,因为 BIOS 和某些驱动程序可能会将内存保留为内存映射设备的 I/O 区域,从而使操作系统无法使用这些内存和应用。
GetPhysicallyInstalledSystemMemory 函数检索的物理内存量必须等于或大于 GlobalMemoryStatusEx 函数报告的量;如果小于,则 SMBIOS 数据格式错误并且函数失败并显示 ERROR_INVALID_DATA。格式错误的 SMBIOS 数据可能表明用户的计算机存在问题。
That means, you would also want to look at GlobalMemoryStatusEx
.
这意味着,您还想查看GlobalMemoryStatusEx
.
回答by Octopus
Okay, guys! I've found the solution by doing this like that [guru mode on]:
好的,伙计们!我通过这样做找到了解决方案 [guru mode on]:
#define _WIN32_WINNT 0x0501 // I misunderstand that
#include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex); // I misunderstand that
GlobalMemoryStatusEx (&statex);
cout << "Physical RAM => " << (float)statex.ullTotalPhys/(1024*1024*1024)<< endl;
system("PAUSE");
return EXIT_SUCCESS;
}
I had to define _WIN32_WINNT 0x0501, but i don't know why [guru mode is off].
我必须定义_WIN32_WINNT 0x0501,但我不知道为什么[大师模式关闭]。
If somebody could explain me what it is doing and why it doesn't work without it.
如果有人可以向我解释它在做什么以及为什么没有它就不能工作。
One more thing, what is that:
还有一件事,那是什么:
statex.dwLength = sizeof (statex);
回答by Naszta
On Windows:
在 Windows 上:
typedef BOOL (WINAPI *PGMSE)(LPMEMORYSTATUSEX);
PGMSE pGMSE = (PGMSE) GetProcAddress( GetModuleHandle( TEXT( "kernel32.dll" ) ), TEXT( "GlobalMemoryStatusEx") );
if ( pGMSE != 0 )
{
MEMORYSTATUSEX mi;
memset( &mi, 0, sizeof(MEMORYSTATUSEX) );
mi.dwLength = sizeof(MEMORYSTATUSEX);
if ( pGMSE( &mi ) == TRUE )
os << "RAM: " << mi.ullTotalPhys / 1048576 << "MB";
else
pGMSE = 0;
}
if ( pGMSE == 0 )
{
MEMORYSTATUS mi;
memset( &mi, 0, sizeof(MEMORYSTATUS) );
mi.dwLength = sizeof(MEMORYSTATUS);
GlobalMemoryStatus( &mi );
os << "RAM: " << mi.dwTotalPhys / 1048576 << "MB";
}
On Linux:
在 Linux 上:
Read /proc/meminfo
.
阅读/proc/meminfo
。
回答by Andrew White
You want to use the GlobalMemoryStatusExwhich returns a MEMORYSTATUSEX. The field you want is called ullTotalPhys.
您想使用返回MEMORYSTATUSEX的GlobalMemoryStatusEx。您需要的字段称为 ullTotalPhys。
回答by Aram Paronikyan
The 0x501
is the WindowsXP version, i.e. the MEMORYSTATUSEX
struct is not supported by some older Windows versions. Your windef.h probably points to a lower WINVER
than 0x5XX
.
这0x501
是 WindowsXP 版本,即MEMORYSTATUSEX
某些较旧的 Windows 版本不支持该结构。你可能WINDEF.H指向更低的WINVER
比0x5XX
。