//-------------------------------------------------------------------
//-- Error Lookup C++ sample - Copyright © 2006, Dave Moats
//--
//-- This sample is provided 'as-is', without any express or implied warranty.
//-- In no event will the authors be held liable for any damages arising from
//-- the use of this sample code.
//--
//-- Permission is granted to anyone to use this sample code for any purpose,
//-- including commercial applications, subject to the following restrictions:
//--
//-- The origin of this code must not be misrepresented;
//-- you must not claim that you wrote the original code.
//-- If you use this code, an acknowledgment in the
//-- documentation is requested - shown below:
//--
//-- Portions Copyright © 2006, Dave Moats (http://www.davemoats.com/).
//--
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-- To use this sample, you must include the following headers
//-------------------------------------------------------------------
#include <windows.h>
#include <tchar.h>
//-------------------------------------------------------------------
//-- Function: lookupError
//-- Arguments: unsigned long errorNumber - error code to lookup
//-- TCHAR *sourceLibrary - path to a custom message library
//-- ( can be NULL )
//-- Returns: N/A
//-- Description: Looks up the error code in the standard windows
//-- message files and if provided, a custom messsage
//-- dll and returns the string representation of the error.
//-------------------------------------------------------------------
void lookupError ( unsigned long errorNumber, TCHAR *sourceLibrary )
{
// intializing the buffer that will store the the error message
TCHAR* errorBuffer = NULL;
// not looking up 0 - since this usually means success
if ( errorNumber != 0 )
{
// using FormatMessage to:
// allocate a buffer for the error message
// looking in the standard windows error codes
// using the OS default language
// allocating a minimum of 10 characters in the return buffer
if ( ::FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errorNumber,
LANG_SYSTEM_DEFAULT,
( TCHAR*)&errorBuffer,
10,
NULL ) != 0 )
{
// removing the trailing \r\n from the returned message
TCHAR* crlfPtr = _tcschr ( errorBuffer, _T ( '\r'));
if ( crlfPtr != NULL )
*crlfPtr = _T ( '\0');
// here is your error code message
_tprintf ( _T ( "Error - %u - %s\n"),
errorNumber,
errorBuffer );
// free up the string buffer
if ( errorBuffer )
LocalFree ( errorBuffer );
}
// FormatMessage failed....do we have a custom message dll?
else if ( sourceLibrary )
{
lookupCustomError ( errorNumber, sourceLibrary );
}
// hmmmm........the lookup failed and there is no custom library
// so just display a standard message
else
{
_tprintf ( _T ( "Unable to lookup %u in the standard windows errors\n"),
errorNumber );
}
}
// just letting you know it looks good
else
_tprintf ( _T ( "The error code to check was 0 - this usually represents SUCCESS\n" ));
}
//-------------------------------------------------------------------
//-- Function: lookupCustomError
//-- Arguments: unsigned long errorNumber - error code to lookup
//-- TCHAR *sourceLibrary - path to a custom message library
//-- Returns: N/A
//-- Description: Looks up the error code in the a custom messsage
//-- dll and returns the string representation of the error.
//-------------------------------------------------------------------
void lookupCustomError ( unsigned long errorNumber, TCHAR *sourceLibrary )
{
// make sure we have a path to a custom message dll
if ( !sourceLibrary )
{
_tprintf ( _T ( "The path to the custom library was not set, unable to lookup the custom error\n" ));
return;
}
// intializing the buffer that will store the the error message
TCHAR* errorBuffer = NULL;
// not looking up 0 - since this usually means success
if ( errorNumber != 0 )
{
// try to load the custom message dll using the path
HINSTANCE hLib = LoadLibrary ( sourceLibrary );
if ( hLib != NULL )
{
// able to load the library so use FormatMessage to:
// allocate a buffer for the error message
// looking in custom message dll
// using the OS default language
// allocating a minimum of 10 characters in the return buffer
if ( ::FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_HMODULE,
hLib,
errorNumber,
LANG_SYSTEM_DEFAULT,
( TCHAR*)&errorBuffer,
10,
NULL ) != 0 )
{
// removing the trailing \r\n from the returned message
TCHAR* crlfPtr = _tcschr ( errorBuffer, _T ( '\r'));
if ( crlfPtr != NULL )
*crlfPtr = _T ( '\0');
// here is your error code message
_tprintf ( _T ( "Error - %u - %s\n"),
errorNumber,
errorBuffer );
// free up the string buffer
if ( errorBuffer )
LocalFree ( errorBuffer );
}
else
{
// couldn't find the error so use a standard error message
_tprintf ( _T ( "Unable to lookup %u in the standard windows or custom error codes\n"),
errorNumber );
}
// free up the library we loaded
FreeLibrary ( hLib);
}
// unable to load the library
else
_tprintf ( _T ( "Unable to load the custom message library: %s\n" ), sourceLibrary );
}
// just letting you know it looks good
else
_tprintf ( _T ( "The error code to check was 0 - this usually represents SUCCESS\n" ));
}
//-- Error Lookup C++ sample - Copyright © 2006, Dave Moats
//--
//-- This sample is provided 'as-is', without any express or implied warranty.
//-- In no event will the authors be held liable for any damages arising from
//-- the use of this sample code.
//--
//-- Permission is granted to anyone to use this sample code for any purpose,
//-- including commercial applications, subject to the following restrictions:
//--
//-- The origin of this code must not be misrepresented;
//-- you must not claim that you wrote the original code.
//-- If you use this code, an acknowledgment in the
//-- documentation is requested - shown below:
//--
//-- Portions Copyright © 2006, Dave Moats (http://www.davemoats.com/).
//--
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-- To use this sample, you must include the following headers
//-------------------------------------------------------------------
#include <windows.h>
#include <tchar.h>
//-------------------------------------------------------------------
//-- Function: lookupError
//-- Arguments: unsigned long errorNumber - error code to lookup
//-- TCHAR *sourceLibrary - path to a custom message library
//-- ( can be NULL )
//-- Returns: N/A
//-- Description: Looks up the error code in the standard windows
//-- message files and if provided, a custom messsage
//-- dll and returns the string representation of the error.
//-------------------------------------------------------------------
void lookupError ( unsigned long errorNumber, TCHAR *sourceLibrary )
{
// intializing the buffer that will store the the error message
TCHAR* errorBuffer = NULL;
// not looking up 0 - since this usually means success
if ( errorNumber != 0 )
{
// using FormatMessage to:
// allocate a buffer for the error message
// looking in the standard windows error codes
// using the OS default language
// allocating a minimum of 10 characters in the return buffer
if ( ::FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errorNumber,
LANG_SYSTEM_DEFAULT,
( TCHAR*)&errorBuffer,
10,
NULL ) != 0 )
{
// removing the trailing \r\n from the returned message
TCHAR* crlfPtr = _tcschr ( errorBuffer, _T ( '\r'));
if ( crlfPtr != NULL )
*crlfPtr = _T ( '\0');
// here is your error code message
_tprintf ( _T ( "Error - %u - %s\n"),
errorNumber,
errorBuffer );
// free up the string buffer
if ( errorBuffer )
LocalFree ( errorBuffer );
}
// FormatMessage failed....do we have a custom message dll?
else if ( sourceLibrary )
{
lookupCustomError ( errorNumber, sourceLibrary );
}
// hmmmm........the lookup failed and there is no custom library
// so just display a standard message
else
{
_tprintf ( _T ( "Unable to lookup %u in the standard windows errors\n"),
errorNumber );
}
}
// just letting you know it looks good
else
_tprintf ( _T ( "The error code to check was 0 - this usually represents SUCCESS\n" ));
}
//-------------------------------------------------------------------
//-- Function: lookupCustomError
//-- Arguments: unsigned long errorNumber - error code to lookup
//-- TCHAR *sourceLibrary - path to a custom message library
//-- Returns: N/A
//-- Description: Looks up the error code in the a custom messsage
//-- dll and returns the string representation of the error.
//-------------------------------------------------------------------
void lookupCustomError ( unsigned long errorNumber, TCHAR *sourceLibrary )
{
// make sure we have a path to a custom message dll
if ( !sourceLibrary )
{
_tprintf ( _T ( "The path to the custom library was not set, unable to lookup the custom error\n" ));
return;
}
// intializing the buffer that will store the the error message
TCHAR* errorBuffer = NULL;
// not looking up 0 - since this usually means success
if ( errorNumber != 0 )
{
// try to load the custom message dll using the path
HINSTANCE hLib = LoadLibrary ( sourceLibrary );
if ( hLib != NULL )
{
// able to load the library so use FormatMessage to:
// allocate a buffer for the error message
// looking in custom message dll
// using the OS default language
// allocating a minimum of 10 characters in the return buffer
if ( ::FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_HMODULE,
hLib,
errorNumber,
LANG_SYSTEM_DEFAULT,
( TCHAR*)&errorBuffer,
10,
NULL ) != 0 )
{
// removing the trailing \r\n from the returned message
TCHAR* crlfPtr = _tcschr ( errorBuffer, _T ( '\r'));
if ( crlfPtr != NULL )
*crlfPtr = _T ( '\0');
// here is your error code message
_tprintf ( _T ( "Error - %u - %s\n"),
errorNumber,
errorBuffer );
// free up the string buffer
if ( errorBuffer )
LocalFree ( errorBuffer );
}
else
{
// couldn't find the error so use a standard error message
_tprintf ( _T ( "Unable to lookup %u in the standard windows or custom error codes\n"),
errorNumber );
}
// free up the library we loaded
FreeLibrary ( hLib);
}
// unable to load the library
else
_tprintf ( _T ( "Unable to load the custom message library: %s\n" ), sourceLibrary );
}
// just letting you know it looks good
else
_tprintf ( _T ( "The error code to check was 0 - this usually represents SUCCESS\n" ));
}
Copyright © 2010 Dave Moats. All rights reserved. Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED. Void where prohibited by law. Please report any issues or broken links.
You may link to this site freely from your own site. You may quote from this site, but please include a link to the original source on the originating site.