注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

信息 灵感 创新

I? =Information,Inspiration,Innovation

 
 
 

日志

 
 
关于我

we are 5. Mathematics, Computation, Programming, Engineering, and Making fun of life.

网易考拉推荐

获取进程中的堆信息  

2014-07-30 18:47:41|  分类: Windows编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
DWORD GetSystemPageSize()
{
    SYSTEM_INFO si;
    GetSystemInfo(&si);
    return si.dwPageSize;
}

void CreateHeapTest()
{
    DWORD dwPageSize=GetSystemPageSize();
    //create a heap of 1 page, and this heap can be 2 pages at most
    HANDLE hHeap=HeapCreate(HEAP_CREATE_ENABLE_EXECUTE,dwPageSize,2*dwPageSize);
    if(hHeap==NULL)//note that NULL means failure, not INVALID_HANDLE_VALUE
    {
        printf("HeapCreate Failed.\n");
        return;
    }
}

//List all heaps of current process
void ListHeaps()
{
    DWORD dwHeapCount,dwHeapLength;
    PHANDLE phHeaps;
    HANDLE  hDefaultProcessHeap;
    int i;
    dwHeapCount=GetProcessHeaps(0,NULL);
    if (dwHeapCount == 0)
    {
        printf("Failed to retrieve the number of heaps\n");
        return;
    }
    
    // Get a handle to the default process heap.
    //
    hDefaultProcessHeap = GetProcessHeap();
    if (hDefaultProcessHeap == NULL) 
    {
        printf("Failed to retrieve the default process heap.\n");
        return;
    }
    printf("Default heap at address: %#p.\n",hDefaultProcessHeap);

    //allocate buffer from the default process heap.
    phHeaps = (PHANDLE)HeapAlloc(hDefaultProcessHeap, 0,sizeof(*phHeaps)*dwHeapCount);
    if (phHeaps == NULL)
    {
        printf("HeapAlloc failed to allocate %d bytes.\n",sizeof(*phHeaps)*dwHeapCount);
        return;
    }

    //save the original number of heaps, compare it with the return value of the next call.
    dwHeapLength=dwHeapCount;
    dwHeapCount = GetProcessHeaps(dwHeapLength, phHeaps);
    if (dwHeapCount == 0)
    {
        printf("Failed to retrieve the number of heaps on second call.\n");
        return;
    }
    //if not equal means new heap created between the call.
    else if(dwHeapCount>dwHeapLength)
    {
        printf("Another component created a heap between calls.\n");
        return;
    }
    printf("This process has %d heap(s).\n",dwHeapCount);
    for(i = 0; i<dwHeapLength;i++)
        printf("Heap %d starts at address: %#p.\n",i,phHeaps[i]);
    //release memory
    if(!HeapFree(hDefaultProcessHeap, 0, phHeaps))
        printf("Failed to free allocation from default process heap.\n");    
}
在主程序中调用这几个函数,效果如下:
int main(int argn,char* argv[])
{
    ListHeaps();
    CreateHeapTest();
    ListHeaps();
    return 0;
}
CX_0730_184158

  评论这张
 
阅读(428)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016