Click here to Skip to main content
15,895,084 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.8K   7.3K   79  
Source code for writing your own Task Manager for Windows Mobile or Windows CE based devices
#include "stdafx.h"
#include "Tools\Exception.h"

namespace Tools
{

Exception::Exception(const CLSID& clsId, UINT resourceId, ...)
{
    _ASSERTE ((resourceId > 0x0200) && (resourceId < 0xFFFF));

    m_hr = MAKE_HRESULT (3/*error*/, FACILITY_ITF, resourceId);

    _bstr_t component = GetProgId (clsId);

    va_list argList;
    va_start (argList, resourceId);
    _bstr_t description (LoadString (resourceId, &argList));
    va_end (argList);

    ICreateErrorInfoPtr iCreateErrorInfo;
    if (SUCCEEDED (CreateErrorInfo (&iCreateErrorInfo)))
    {
        iCreateErrorInfo->SetGUID (GUID_NULL);
        iCreateErrorInfo->SetSource (component);
        iCreateErrorInfo->SetDescription (description);

        IErrorInfoPtr iErrorInfo (iCreateErrorInfo);
        SetErrorInfo (0, iErrorInfo);
    }
}

HRESULT Exception::GetException() const
{
    return m_hr;
}

_bstr_t Exception::LoadString (UINT resourceId, va_list *argList)
{
    _bstr_t description;

    WCHAR format[256];
    int length = ::LoadString(::GetModuleInstance(), resourceId, format, ArraySize(format));

    if (length == 0)
    {
        description = L"Unkown message";
    }
    else
    {
        LPWSTR buffer = NULL;
        ::FormatMessage (FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
		                 format, 0, 0, (LPWSTR) &buffer, 0, argList);
        if (buffer != NULL)
        {
    	    description = buffer;
            LocalFree (buffer);
        }
    }

    return description;
}

_bstr_t Exception::GetProgId (const CLSID& clsId)
{
    _bstr_t progId (L"Unknown");
    
    LPOLESTR progIdOle;
    ProgIDFromCLSID (clsId, &progIdOle);

    if (progIdOle != NULL)
    {
        progId = progIdOle;
        CoTaskMemFree (progIdOle);
    }

	return progId;
}

} // namespace Tools

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