Muhammad
Muhammad

Reputation: 1695

Allocating memory using AWE

I'm using 32bit Windows XP SP3 with PAE enabled and 8GB physical memory.

All i want is to use AWE to allocate memory from the dark side - i.e. the hidden 4GB - so i wrote i gave my user Lock pages in memory right then i wrote the following code:

#include <windows.h>
#include <stdio.h>

BOOL EnableAWE();

int main()
{
    if(!EnableAWE())
    {
        printf("Can not enable AWE on this system.\n");
        return 1;
    }

    HANDLE hProcess = GetCurrentProcess();
    SIZE_T byts = ~0u >> 1;
    LPVOID ptr = VirtualAllocEx(hProcess, NULL, byts, MEM_PHYSICAL | MEM_RESERVE, PAGE_READWRITE);

    if (ptr == NULL)
    {
        printf("Allocation failed for requested memory size.\n");
        return 1;
    }

    VirtualFreeEx(hProcess, ptr, 0, MEM_RELEASE);

    return 0;
}

BOOL EnableAWE()
{
    HANDLE hToken = NULL;

    TOKEN_PRIVILEGES tp;
    tp.PrivilegeCount = 1;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken)) return FALSE;
    if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &(tp.Privileges[0].Luid))) return FALSE;
    if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) return FALSE;

    return TRUE;
}

the function EnableAWE always return true, but when i try to allocate the 2GB using VirtualProtect it fails with error code 87 which means The parameter is incorrect.

i don't know which parameter is incorrect.

thanks.

Upvotes: 1

Views: 1527

Answers (2)

Raymond Chen
Raymond Chen

Reputation: 45172

Um, VirtualAlloc does not allocate AWE memory. (AllocateUserPhysicalPages actually allocates the memory.) It reserves virtual address space into which AWE memory can be mapped. And your process does not have 2GB of available virtual address space.

Upvotes: 1

Igor Nazarenko
Igor Nazarenko

Reputation: 2262

There's no call to VirtualProtect in the code that you posted. Also, did you compile with /LARGEADDRESSAWARE?

In any case, I suspect that you cannot get 2GB of contiguous address space on Win32, even with PAE. Try reducing byts and see if that helps.

Upvotes: 0

Related Questions