ERROR.C 4.38 KB

/*-----------------------------------------------------------------------------

    This is a part of the Microsoft Source Code Samples. 
    Copyright (C) 1995 Microsoft Corporation.
    All rights reserved. 
    This source code is only intended as a supplement to 
    Microsoft Development Tools and/or WinHelp documentation.
    See these sources for detailed information regarding the 
    Microsoft samples programs.

    MODULE: Error.c

    PURPOSE: Implement error handling functions 
             called to report errors.

    FUNCTIONS:
        ErrorExtender - 调用FormatMessage将错误代码转换为错误文本
        ErrorReporter - 向用户报告错误
        ErrorHandler  -报告错误,然后退出进程
        ErrorInComm   - 报告错误,关闭通信连接,然后退出

-----------------------------------------------------------------------------*/

#include <windows.h>
#include "mttty.h"

/*
    Prototypes of functions called only in this module
*/
DWORD ErrorExtender(DWORD, char **);


/*-----------------------------------------------------------------------------

FUNCTION: ErrorExtender(DWORD, char **)

PURPOSE: Translates error codes into error strings

PARAMETERS:
    dwError  - error code to be translated
    szBuffer - pointer to error string buffer

COMMENTS: If code can't be translated, then a 1 byte NULL string
          created.  The buffer, whether created by FormatMessage, or
          directly is supposed to be freed by the caller.

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
DWORD ErrorExtender(DWORD dwError, char ** szBuffer)
{
    DWORD dwRes = 0;

    dwRes = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | 80 ,
                          NULL, dwError,
                          MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
                          (LPTSTR) szBuffer, 0, NULL);

    if (dwRes == 0) {
        *szBuffer = LocalAlloc(LPTR, 1);
        return 1;
    }

    return dwRes;
}


/*-----------------------------------------------------------------------------

FUNCTION: ErrorReporter(char *)
向用户报告错误

PARAMETERS:
    szMessage - 来自app的错误消息

COMMENTS: 报告控制台和调试器中的错误字符串

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void ErrorReporter(char * szMessage)
{
    char * szFormat = "Error %d: %s.\n\r%s\r\n";    // format for wsprintf
    char * szExtended;      // error string translated from error code
    char * szFinal;         // final string to report
    DWORD dwExtSize;
    DWORD dwErr;

    dwErr = GetLastError();

    /*
		从系统获取错误字符串
    */
    dwExtSize = ErrorExtender(dwErr, &szExtended);
    
    /*
		为系统中传入的错误字符串分配缓冲区
		以及szFormat字符串中的额外内容
    */
    szFinal = LocalAlloc(LPTR, strlen(szMessage) + dwExtSize + 30);

    if (szFinal == NULL)	// if no final buffer, then can't format error
        MessageBox(ghwndMain, "Cannot properly report error.", "Fatal Error", MB_OK);
    else {	
        wsprintf(szFinal, szFormat, dwErr, szMessage, szExtended);

        OutputDebugString(szFinal);

        if (g_appdata.fDisplayErrors)
            MessageBox(ghwndMain, szFinal, NULL, MB_OK);

        LocalFree(szFinal);                  // free final buffer
    }

    /*
		释放扩展字符串缓冲区
    */
    LocalFree(szExtended);

    return;
}


/*-----------------------------------------------------------------------------

FUNCTION: ErrorHandler( char * )

PURPOSE: 处理一个致命错误(在comm端口打开之前)

PARAMETERS:
    szMessage - 来自app的错误消息

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void ErrorHandler(char * szMessage)
{	
    ErrorReporter(szMessage);
    ExitProcess(0);
}


/*-----------------------------------------------------------------------------

FUNCTION: ErrorInComm( char * )

PURPOSE: 在comm端口打开后处理一个致命错误

PARAMETERS:
    szMessage - 来自app的错误消息

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void ErrorInComm(char * szMessage)
{
    ErrorReporter(szMessage);
    BreakDownCommPort();
    ExitProcess(0);
}