heapmemory.cpp

来自「wince6.0平台上的任务管理器,功能类似于windows的任务管理器. 」· C++ 代码 · 共 87 行

CPP
87
字号


#include "stdafx.h"
#include <tlhelp32.h>
#include "HeapMemory.h"

HeapMemory::HeapMemory(DWORD processId) :
    m_processId(processId),
    m_heapSize(0)
{
    GetHeapInfo();
}

void HeapMemory::GetHeapInfo()
{
    bool ok = false;
    HANDLE snapShot = INVALID_HANDLE_VALUE;
    m_heapSize = 0;

#if defined(UNDER_CE)
#else
    // This appears to be too slow on XP/Vista
    return;
#endif

    // Need to use __try __except on ToolHelp API.
    // If a process is being destroyed (shutdown), the API crashes (AV on NULL pointer)
    // Can use try catch if /EHa compiler settings is used
//  __try
    try
    {
		snapShot = CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST, m_processId);

        if (snapShot != INVALID_HANDLE_VALUE)
        {
            HEAPLIST32 heapList;
            HEAPENTRY32 heapEntry;

            heapList.dwSize = sizeof(HEAPLIST32);
            heapEntry.dwSize = sizeof(HEAPENTRY32);
            BOOL ret = Heap32ListFirst(snapShot, &heapList);
            while (ret == TRUE)
            {
#if defined(UNDER_CE)
                BOOL ret2 = Heap32First(snapShot, &heapEntry, heapList.th32ProcessID, heapList.th32HeapID);
#else
                BOOL ret2 = Heap32First(&heapEntry, heapList.th32ProcessID, heapList.th32HeapID);
#endif

                // Loop the blocks in the heaps to get the size
                while (ret2 == TRUE)
                {
                    if (heapEntry.dwFlags != LF32_FREE)
                    {
                        m_heapSize += heapEntry.dwBlockSize;
                    }
#if defined(UNDER_CE)
                    ret2 = Heap32Next(snapShot, &heapEntry);
#else
                    ret2 = Heap32Next(&heapEntry);
#endif
                }

                ret = Heap32ListNext(snapShot, &heapList);
            }
        }
        ok = true;
//  } __except ( Filter(GetExceptionCode(), GetExceptionInformation()) )
    } catch (...)
    {
        ok = false;
        m_heapSize = 0;
        if (snapShot != INVALID_HANDLE_VALUE)
        {
#if (_WIN32_WCE < 0x600) && defined(UNDER_CE)
            CloseToolhelp32Snapshot(snapShot);
#endif
        }
    }
}

DWORD HeapMemory::GetHeapMemory() const
{
    return m_heapSize;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?