Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / MFC

Handling Windows API Errors with CWinException

Rate me:
Please Sign up or sign in to vote.
2.42/5 (5 votes)
4 Jan 2002CPOL2 min read 72K   1.1K   22   8
Class for handling Windows API Errors
Given in this article is a class that handles Windows API error with CWinException.

Introduction

When using the Windows API, I think that it is better to get a well-formatted error description rather than its unmeaningful code. I also thought it may be useful that when you have some class based mostly on Win32 API functions to have it throw exceptions that can be handled in the caller block if needed. CWinException class solves those problems - it is derived from MFC's CException class and can be thrown by any API function that sets the 'last error' if it fails. Also, if a function does not set the last error, then you can set the last error to an appropriate value from the source block and catch the exception in the caller block.

Description

The CWinException class makes it possible to get the last error, set the value of the last error, or clear it (set it to ERROR_SUCCESS). It can also get a formatted error description and report it.

Usage

C++
void CService::Start_1(void) throw()
{
    if (!::StartService(m_schService,    // handle to service
            NULL,        // number of arguments 
            NULL))        // no arguments
    {
        throw new CWinException();
    }
}

// Or:

BOOL CService::Start_2(void)
{
    return ::StartService(m_schService, NULL, NULL);
}

// Exception handling

// Header
include "WinException.h"
include "Service.h"
//...
public:
    CService m_cService;
//...

// cpp file
//...

void CMainFrame::OnStartService_1(void)
{
    try
    {
        m_cService.Start_1();
    }
    catch(CWinException* e)
    {
        e->ReportError();
		e->Delete();
    }
}

// Or:

void CMainFrame::OnStartService_2(void)
{
    if(m_cService.Start_1() == FALSE)
    {
        CWinException e;
        e.ReportError();
    }
}

Base Class

  • CException

Class Members

  • CWinException() - default constructor, gets last error on initialize
  • CWinException(DWORD dwErrCode) - sets last error code to dwErrCode on initialize
  • operator LPCTSTR() const - gives direct access to error description stored in CString type private variable
  • operator DWORD() const - gives direct access to error code stored in DWORD type private variable
  • BOOL SetLastError(DWORD dwErrorCode) - sets last error to dwErrorCode, returns TRUE if successful
  • DWORD GetLastError(void) - regets last error code and returns it
  • BOOL ToString(DWORD dwErrorCode, CString& szError) - writes dwErrCode error description to szError, returns TRUE if successful
  • BOOL GetLastErrorString(CString& szLastError) - writes last error description to szError, returns TRUE if successful.
  • void ClearLastError(void) - sets last error to 0 (ERROR_SUCCESS), clears it.
  • virtual BOOL GetErrorMessage( LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext = NULL ) - provides text about an error that has occurred. Returns true if successful.
    • lpszError - a pointer to a buffer that will receive an error message
    • nMaxError - the maximum number of characters the buffer can hold, including the NULL terminator. If NULL, all characters will be written.
    • pnHelpContext - always NULL
  • virtual int ReportError( UINT nType = MB_OK, UINT nMessageID = 0) - reports error text in a message box to the user. Returns an AfxMessageBox value.
    • nType - any combination of the message-box styles.
    • nMessageID - Specifies the resource ID (string table entry) of a message to add to the standard error message string (Run-time error : %d) if not NULL.

History

  • 26th December, 2001: Initial version

License

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


Written By
Software Developer (Senior) SafeNet Inc
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Armen Hakobyan26-Dec-08 2:23
professionalArmen Hakobyan26-Dec-08 2:23 
Bad
Generalcall to unexpected() Pin
Alexei Zakharov15-Mar-03 21:17
Alexei Zakharov15-Mar-03 21:17 
GeneralMemory Leak ! Pin
Bernhard2-Jan-02 4:42
Bernhard2-Jan-02 4:42 
GeneralRe: Memory Leak ! Pin
Bernhard2-Jan-02 4:55
Bernhard2-Jan-02 4:55 
GeneralRe: Memory Leak ! Pin
6-Jan-02 6:16
suss6-Jan-02 6:16 
GeneralRe: Memory Leak ! Pin
Alvaro Mendez7-Jan-02 11:31
Alvaro Mendez7-Jan-02 11:31 
GeneralRe: Memory Leak ! Pin
Tim Smith7-Jan-02 12:08
Tim Smith7-Jan-02 12:08 
GeneralUh, I dont think OnStartService_2 would work... Pin
Peter Yee31-Dec-01 6:49
Peter Yee31-Dec-01 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.