获取RAM系统大小

发布于 2024-10-31 03:34:29 字数 45 浏览 7 评论 0原文

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

I would like to know how can I get the size of my RAM through C++ (on Windows 7).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧城烟雨 2024-11-07 03:34:29

使用 GetPhysicallyInstalledSystemMemory 检索计算机上物理安装的 RAM 量。

(请注意,这需要 Windows Vista SP1 或更高版本。该功能在早期版本的 Windows 操作系统上不可用。)

MSDN 上的评论说:

获取物理安装的系统内存
函数检索金额
物理安装的 RAM
计算机的 SMBIOS 固件表。
这可能与金额不同
由 GlobalMemoryStatusEx 报告
函数,设置 ullTotalPhys
MEMORYSTATUSEX 结构的成员
到物理内存量
适用于操作系统
使用
。可用内存量
到操作系统可以少
比物理内存量
安装在计算机中是因为
BIOS和一些驱动程序可能会保留
内存作为 I/O 区域
内存映射设备,使得
内存无法用于操作
系统和应用程序。

物理内存量
检索到的
获取物理安装的系统内存
函数必须等于或大于
比报告的金额
GlobalMemoryStatusEx 函数;
如果是
较少,SMBIOS 数据格式错误
并且该功能失败
错误_无效_数据。 SMBIOS 格式错误
数据可能表明存在问题
用户的计算机。

这意味着,您还需要查看 GlobalMemoryStatusEx

Use GetPhysicallyInstalledSystemMemory to retrieve the amount of RAM that is physically installed on the computer.

(Note that this requires Windows Vista SP1 or later. The function is not available on earlier versions of the Windows operating system.)

The remarks on MSDN say:

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.

That means, you would also want to look at GlobalMemoryStatusEx.

野生奥特曼 2024-11-07 03:34:29

好吧,伙计们!我通过这样做[大师模式打开]找到了解决方案:

#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;
}

我必须定义_WIN32_WINNT 0x0501,但我不知道为什么[大师模式关闭]。

如果有人可以向我解释它在做什么以及为什么没有它它就不起作用。

还有一件事,那是什么:

statex.dwLength = sizeof (statex);

Okay, guys! I've found the solution by doing this like that [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].

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);
孤星 2024-11-07 03:34:29

在 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";
}

在 Linux 上:

读取 /proc/meminfo

On 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:

Read /proc/meminfo.

ぃ弥猫深巷。 2024-11-07 03:34:29

您想要使用 GlobalMemoryStatusEx< /code>返回一个 MEMORYSTATUSEX 。您想要的字段称为ullTotalPhys

You want to use the GlobalMemoryStatusEx which returns a MEMORYSTATUSEX. The field you want is called ullTotalPhys.

捂风挽笑 2024-11-07 03:34:29

0x501是WindowsXP版本,即MEMORYSTATUSEX结构不受某些旧Windows版本的支持。您的windef.h可能指向比0x5XX更低的WINVER

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文