windows 内存分配尝试(AWE相关API编译器没找到库,所以全部注释)

108 篇文章 1 订阅
#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值