Converting an HRESULT to a Win32 error code: Diagram and answer to exercise

Raymond Chen

Here’s the diagram from How do I convert an HRESULT to a Win32 error code?. If you are offended by VML, cover your ears and hum for a while.

Win32 HRESULT

The little sliver at the top is the mapping of zero to zero. The big white box at the bottom is the mapping of all negative numbers to corresponding negative numbers. And the rainbow represents the mapping of all the positive values, mod 65536, into the range 0x80070000 through 0x8007FFFF.

Now let’s take a look at that puzzle I left behind:

Sometimes, when I import data from a scanner, I get the error “The directory cannot be removed.” What does this mean?

My psychic powers told me that the customer was doing something like this (error checking deleted):

ReportError(HWND hwnd, HRESULT hr)
{
 DWORD dwError = HRESULT_CODE(hr);
 TCHAR szMessage[256];
 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL,
               dwError, 0, szMessage, 256, NULL);
 MessageBox(hwnd, szMessage, TEXT("Error"), MB_OK);
}

and that the actual HRESULT was WIA_ERROR_COVER_OPEN, which is defined as

#define WIA_ERROR_COVER_OPEN MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIA, 16)

Passing this value to HRESULT_CODE would yield 16, which maps to

//
// MessageId: ERROR_CURRENT_DIRECTORY
//
// MessageText:
//
//  The directory cannot be removed.
//
#define ERROR_CURRENT_DIRECTORY          16L

And that would explain why the customer reported this strange error when reading data from a scanner.

0 comments

Discussion is closed.

Feedback usabilla icon