Skip to content

NTSTATUS and HRESULT projected as struct instead of enum #1643

@MichaelGrafnetter

Description

@MichaelGrafnetter

Is your feature request related to a problem? Please describe.
Unlike WIN32_ERROR, NTSTATUS and HRESULT are projected as structs instead of enums. As individual error codes are not constant expressions, they cannot be used in switch statements:

if (status == NTSTATUS.STATUS_SUCCESS || status == NTSTATUS.STATUS_PENDING || status == NTSTATUS.STATUS_MORE_ENTRIES || status == NTSTATUS.STATUS_BUFFER_TOO_SMALL)
{
    // No error occurred, so exit gracefully.
    return status;
}

if (status == NTSTATUS.STATUS_INVALID_PARAMETER)
{
    throw new ArgumentException();
}

Describe the solution you'd like
With enums, switch statements can be used, which simplifies the code:

switch (error)
{
    case WIN32_ERROR.ERROR_DS_INVALID_DN_SYNTAX:
    case WIN32_ERROR.ERROR_INVALID_PARAMETER:
    case WIN32_ERROR.ERROR_INVALID_NAME:
    case WIN32_ERROR.ERROR_BAD_ARGUMENTS:
    case WIN32_ERROR.ERROR_INVALID_FLAG_NUMBER:
    case WIN32_ERROR.ERROR_INVALID_ADDRESS:
        exceptionToThrow = new ArgumentException(genericException.Message, genericException);
        break;
    case WIN32_ERROR.ERROR_ARITHMETIC_OVERFLOW:
        exceptionToThrow = new ArithmeticException(genericException.Message, genericException);
        break;
    case WIN32_ERROR.ERROR_BAD_EXE_FORMAT:
        exceptionToThrow = new BadImageFormatException(genericException.Message, genericException);
        break;
    case WIN32_ERROR.ERROR_BAD_FORMAT:
    case WIN32_ERROR.ERROR_SXS_MANIFEST_PARSE_ERROR:
    case WIN32_ERROR.ERROR_INVALID_DATA:
    case WIN32_ERROR.ERROR_DATATYPE_MISMATCH:
        exceptionToThrow = new FormatException(genericException.Message, genericException);
        break;
...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions