#include<windows.h>
#include<stdio.h>
#include<tchar.h>
#include<WinBase.h>
#define MEMORY_REQUESTED 1024*1024
BOOL LoggedSetLockPagesPrivilege(HANDLE hProcess,BOOL bEnable);
int _cdecl main(){
BOOL bResult;
ULONG_PTR NumberOfPages;
ULONG_PTR NumberOfPagesInitial;
ULONG_PTR *aPFNs; //page info;holds opaque data
PVOID lpMemReserved;//awe window
SYSTEM_INFO sSysInfo;
int PFNArraySize;//memory to rquest for PFN array
<span style="color:#FF0000;">GetSystemInfo</span>(&sSysInfo);
_tprintf(_T("This computer has page size:%dB\n"),sSysInfo.dwPageSize);//1KB = 1024B
int kbPageSize = sSysInfo.<span style="color:#FF0000;">dwPageSize</span>/1024;
_tprintf(_T("This computer has page size:%dKB\n"),kbPageSize);
// Calculate the number of pages of memory to request.
NumberOfPages = MEMORY_REQUESTED/sSysInfo.dwPageSize;
_tprintf(_T("Requesting %d pages of memory.\n"),NumberOfPages);
PFNArraySize = NumberOfPages * sizeof(ULONG_PTR);
_tprintf(_T("Requestig a PFN array of %d bytes.\n"),PFNArraySize);
_tprintf(_T("Getting process's heap address : %p \n"),<span style="color:#FF0000;">GetProcessHeap</span>());
//Allocate heap for aPFNs
aPFNs = (ULONG_PTR*)<span style="color:#FF0000;">HeapAlloc</span>(<span style="color:#FF0000;">GetProcessHeap</span>(),0,PFNArraySize);
_tprintf(_T("aPFNs address : %p \n"),aPFNs);
if(aPFNs == NULL){
_tprintf(_T("Failed to allocate on heap.\n"));
return -1;
}
//Enable the privilege
_tprintf(_T("Getting process handle : %p\n"),<span style="color:#FF0000;">GetCurrentProcess</span>());//返回-1 即FFFFFFFF
/*if(!LoggedSetLockPagesPrivilege(<span style="color:#FF0000;">GetCurrentProcess</span>(),TRUE)){//编译不通过:Cannnot enable the SE_LOCK_MEMORY_NAME privilege;please check the local policy.
return -2;
}
*/
//Allocate the physical memory.
NumberOfPagesInitial = NumberOfPages;
/*bResult = <span style="color:#FF0000;">AllocateUserPhysicalPages</span>(<span style="color:#FF0000;">GetCurrentProcess</span>(),&NumberOfPages,aPFNs);
if(bResult != TRUE){
_tprintf(_T("Cannot allocate physical pages (%u) \n"),<span style="color:#FF0000;">GetLastError</span>());
return -3;
}
if(NumberOfPagesInitial != NumberOfPages){
_tprintf(_T("Allocated only %p pages.\n"),NumberOfPages);
return -9;
}
*/
//Reserve the virtual memory.
lpMemReserved = <span style="color:#FF0000;">VirtualAlloc</span>(NULL,MEMORY_REQUESTED,MEM_RESERVE|MEM_PHYSICAL,PAGE_READWRITE);
if(lpMemReserved == NULL){
_tprintf(_T("Cannot reserve memory.\n"));
return -4;
}
//Map the physical memory into the window.
/*bResult = <span style="color:#FF0000;">MapUserPhysicalPages</span>(lpMemReserved,
NumberOfPages,
aPFNs);
if(bResult != TRUE){
_tprintf(_T("MapUserPhysicalPages failed (%u)\n"),GetLastError());
return -5;
}*/
//unmap
/*bResult = <span style="color:#FF0000;">MapUserPhysicalPages</span>(lpMemReserved,NumberOfPages,NULL);
if(bResult != TRUE){
_tprintf(_T("MapUserPhysicalPages failed (%u)\n"),GetLastError());
return -6;
}
//Free the physical pages.
bResult = <span style="color:#FF0000;">FreeUserPhysicalPages</span>(<span style="color:#FF0000;">GetCurrentProcess</span>(),&NumberOfPages,aPFNs);
if(bResult != TRUE){
_tprintf(_T("Cannot free physical pages,error %u.\n"),GetLastError());
return -7;
}*/
//Free virtual memory
bResult = <span style="color:#FF0000;">VirtualFree</span>(lpMemReserved,0,MEM_RELEASE);
//Release the aPFNs array
bResult = <span style="color:#FF0000;">HeapFree</span>(<span style="color:#FF0000;">GetProcessHeap</span>(),0,aPFNs);
if(bResult != TRUE){
_tprintf(_T("Call to HeapFree has failed (%u)\n"),<span style="color:#FF0000;">GetLastError</span>());
return -8;
}
return 0;
}
/*****************************************************************
LoggedSetLockPagesPrivilege: a function to obtain or
release the privilege of locking physical pages.
Inputs:
HANDLE hProcess: Handle for the process for which the
privilege is needed
BOOL bEnable: Enable (TRUE) or disable?
Return value: TRUE indicates success, FALSE failure.
*****************************************************************/
BOOL LoggedSetLockPagesPrivilege(HANDLE hProcess,BOOL bEnable){
struct
{
DWORD Count;
LUID_AND_ATTRIBUTES Privilege [1];//1 个元素的数组
}Info;
HANDLE Token;
BOOL Result;
//open the token
Result = <span style="color:#FF0000;">OpenProcessToken</span>(hProcess,TOKEN_ADJUST_PRIVILEGES,&Token);
if(Result != TRUE){
_tprintf(_T("Cannot open process token.\n"));
return FALSE;
}
//Enable of disable?
Info.Count = 1;
if(bEnable){
Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
}
else{
Info.Privilege[0].Attributes = 0;
}
//Get the LUID
Result = <span style="color:#FF0000;">LookupPrivilegeValue</span>(NULL,SE_LOCK_MEMORY_NAME,&(Info.Privilege[0].Luid));
if(Result != TRUE){
_tprintf(_T("Cannot get privilege for %s.\n"),SE_LOCK_MEMORY_NAME);
return FALSE;
}
//Adjust the privilege.
Result = <span style="color:#FF0000;">AdjustTokenPrivileges</span>(Token,FALSE,(PTOKEN_PRIVILEGES)&Info,0,NULL,NULL);
//Check the result
if(Result != TRUE){
_tprintf(_T("Cannot adjust token privileges (%u) \n"),<span style="color:#FF0000;">GetLastError</span>());
return FALSE;
}
else{
if(<span style="color:#FF0000;">GetLastError</span>() != ERROR_SUCCESS){
_tprintf(_T("Cannnot enable the SE_LOCK_MEMORY_NAME privilege;"));
_tprintf(_T("please check the local policy.\n"));
return FALSE;
}
}
return TRUE;
}
C:\Users\jackz\Desktop\codes\cpp>gcc awe.cpp
C:\Users\jackz\Desktop\codes\cpp>a
This computer has page size:4096B
This computer has page size:4KB
Requesting 256 pages of memory.
Requestig a PFN array of 1024 bytes.
Getting process's heap address : 007E0000
aPFNs address : 0080ACF0
Getting process handle : FFFFFFFF