ChkLock: ErrorHandler.cpp

ChkLock: ErrorHandler.cpp


//	
//	ErrorHandler.cpp
//
//	Print out fancy error messages
//
//
#include "ErrorHandler.h"
#include "ChkLock.h"

#include <stdio.h>

void ErrorHandler( char *szModule, char *szSymptom, DWORD dwErrorNum )
{
	char		*szMessageBuffer;
	char		*szMessage;
	HINSTANCE	hNetMsg;
	DWORD		dwBufferLength;
	DWORD		dwMessageFlags;

	//	Initialize variables
	szMessage		= new char[ MAX_ERROR_TEXT ];
	hNetMsg			= NULL;
	szMessageBuffer	= NULL;
	dwBufferLength	= 0;
	dwMessageFlags	= FORMAT_MESSAGE_ALLOCATE_BUFFER |
					  FORMAT_MESSAGE_IGNORE_INSERTS |
					  FORMAT_MESSAGE_MAX_WIDTH_MASK |
					  FORMAT_MESSAGE_FROM_SYSTEM ;
	memset( szMessage, 0, MAX_ERROR_TEXT );
	
	//	If it's a NET error, load the proper message table
	if ((dwErrorNum >= NERR_BASE) && (dwErrorNum <= MAX_NERR))
	{
		hNetMsg = LoadLibraryEx( "netmsg.dll", NULL, LOAD_LIBRARY_AS_DATAFILE );

		if (hNetMsg != NULL)
		{
			dwMessageFlags |= FORMAT_MESSAGE_FROM_HMODULE;
		}
	}

	if (dwErrorNum < WSABASEERR)
	{
		//	Format the message and print it out
		if (FormatMessage( dwMessageFlags,
						   hNetMsg,				//	If this is still null, just use system errors
						   dwErrorNum,
						   0,
						   (LPSTR)&szMessageBuffer,
						   0,
						   NULL ))
		{
			//	Format the body
			sprintf( szMessage, TEXT_STRING, szSymptom, dwErrorNum, szMessageBuffer );

			//	Show the message
			cout<<szMessage<<endl;
		} else {
			//	Format the body
			sprintf( szMessage, TEXT_STRING, szSymptom, dwErrorNum, "No text available for this message" );

			cout<<szMessage<<endl;
		}	//	Did FormatMessage work?
	} else {
				//	Format the body
			sprintf( szMessage, TEXT_STRING, szSymptom, dwErrorNum, "No text available for this message" );

			//	Show the message
			cout<<szMessage<<endl;
	}

	//	Free the library, if we loaded it
	if (hNetMsg != NULL)
	{
		FreeLibrary( hNetMsg );
	}

	//	Return memory
	delete szMessage;
}	//	(ErrorHandler)

Back to the ChkLock source code page


This page is maintained by Peyton Engel.
Last updated 13 October 2000