简体   繁体   English

获取 RAM 系统大小

[英]Get RAM system size

我想知道如何通过 C++(在 Windows 7 上)获取我的 RAM 大小。

"

Use GetPhysicallyInstalledSystemMemory to retrieve the amount of RAM that is physically installed on 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 remarks on MSDN say: MSDN的评论说:

The GetPhysicallyInstalledSystemMemory function retrieves the amount of physically installed RAM from the computer's SMBIOS firmware tables. GetPhysicallyInstalledSystemMemory函数从计算机的SMBIOS固件表中检索物理安装的RAM量。 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 . 这可能与GlobalMemoryStatusEx函数报告的数量不同,后者将MEMORYSTATUSEX结构的ullTotalPhys成员设置为可供操作系统使用的物理内存量 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. 操作系统可用的内存量可能小于计算机中物理安装的内存量,因为BIOS和某些驱动程序可能会将内存保留为内存映射设备的I / O区域,从而使操作系统无法访问内存和应用程序。

The amount of physical memory retrieved by the GetPhysicallyInstalledSystemMemory function must be equal to or greater than the amount reported by the GlobalMemoryStatusEx function; GetPhysicallyInstalledSystemMemory函数检索的物理内存量必须等于或大于GlobalMemoryStatusEx函数报告的量; if it is less, the SMBIOS data is malformed and the function fails with ERROR_INVALID_DATA. 如果它更小,则SMBIOS数据格式错误,并且函数因ERROR_INVALID_DATA而失败。 Malformed SMBIOS data may indicate a problem with the user's computer. 格式错误的SMBIOS数据可能表示用户计算机出现问题。

That means, you would also want to look at GlobalMemoryStatusEx . 这意味着,您还需要查看GlobalMemoryStatusEx

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

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);

You want to use the GlobalMemoryStatusEx which returns a MEMORYSTATUSEX . 您想使用返回MEMORYSTATUSEXGlobalMemoryStatusEx The field you want is called ullTotalPhys. 您想要的字段称为ullTotalPhys。

The 0x501 is the WindowsXP version, ie the MEMORYSTATUSEX struct is not supported by some older Windows versions. 0x501是WindowsXP版本,即一些较旧的Windows版本不支持MEMORYSTATUSEX结构。 Your windef.h probably points to a lower WINVER than 0x5XX . 你的windef.h可能指向比0x5XX更低的WINVER

This should work:这应该有效:

#include <windows.h>

unsigned int get_ram(void)
{
    MEMORYSTATUSEX mem;
    mem.dwLength = sizeof(mem);
    GlobalMemoryStatusEx (&mem);

    return mem.ullTotalPhys >> 20;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM