microsoft / microsoft/CsWin32

NTSTATUS and HRESULT projected as struct instead of enum

Open
#1,643 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
2.5k
Forks
124
Avg merge
1d 3h
Merged PRs (30d)
9

Description

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;
...
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how NTSTATUS and HRESULT are projected and compare that path with the existing WIN32_ERROR enum projection. Determine the generator entry point and related tests from those type names; done means generated NTSTATUS and HRESULT values can be used in C# switch statements without breaking existing projections.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.