November 10th, 2006

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

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.