Reputation: 2484
I want to query the installed memory's size. I used the above code:
void GetInstalledMemory( char * MemorySize )
{
memset( MemorySize, 0, sizeof( MemorySize ) );
MEMORYSTATUSEX statex;
statex.dwLength = sizeof( statex );
if ( !GlobalMemoryStatusEx( &statex ) ) strcpy( MemorySize, "N/A" );
else sprintf( MemorySize, "%I64d", statex.ullTotalPhys / 1024 / 1024 );
}//GetInstalledMemory
The problem with this code is, that under a 32 bit PC it shows 3240 Mb, and under a 64 bit PC it shows 3976 MB RAM, however both PC has 4.0 GB RAM installed. Is there any way to get somehow the installed memory's correct size?
Thanks!
Upvotes: 1
Views: 1330
Reputation: 57650
This code might work,
#include <windows.h>
#include <stdio.h>
int main()
{
MEMORYSTATUSEX m;
m.dwLength = sizeof (m);
GlobalMemoryStatusEx (&m);
printf("Installed Memory size = %I64d KB\n", m.ullTotalPhys/1024);
return 0;
}
But hardware accessing functions are not in standard C. So they wont be portable.
The problem with this code is, that under a 32 bit PC it shows 3240 Mb, and under a 64 bit PC it shows 3976 MB RAM, however both PC has 4.0 GB RAM installed.
This is because 32 bit machine can not address more than 3Gb memory.
Upvotes: 3
Reputation: 2484
Let me answer my own question. So, there is really no way to get the installed memory's correct size, because windows in the amount of memory size windows won't calculate the videocard's memory size. So if you have 4 GB RAM installed, and you have a 512 Mb video card, and you want to query the installed memory's size, then you will get as result, the you have ~3488MB RAM. However, from WMI you you can query the correct size. In Win32_PhysicalMemory get value for Capacity and you'll have the correct value.
I know, that WMI query is a complicated a little bit under C, but unfortunately there are some things, that you can query only from there. In my application, i'm making right now, i already use wmi querys, so it's not a problem for me.
Thanks everyone for your help!
kampi
Upvotes: 0
Reputation: 5917
Neither ANSI/ISO C nor POSIX allow you to query the size of installed main memory, the rationale being that it makes no sense for your program logic to depend on the amount of installed RAM. The following two generic workarounds do however fairly well, especially when combined:
Upvotes: -1
Reputation: 182761
You would have to use the DMI interface and would be limited to systems that support it. Many versions of Windows (including XP and Vista with no service packs) have no idea how much physical memory is present.
Upvotes: 0