Is there a way to get the string representation of HRESULT value using win API?

后端 未结 4 1845
陌清茗
陌清茗 2020-12-01 02:55

Is there a function in win API which can be used to extract the string representation of HRESULT value?

The problem is that not all return values are documented in M

4条回答
  •  心在旅途
    2020-12-01 03:24

    Here's a sample using FormatMessage()

    LPTSTR SRUTIL_WinErrorMsg(int nErrorCode, LPTSTR pStr, WORD wLength )
    {
        try
        {
            LPTSTR  szBuffer = pStr;
            int nBufferSize = wLength;
    
            //
            // prime buffer with error code
            //
            wsprintf( szBuffer, _T("Error code %u"), nErrorCode);
    
            //
            // if we have a message, replace default with msg.
            //
            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
                    NULL, nErrorCode,
                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                    (LPTSTR) szBuffer,   
                    nBufferSize,    
                    NULL );
        }
        catch(...)
        {
        }
        return pStr;
    } // End of SRUTIL_WinErrorMsg()
    

提交回复
热议问题