dbenham wrote:But regardless whether the command is internal or external, if the command returns a 0 exit code, then && fires, if non-zero, then || fires. The really odd thing is that a few internal commands fire || upon error, yet fail to set ERRORLEVEL unless || is used. This is evidence that the exit code and ERRORLEVEL are not quite synonymous.
You are right; the exit code and the ERRORLEVEL are different constructs:
1) Exit code:
There are different
x86 calling conventions (where to find parameters and return values).
The standard calling convention for the Microsoft Win32 API is "stdcall", so i assume it is also used by "cmd.exe".
Then the exit code is the value of the (E)AX register when returning from a function.
(This value of the exit code is just located somewhere else if "cmd.exe" uses another calling convention.)
2) ERRORLEVEL:
This information is stored within the
process structure (line 0055) (sorry no Microsoft sources available; but it should be very similar), unique per process.
You could retrieve this information in C++ for example using
GetExitCodeProcess.
The default is that the exit code updates the errorlevel (see "Remarks" section of GetExitCodeProcess linked above).
But if another application is executed before the internal command is finished, or if a thread exits later, it may replace the errorcode with no_error (or vice versa).
Internal catched Exception may not lead to en error (although this should be a bug from my point of view).
If an application is no console application then only the "loading success" is returned.
penpen
Edit 1: Changed "function" to "application" ("But if another application is executed before the internal ") because most times: functions will not set the errorlevel. (in this case: application == internal/external commands that set the errorlevel).
Edit 2: Corrected a flaw.