• HRESULT 数据类型和显示错误信息


    HRESULT

    What we should know about HRESULT ?
    - HRSULT is a kind of Data Type ( Long 32bit) which is used for Windows.
    - It is The return codes used by COM interfaces.
    - To test an HRESULT value, use the FAILED and SUCCESSED macros.
    - This type is declared in WinNT.h as follows:
       typedef LONG HRESULT;

    Structure of COM Error Codes.
    - HRESULT value has 32bits and is divided into 3 fields:  a severity code, a facility code, and an error code.

    Bit    31 30 29 28 27, 26 25 24 ... 16, 15 14 ... 0
    Field  S   R  C   N   r , Facility           , Error


    Convert HRSULT retrun codes to error messages.
    法(1) We can use  AMGetErrorText Function.
    - Syntax :

    DWORD AMGetErrorText (
    	HRESULT hr,           // HRESULT value.
    	TCHAR *pBuffer,     // Pointer to a character buffer that receives the error message.
     	DWORD MaxLen     // Number of characters in pBuffer.
    );



    - Requirement:
       Header: Errors.h (dshow.h)
       Lib : Quarz.lib
    - Example:

    void ShowError(HRESULT hr)
    {
        if (FAILED(hr))
        {
            TCHAR szErr[MAX_ERROR_TEXT_LEN];
            DWORD res = AMGetErrorText(hr, szErr, MAX_ERROR_TEXT_LEN);
            if (res == 0)
            {
                StringCchPrintf(szErr, MAX_ERROR_TEXT_LEN, "Unknown Error: 0x%2x", hr);
            }
            MessageBox(0, szErr, TEXT("Error!"), MB_OK | MB_ICONERROR);
        }
    }


     

    //But , 我尝试上述,似乎总是找不到AMGetErrorText函数,找不到合适的H文件,可能需要安装额外的SDK.


    法 (2) 

    CString HrToMessage( HRESULT hr )
    {
         LPVOID lpMsgBuf;
         CString strTmp;
         ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
             FORMAT_MESSAGE_FROM_SYSTEM,
             NULL,
             hr,
             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
             (LPTSTR) &lpMsgBuf,
             0,
             NULL );
         strTmp.Format( _T("%s"), (char *) lpMsgBuf );
         ::LocalFree( lpMsgBuf );
         return strTmp;
    }


     

  • 相关阅读:
    16_基于FPGA的DA_TLC5615驱动输出
    17_IIC协议与FPGA驱动AT24C04
    14_基于FPGA的DSS与嵌入式逻辑分析仪的调用
    11_基于FPGA的按键计数器
    08_基于FPGA的测頻计的设计
    15_基于FPGA的AD_TLC549采集模拟信号
    13_基于FPGA的液晶1602显示
    09_基于FPGA驱动蜂鸣器唱歌
    HTML 综合案例简陋的相亲网站界面
    go 语言里面的 & 和 *
  • 原文地址:https://www.cnblogs.com/fdyang/p/2858738.html
Copyright © 2020-2023  润新知