`
fdyang
  • 浏览: 79865 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

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;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics