Question

Say, if I have a WinAPI that fails with an HRESULT code. Is there a way to convert it to an error description string?

Was it helpful?

Solution

This is the helper function we use in-house to extract a Win32 error code from an HRESULT:

DWORD Win32FromHResult(HRESULT hr)
{
    if ((hr & 0xFFFF0000) == MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, 0))
    {
        return HRESULT_CODE(hr);
    }

    if (hr == S_OK)
    {
        return ERROR_SUCCESS;
    }

    // Not a Win32 HRESULT so return a generic error code.
    return ERROR_CAN_NOT_COMPLETE;
}

You can then use FormatMessage to get it into string form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top