bgroenks
bgroenks

Reputation: 1889

MinGW GCC Not Recognizing MEMSTATUSEX?

I am using MinGW GCC compiler on Windows 7. I am trying to compile source that contains the following code:

MEMORYSTATUSEX mem_stat;
mem_stat.dwLength = sizeof(memstat);
BOOL success = GlobalMemoryStatusEx(mem_stat);
ram_ptr = &(mem_stat->ullAvailPhys);

As I'm sure you can guess, this code simply gets the available memory using the MEMORYSTATUSEX struct returned by GlobalMemoryStatusEx.

When I try to compile, I get this error:

error: unknown type name 'MEMORYSTATUSEX'

I looked in winbase.h (in the MinGW installation include folder) and guess what I found?

#if (_WIN32_WINNT >= 0x0500)
typedef struct _MEMORYSTATUSEX {
    DWORD dwLength;
    DWORD dwMemoryLoad;
    DWORDLONG ullTotalPhys;
    DWORDLONG ullAvailPhys;
    DWORDLONG ullTotalPageFile;
    DWORDLONG ullAvailPageFile;
    DWORDLONG ullTotalVirtual;
    DWORDLONG ullAvailVirtual;
    DWORDLONG ullAvailExtendedVirtual;
} MEMORYSTATUSEX,*LPMEMORYSTATUSEX;
#endif

So it's there. I'm guessing this has something to do with the precompiler if/endif, but I don't how to fix that....

Also what's even more bizzare is that if I use the MEMORYSTATUS struct instead, it works fine.

According to the MS docs, both have the same minimum client version requirement:

MEMORYSTATUSEX: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366589%28v=vs.85%29.aspx

MEMORYSTATUS: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366772%28v=vs.85%29.aspx

Is there some compiler flag I need to set? Or does anyone have any other solutions?

Thanks in advance for your help!

Upvotes: 0

Views: 1593

Answers (2)

Niranjan Viladkar
Niranjan Viladkar

Reputation: 73

Before including Windows.h, add :

#define WINVER 0x0500

The header file windef.h says :

/*
 * If you need Win32 API features newer the Win95 and WinNT then you must
 * define WINVER before including windows.h or any other method of including
 * the windef.h header.
 */

and then compile with the -std=c++11 flag like :

g++ -Wall -std=c++11 -c <yourFile>.cpp -o <yourFile>.o

Upvotes: 3

bgroenks
bgroenks

Reputation: 1889

Apparently you have to define _WIN32_WINNT yourself either as a compiler flag or definition statement in one of your header/source files for this particular function to work properly.

Adding the #define _WIN32_WINNT 0x0500 will allow the code to compile normally.

Upvotes: 0

Related Questions