HRESULT

Most COM function return an HRESULT. An HRESULT has 3 fields: an SCODE (the high bit), an MS-defined Facility, and the application-defined code.

Helper macros:

  • HRESULT_SEVERITY, SUCCEEDED, FAILED, IS_ERROR
  • HRESULT_FACILITY and HRESULT_CODE
  • MAKE_HRESULT

Win32 support:

FormatMessage can get strings from all the system HRESULTs, but it’s a little messy. Here’s a wrapper.

void ErrorMessage(char* szMessage, HRESULT hr)
{
    if(HRESULT_FACILITY(hr) == FACILITY_WINDOWS)
        hr = HRESULT_CODE(hr);

    char* szError;
    if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER\|
        FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
        (LPTSTR)&szError, 0, NULL) != 0)
    {
        printf("%s: (%0x) %s", szMessage, hr, szError);
        LocalFree(szError);
    }
    else
        printf("Error number not found\n");
}

.NET interop

Many .NET exceptions map to HRESULTS.


References