Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Task Manager for Windows Mobile and Windows CE

, 30 Nov 2008 CPOL
Source code for writing your own Task Manager for Windows Mobile or Windows CE based devices
CETaskManager_demo.zip
Documents and Settings
Werner
My Documents
The CodeProject
CETaskManager_V2
CETaskManager.CAB
CETaskManager_src.zip
CETaskManager
CAB
TaskManager.vddproj
CEChart
bin
Release
CE
CEchart.dll
CEchart.pdb
CETaskManager
Properties
Settings.settings
Res
TaskMgr.ico
TaskMgr.ico
ToolBox
Idl
Res
CpuLoad.rgs
CpuLoadDCOM.rgs
Process.rgs
ProcessDCOM.rgs
ProcessList.rgs
ProcessListDCOM.rgs
System.rgs
SystemDCOM.rgs
ToolBox.def
ToolBox.rgs
ToolBoxdcom.rgs
Src
Threads
Tools
Windows
ToolBox.vcproj.ISAS5.Werner.user
ToolBoxTest

#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;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Werner Willemsens
Team Leader
Belgium Belgium
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 30 Nov 2008
Article Copyright 2008 by Werner Willemsens
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid