【Windows核心编程】VirtualAlloc 例子

时间:2022-04-09 13:34:00
 // VirtualAlloc.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <process.h>
#include <iostream>
using namespace std; #ifdef UNICODE
#define PRINT wcout
#else
#define PRINT cout
#endif int _tmain(int argc, _TCHAR* argv[])
{
SIZE_T sizeOfLargePage = GetLargePageMinimum();
if ( == sizeOfLargePage)
{
cerr<<"error in GetLargePageMinium \n"<<endl;
return -;
}
cout<<"sizeOfLargePage is "<<sizeOfLargePage<<endl; int nCount = ; //PVOID pAddr = VirtualAlloc(NULL, sizeOfLargePage * nCount, MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
PVOID pAddr = VirtualAlloc(NULL, sizeOfLargePage * nCount, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); if (NULL == pAddr)
{
cerr<<"error in VirtualAlloc \n";
return -;
} TCHAR szBuffer[] = _T("hello world!");
size_t nBuffer = _countof(szBuffer); // memcpy_s(pAddr, _countof(szBuffer), szBuffer, _countof(szBuffer)); //_countof参数只能是数组,返回字符数
memcpy_s(pAddr, sizeof(szBuffer), szBuffer,sizeof(szBuffer)); PRINT<<(TCHAR*)pAddr<<endl; VirtualFree(pAddr, , MEM_DECOMMIT | MEM_RELEASE); return ;
}