zoukankan      html  css  js  c++  java
  • 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;
    }


     

  • 相关阅读:
    python,生产环境安装
    neo4j 图数据库
    RNN系列
    机器学习关于AUC的理解整理
    fensorflow 安装报错 DEPENDENCY ERROR
    dubbo Failed to check the status of the service com.user.service.UserService. No provider available for the service
    使用hbase遇到的问题
    MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk
    gradle 安装
    jenkins 安装遇到的坑
  • 原文地址:https://www.cnblogs.com/fdyang/p/2858738.html
Copyright © 2011-2022 走看看