Click here to Skip to main content
15,891,431 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.6K   7.3K   79  
Source code for writing your own Task Manager for Windows Mobile or Windows CE based devices
// ToolBoxTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <atlcom.h>

#import "..\ToolBox\Generated\ToolBox.tlb"

// CE only supports CC_CDECL, not CC_STDCALL
static _ATL_FUNC_INFO MeasurementInfo = { CC_CDECL, VT_EMPTY , 1, { VT_I4 } };

template <typename _dispinterface, const IID* _diid>
class IUnknownImpl : 
    public IUnknown
{
public:
    STDMETHOD (QueryInterface) (REFIID iid, void **ppvObj)
    {
        if (iid == IID_IUnknown)
            *ppvObj = (IUnknown*)(this);
        else if (iid == *_diid)
            *ppvObj = (_dispinterface*)(this);
        else
        {
            *ppvObj = NULL;
            return E_NOINTERFACE;
        }

        AddRef ();

        return S_OK;
    }

    STDMETHOD_ (ULONG, AddRef) ()
    {
	    return InterlockedExchangeAdd((long*) &m_nRefs, 1) + 1 ;
    }

    STDMETHOD_ (ULONG, Release) ()
    {       
        ULONG ref = InterlockedExchangeAdd( (long*) &m_nRefs, -1) -1;     

        if (ref == 0)
        {
            delete this;
            return 0; // Don't return m_nRefs because this one just got deleted !!
        }            
        return m_nRefs;
    }

private:
    LONG volatile       m_nRefs;
};


class CMeasureCpuLoad :
    public IUnknownImpl<ToolBoxLib::_ICpuLoadEvents, &__uuidof(ToolBoxLib::_ICpuLoadEvents)>,
    public IDispEventImpl<999, CMeasureCpuLoad, &__uuidof(ToolBoxLib::_ICpuLoadEvents), &__uuidof(ToolBoxLib::__ToolBoxLib), 1, 0 >
{
public:
    BEGIN_SINK_MAP(CMeasureCpuLoad)
        SINK_ENTRY_INFO(999, __uuidof(ToolBoxLib::_ICpuLoadEvents), 1, &CMeasureCpuLoad::OnMeasurement, &MeasurementInfo) 
    END_SINK_MAP()

    // Reimplementation
    STDMETHOD (QueryInterface) (REFIID iid, void **ppvObj)
    {
        return IDispEventImpl<999, CMeasureCpuLoad, &__uuidof(ToolBoxLib::_ICpuLoadEvents), &__uuidof(ToolBoxLib::__ToolBoxLib), 1, 0 >::_LocDEQueryInterface(iid, ppvObj);
    }

    // CE only supports __cdecl, not __stdcall
    void __cdecl OnMeasurement(LONG CpuLoad)
    {
        wprintf(L"CpuLoad %d\n", CpuLoad);
    }

    void Subscribe(ToolBoxLib::ICpuLoadPtr cpuLoad)
    {
        HRESULT hr = S_OK;

        IConnectionPointContainerPtr iCPC(cpuLoad);
        IConnectionPointPtr iCP;
		hr = iCPC->FindConnectionPoint (__uuidof (ToolBoxLib::_ICpuLoadEvents), &iCP);
        if (FAILED (hr))
            throw _com_error (hr);

	    hr = iCP->Advise (static_cast<IUnknown*>(this), &m_cookie);

        if (FAILED (hr))
            throw _com_error (hr);
    }

private:
    DWORD   m_cookie;
};

void Test()
{
    HRESULT hr = S_OK;
    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

    try
    {
        ToolBoxLib::ICpuLoadPtr cpuLoad;
        hr = cpuLoad.CreateInstance(__uuidof(ToolBoxLib::CpuLoad));
        hr = cpuLoad->Start();

        CMeasureCpuLoad callback;
        callback.Subscribe(cpuLoad);

        ToolBoxLib::IProcessListPtr processList;
        hr = processList.CreateInstance(__uuidof(ToolBoxLib::ProcessList));
        processList->Update();

        printf("Press 'stop' and '<enter>' to stop.\n");
        char reply[64];
        scanf("%s", &reply);

        hr = cpuLoad->Stop();
        // release
        cpuLoad = NULL;

    } catch (_com_error& err)
    {
        err;
    } catch (...)
    {
    }

    CoUninitialize();
}

int _tmain(int argc, _TCHAR* argv[])
{
    Test();

	return 0;
}

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