• Windows中根据GetLastError的错误代码,取得文字描述


    根据GetLastError()得到的错误代码,取得文字描述信息。

    例如得到错误号2或123时:

      

    void ShowError(DWORD dwErrNo, LPTSTR lpszFunction)
    { 
        // Retrieve the system error message for the last-error code
        LPVOID lpMsgBuf;
        LPVOID lpDisplayBuf;
        
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            dwErrNo,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &lpMsgBuf,
            0, NULL );
    
        // Display the error message and exit the process
    
        lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
            (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
        StringCchPrintf((LPTSTR)lpDisplayBuf, 
            LocalSize(lpDisplayBuf) / sizeof(TCHAR),
            TEXT("%s failed with error %d: %s"), 
            lpszFunction, dwErrNo, lpMsgBuf); 
        MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 
    
        LocalFree(lpMsgBuf);
        LocalFree(lpDisplayBuf);
    //    ExitProcess(dwErrNo);
    }

    调用例:

    DWORD dwErrNo = GetLastError();
    if ( NO_ERROR != dwErrNo ){
        ShowError( dwErrNo, "foobar" );
    //    return dwErrNo;
    }

     

  • 相关阅读:
    BGP
    ospf路由认证
    rip路由认证
    php-数组的相关函数及排序算法
    php-多维数组,数组遍历
    php-数组的概念,语法及特点
    php-错误处理
    MySQL性能优化
    JUC多线程03
    JUC多线程01
  • 原文地址:https://www.cnblogs.com/gamesun/p/3131255.html
Copyright © 2020-2023  润新知