Getting available RAM with GlobalMemoryStatus()/GlobalMemoryStatusEx() ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#define MEGABYTE (1024 * 1024)

DWORD GetTotalRAM()
{
	MEMORYSTATUS MemoryStatus;
	ZeroMemory(&MemoryStatus, sizeof(MEMORYSTATUS));
	MemoryStatus.dwLength = sizeof(MEMORYSTATUS);
	
	GlobalMemoryStatus(&MemoryStatus);
	if(MemoryStatus.dwTotalPhys == -1) // overflow
	{
		MEMORYSTATUSEX MemoryStatusEx;
		GlobalMemoryStatusEx(&MemoryStatusEx);
		return (MemoryStatusEx.ullTotalPhys / (1024 * 1024));
	}
	return (MemoryStatus.dwTotalPhys / MEGABYTE);
}

int __cdecl _tmain(__in int argc, __in_ecount_z(argc) _TCHAR* argv[], __in_z _TCHAR* envp[])
{
	UNREFERENCED_PARAMETER(argc);
	UNREFERENCED_PARAMETER(argv);
	UNREFERENCED_PARAMETER(envp);

	_tprintf(_T("Total Available RAM: %d MB\n"), GetTotalRAM());
	_gettchar();
	return EXIT_SUCCESS;
}


Keeps returning & printing 0, any idea why?
Last edited on
Topic archived. No new replies allowed.