Skip to content

Please add .IsNull helper on handle structs to improve the usage syntax over .Value == 0 #206

Closed

Description

@jnm2

Instead of one of these:

if (PInvoke.SelectObject(bitmapDC, objectHandle).Value == 0)
if (PInvoke.SelectObject(bitmapDC, objectHandle).Equals(0))

I'd like to do something with the word 'null' in it since it's a handle:

if (PInvoke.SelectObject(bitmapDC, objectHandle).IsNull)

Alternatives that I would not prefer are == HGDIOBJ.Null or == default.

Activity

  1. AArnott commented on Mar 17, 2021

    @AArnott
    Member

    #122 is similar, in that we want an IsNull property exposed there. But that's for a vtbl which clearly is null or non-null.

    In this case, what you probably really want to know is whether the handle is invalid. Note some handles are invalid when set to 0, while others are invalid at -1. Some are invalid with either value. So I think the new property should be called IsInvalid.

    We're blocked on delivering this till the metadata delivers a way for us to know which values are in fact invalid.

  2. added
    blocked
    on Mar 17, 2021
  3. jnm2 commented on Mar 17, 2021

    @jnm2
    ContributorAuthor

    Actually, I'm specifically interested in NULL (0), not in handle validity, because this is how SelectObject signals an error.

    https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-selectobject#return-value

    If an error occurs and the selected object is not a region, the return value is NULL.

  4. removed
    blocked
    on Mar 19, 2021
  5. jnm2 commented on May 1, 2021

    @jnm2
    ContributorAuthor

    Andrew Arnott (@AArnott) Given my last comment, would you accept a PR for this?

  6. jnm2 commented on May 2, 2021

    @jnm2
    ContributorAuthor

    Andrew Arnott (@AArnott) I'm getting isHandle == false in DeclareTypeDefStruct for HGDIOBJ. Does that mean metadata is wrong? Should I make all typedef structs have IsNull or just handle structs?

    Also, is there an assumed langversion level? I was thinking of generating value == default in case the field type is changed to IntPtr for langversion < 9. But I could also put in smarts so that we get:

    • value == default for 7.1+, otherwise value == default(IntPtr)
    • value == 0 for 9+, otherwise value == IntPtr.Zero
    • value is 0 for 9+, otherwise value == IntPtr.Zero
    • value == IntPtr.Zero always and make sure that we only generate this when value is pointer-sized (probably should anyway)
    • etc
  7. AArnott commented on May 3, 2021

    @AArnott
    Member

    I would use option 4 from your list. Supporting down-level lang versions is a (low priority) goal and I want to avoid branching for different versions unless there's a really compelling reason, so going with the syntax that's supported by lower versions is preferred. That said, I can't see us supporting anything less than C# 7.1.

    Does that mean metadata is wrong?

    No. That's probably just a side-effect of our non-support of AlsoUsableForAttribute (#199). If HGDIOBJ has that attribute, we might follow to the next struct and see if it has an RIAAFree attribute on it.
    But then, some structs are handles that should never be freed, so I guess using RIAAFree to determine whether a typedef represents a handle isn't the greatest way.

    Should I make all typedef structs have IsNull or just handle structs?

    Any IntPtr field-based typedef struct is probably a reasonable target, since as "pointers" null is a legit concept. If a typedef ever exists for what ought to be treated as nint we can deal with that later.

  8. jnm2 commented on May 3, 2021

    @jnm2
    ContributorAuthor

    Oh, hmm. I found this while searching for == default to make the change to == IntPtr.Zero:

    // public override bool IsInvalid => this.handle == default || this.Handle == INVALID_HANDLE_VALUE;

    Should they stay consistent? Which way should they both be?

  9. AArnott commented on May 3, 2021

    @AArnott
    Member

    default is fine.

  10. jnm2 commented on May 3, 2021

    @jnm2
    ContributorAuthor

    In that case, I'm already finished: #258

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions