#include <windows.h>
#include <stdio.h>
#include <tchar.h>
/*Code by ping0s1992*/
DWORD PrintHeapSize(HANDLE hHeap,LPVOID lpMem);
INT main(INT argc,PTCHAR argv[]){
SYSTEM_INFO si;
HANDLE hHeap; //handle of heap
LPVOID lpMem; //pointer of memory block
LPVOID lpReAlloc; // pointer of realloc memory block
DWORD dwHeapSize;
HANDLE hHeaps[24];
DWORD dwHeapNum; //number of heap
GetSystemInfo(&si);
printf("系统内存页大小:0x%x\n系统内存分配粒度:0x%x\n",si.dwPageSize,si.dwAllocationGranularity);
if(argc == 2 && 0 == lstrcmp(argv[1],TEXT("-a"))){
hHeap = HeapCreate(HEAP_NO_SERIALIZE,si.dwPageSize,si.dwPageSize*10);
printf("创建一个大小限定的堆\n");
}else if(argc == 2 && 0 == lstrcmp(argv[1],TEXT("-s"))){
hHeap=GetProcessHeap();
printf("系统中已经存在的堆\n");
}else{
hHeap = HeapCreate(HEAP_NO_SERIALIZE,0,0);
printf("创建一个堆,初始化为一页,可自增长.\n");
}
if(hHeap == NULL){
printf("创建堆失败:%d\n",GetLastError());
return 1;
dwHeapNum = GetProcessHeaps(24,hHeaps);
if(dwHeapNum == 0){
printf("获取进程中堆数量失败:%d\n",GetLastError());
printf("当前进程中一共有%u个堆.\n",dwHeapNum);
lpMem = HeapAlloc(hHeap,HEAP_ZERO_MEMORY,si.dwPageSize*3);
if(lpMem == NULL){
printf("在堆中分配内存失败:%d\n",GetLastError());
PrintHeapSize(hHeap,lpMem);
lpReAlloc = HeapReAlloc(hHeap,HEAP_ZERO_MEMORY,lpMem,si.dwPageSize*11);
if(lpReAlloc == NULL){
printf("在堆中重新分配内存失败:%d",GetLastError());
printf("在堆中再分配内存,地址为:0x%x.\n原地址:0x%x\n",lpReAlloc,lpMem);
return 0;
}
DWORD PrintHeapSize(HANDLE hHeap,LPVOID lpMem){
SIZE_T dwHeapSize;
dwHeapSize = HeapSize(hHeap,HEAP_NO_SERIALIZE,lpMem);
if(dwHeapSize == -1){
printf("Get HeapSize error:%d",GetLastError());
printf("内存块大小为:0x%x\n",dwHeapSize);
<a href="http://blog.51cto.com/attachment/201107/123831370.png" target="_blank"></a>
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/624390,如需转载请自行联系原作者