But are those error codes designed to be used as application return
values? Is it normal practice on windows to use then as such?
The Windows error codes are designed to identify the cause and
severity of the error and it's documented in winerror.h:
//
// Note: There is a slightly modified layout for HRESULT values
below,
// after the heading "COM Error Codes".
//
// Values are 32 bit values laid out as follows:
//
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +---+-+-+-----------------------+-------------------------------+
// |Sev|C|R| Facility | Code |
// +---+-+-+-----------------------+-------------------------------+
//
// where
//
// Sev - is the severity code
//
// 00 - Success
// 01 - Informational
// 10 - Warning
// 11 - Error
//
// C - is the Customer code flag
//
// R - is a reserved bit
//
// Facility - is the facility code
//
// Code - is the facility's status code
//
They then go on to enumerate the facility codes and all of those
facilities are internal to Windows.
Each of the error codes returned from Win32 are enumerated in that
header and the text of the message associated with that error code is
also listed in the comments about that error code.
The COM error codes have an altered bitmap format and it's these codes
that are interpreted by the HRESULT macro.
//
// HRESULTs are 32 bit values layed out as follows:
//
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +-+-+-+-+-+---------------------+-------------------------------+
// |S|R|C|N|r| Facility | Code |
// +-+-+-+-+-+---------------------+-------------------------------+
//
// where
//
// S - Severity - indicates success/fail
//
// 0 - Success
// 1 - Fail (COERROR)
//
// R - reserved portion of the facility code, corresponds to NT's
// second severity bit.
//
// C - reserved portion of the facility code, corresponds to NT's
// C field.
//
// N - reserved portion of the facility code. Used to indicate a
// mapped NT status value.
//
// r - reserved portion of the facility code. Reserved for
internal
// use. Used to indicate HRESULT values that are not
status
// values, but are instead message ids for display
strings.
//
// Facility - is the facility code
//
// Code - is the facility's status code
//
In Windows, a dialog box appears with the message text and the error
code from the Win32 system so there is really no reason to return it
from the application. I suppose a lazy C++ program could pass it up
the call chain and let it all unroll but then you run into the hazards
Alf is concerned about with collision of the error code with some
constant that doesn't have the same meaning at a higher level.
Windows itself returns error 259L, that Alf is so paranoid about, and
it's defined thus:
//
// MessageId: ERROR_NO_MORE_ITEMS
//
// MessageText:
//
// No more data is available.
//
#define ERROR_NO_MORE_ITEMS 259L
The actual Windows error codes returned from Win32 facilities are
logged by the system in the Event Log (the Windows equivalent of the
*nix syslog) and can be analyzed post-mortem if need be.
The point of the multiplicity of Win32 error codes, IMHO, is for a
user program to interpret the error at the point of use and deal with
it appropriately, terminating the operation and returning its own
error codes as necessary and they were never intended to returned from
main.
Alf commits a grevious error when he cites STILL_RUNNING as value 259.
The actual macro name is STILL_ACTIVE and can be returned from the
Win32 GetExitCodeThread function and must be avoided when terminating
a thread or process since the return value from a thread lingers.
Microsoft warns about this in their documentation:
"Important The GetExitCodeThread function returns a valid error code
defined by the application only after the thread terminates.
Therefore, an application should not use STILL_ACTIVE (259) as an
error code. If a thread returns STILL_ACTIVE (259) as an error code,
applications that test for this value could interpret it to mean that
the thread is still running and continue to test for the completion of
the thread after the thread has terminated, which could put the
application into an infinite loop."
Alf's policy of returning Windows internal error codes from his
application creates complication and hazard where none would exist by
simply returning EXIT_SUCCESS or EXIT_FAILURE.
But then, I don't know anything and I am plinked.