【Windows核心编程】VirtualAlloc 例子

 1 // VirtualAlloc.cpp : 定义控制台应用程序的入口点。  2 //  3 #include "stdafx.h"

 4 #include <Windows.h>

 5 #include <process.h>

 6 #include <iostream>

 7 using namespace std;  8 

 9 #ifdef UNICODE 10 #define  PRINT wcout

11 #else

12 #define  PRINT cout

13 #endif

14 

15 int _tmain(int argc, _TCHAR* argv[]) 16 { 17     SIZE_T sizeOfLargePage = GetLargePageMinimum(); 18     if (0 == sizeOfLargePage) 19  { 20         cerr<<"error in GetLargePageMinium \n"<<endl; 21         return -1; 22  } 23     cout<<"sizeOfLargePage is "<<sizeOfLargePage<<endl; 24 

25     int nCount = 10; 26 

27     //PVOID pAddr = VirtualAlloc(NULL, sizeOfLargePage * nCount, MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

28     PVOID pAddr = VirtualAlloc(NULL, sizeOfLargePage * nCount, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); 29 

30     if (NULL == pAddr) 31  { 32         cerr<<"error in VirtualAlloc \n"; 33         return -2; 34  } 35 

36     TCHAR szBuffer[] = _T("hello world!"); 37     size_t nBuffer = _countof(szBuffer); 38 

39     

40    // memcpy_s(pAddr, _countof(szBuffer), szBuffer, _countof(szBuffer)); //_countof参数只能是数组,返回字符数

41     memcpy_s(pAddr, sizeof(szBuffer), szBuffer,sizeof(szBuffer)); 42 

43 

44     PRINT<<(TCHAR*)pAddr<<endl; 45 

46     VirtualFree(pAddr, 0, MEM_DECOMMIT | MEM_RELEASE); 47 

48     return 0; 49 }

 

你可能感兴趣的:(windows)