问题
In listings of Win32 error codes, each error has three components:
- The numeric error code
- A descriptive message
- An identifier consisting of capitalized words separated by underscores
According to the documentation, the term "message identifier" refers to the descriptive message, but it doesn't say what the term is for the capitalized error name, and I haven't been able to find that anywhere. These identifiers appear to be analogous to what's called the "Error Id" in a PowerShell ErrorRecord object, but googling for "win32 error id" and "win32 error identifier" didn't lead to an answer.
For example, in the following error:
ERROR_TOO_MANY_OPEN_FILES
4 (0x4)
The system cannot open the file.
4
is the error code.The system cannot open the file.
is the message identifier.ERROR_TOO_MANY_OPEN_FILES
is the __________?
Also, how can this text value be determined, given an error code? I can easily determine the message identifier associated with a given error code like this:
string MessageIdentifier = new Win32Exception(ErrorCode).Message;
However, the Win32Exception class doesn't appear to have a property that corresponds to these capitalized error names (analogous to the ErrorRecord class's ErrorId property).
In some listings I've seen these kinds of identifiers referred to as "constants", but if they're constants, where are they defined/enumerated and how do you access them from a program?
回答1:
For example, in the following error:
ERROR_TOO_MANY_OPEN_FILES
4 (0x4)
The system cannot open the file.•
4
is the error code.
•The system cannot open the file.
is the message identifier.
•ERROR_TOO_MANY_OPEN_FILES
is the __________?
You are wrong on the last two points. 4
is both the error code and the message identifier, per the same documentation you linked to:
All Win32 error codes MUST be in the range 0x0000 to 0xFFFF, although Win32 error codes can be used both in 16-bit fields (such as within the HRESULT type specified in section 2.1) as well as 32-bit fields. Most values also have a default message defined, which can be used to map the value to a human-readable text message; when this is done, the Win32 error code is also known as a message identifier.
The system cannot open the file.
is the message text that belongs to message identifier 4
. That text is reported by FormatMessage() and Win32Exception.Message.
ERROR_TOO_MANY_OPEN_FILES
is just a human-readable #define
in winerror.h in the Win32 SDK:
//
// MessageId: ERROR_TOO_MANY_OPEN_FILES
//
// MessageText:
//
// The system cannot open the file.
//
#define ERROR_TOO_MANY_OPEN_FILES 4L
There is no function in either the Win32 API or .NET to return the text ERROR_TOO_MANY_OPEN_FILES
given the error code 4
. You would have to write your own lookup code if you need that functionality, as demonstrated by this pinvoke.net example:
WINERROR (Constants)
int errorCode = 4; //Microsoft.Win32.Interop.ResultWin32.ERROR_TOO_MANY_OPEN_FILES
string identName = Microsoft.Win32.Interop.ResultWin32.GetErrorName(errorCode);
// returns "ERROR_TOO_MANY_OPEN_FILES"
来源:https://stackoverflow.com/questions/30203717/what-are-the-capitalized-text-identifiers-for-win32-error-codes-called-and-how