WinApi学习笔记-内存的复制,填充,输出等操作
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define MEM_BLOCK_SIZE 32
//lpMem需要打印的内存指针
//dwSize大小
BOOL ShowMemContent(LPVOID lpMem,SIZE_T dwSize)
{
BYTE lpShow[MEM_BLOCK_SIZE];
INT i = 0;
if(dwSize >MEM_BLOCK_SIZE)
{
cout<<"over flow堆栈溢出";
return FALSE;
}
CopyMemory((LPVOID)lpShow,lpMem,dwSize);
for(;i<dwSize;i++)
{
//cout<<&lpShow[i];
printf("%.2X",lpShow[i]);
if(!((i+1)%16))
{
cout<<endl;
}
}
cout<<endl;
return TRUE;
}
int main(void)
{
HANDLE hHeap = GetProcessHeap();
LPVOID lpSrc;
LPVOID lpDis;
lpSrc = HeapAlloc(hHeap,0,MEM_BLOCK_SIZE);
lpDis = HeapAlloc(hHeap,0,MEM_BLOCK_SIZE);
cout<<"HeapAlloc分配但不清零"<<endl;
ShowMemContent(lpDis,MEM_BLOCK_SIZE);
ZeroMemory(lpDis,MEM_BLOCK_SIZE);
cout<<"HeapAlloc分配并清零"<<endl;
ShowMemContent(lpDis,MEM_BLOCK_SIZE);
cout<<"用指定的内容填充内存"<<endl;
FillMemory(lpSrc,MEM_BLOCK_SIZE,0xBB);
FillMemory(lpSrc,MEM_BLOCK_SIZE/2,0xAA);
CopyMemory(lpDis,lpSrc,MEM_BLOCK_SIZE);
ShowMemContent(lpDis,MEM_BLOCK_SIZE);
HeapFree(hHeap,0,lpSrc);
HeapFree(hHeap,0,lpDis);
CHAR a;
std::cin>>a;
}