Click here to Skip to main content
15,885,916 members
Articles / Mobile Apps / Windows Mobile

Task Manager for Windows Mobile and Windows CE

Rate me:
Please Sign up or sign in to vote.
4.51/5 (23 votes)
30 Nov 2008CPOL10 min read 112.3K   7.3K   79  
Source code for writing your own Task Manager for Windows Mobile or Windows CE based devices
#include "stdafx.h"
#include "VirtualMemory.h"
#include "Windows\Handle.h"

VirtualMemory::VirtualMemory(DWORD processId, DWORD baseAddress) :
    m_baseAddress(baseAddress),
    m_committedMemory(0),
    m_reservedMemory(0),
    m_totalMemory(0)
{
    GetMemoryInfo(processId);
}

void VirtualMemory::GetMemoryInfo(DWORD processId)
{
    DWORD startAddress = m_baseAddress;
    DWORD endAddress = startAddress + 0x40000000;
    DWORD address = startAddress;
#if (_WIN32_WCE < 0x600) && defined(UNDER_CE)
#else
    Windows::Handle handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processId);
#endif

    while (address < endAddress)
    {
        MEMORY_BASIC_INFORMATION memoryInfo;
#if (_WIN32_WCE < 0x600) && defined(UNDER_CE)
        SIZE_T rv = ::VirtualQuery((LPVOID)address, &memoryInfo, sizeof(memoryInfo));
#else
        SIZE_T rv = ::VirtualQueryEx(handle, (LPVOID)address, &memoryInfo, sizeof(memoryInfo));
#endif
        if (rv != 0)
        {
            if (memoryInfo.State == MEM_COMMIT)
                m_committedMemory += memoryInfo.RegionSize;
            if (memoryInfo.State == MEM_RESERVE)
                m_reservedMemory += memoryInfo.RegionSize;
            if (memoryInfo.State != MEM_FREE)
                m_totalMemory += memoryInfo.RegionSize;

            address += memoryInfo.RegionSize;
        }
        else
            break;
    }
}

DWORD VirtualMemory::GetCommittedMemory() const
{
    return m_committedMemory;
}

DWORD VirtualMemory::GetReservedMemory() const
{
    return m_reservedMemory;
}

DWORD VirtualMemory::GetTotalMemory() const
{
    return m_totalMemory;
}

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)


Written By
Team Leader
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions