简体   繁体   中英

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 - ie 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM