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

Task Manager for Windows Mobile and Windows CE

, 30 Nov 2008
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
// 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)

Share

About the Author

Werner Willemsens
Team Leader
Belgium Belgium
No Biography provided

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