首页 > Windows开发 > 详细

【Windows核心编程】VirtualAlloc 例子

时间:2014-03-27 05:47:28      阅读:599      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 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 }
bubuko.com,布布扣

【Windows核心编程】VirtualAlloc 例子,布布扣,bubuko.com

【Windows核心编程】VirtualAlloc 例子

原文:http://www.cnblogs.com/cuish/p/3627392.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!