Skip to content

Add InvalidHandleValueAttribute support - #853

Merged
Steve Otteson (sotteson1) merged 5 commits into
microsoft:masterfrom
riverar:master
Mar 28, 2022
Merged

Steve Otteson (sotteson1) merged 5 commits into
microsoft:masterfrom
riverar:master

Conversation

@riverar

@riverar Rafael Rivera (riverar) commented Mar 25, 2022 •

Copy link
Copy Markdown
Collaborator

Adds a new InvalidHandleValue attribute (AllowMultiple=true) to attach to NativeTypeDef supporting structures (handles). This will be used by binding authors downstream to correctly implement invalid/valid handle semantics and to generate the appropriate checks before calling an associated RAIIFree function.

Also includes a minor refactor of the NativeTypedefStructsCreator class to make it testable. (Tests added.)

Examples:

[RAIIFree("FindVolumeClose")]
[InvalidHandleValue(-1)]
[NativeTypedef]    
public struct FindVolumeHandle
{
   public IntPtr Value;
}

[RAIIFree("UnhookWinEvent")]
[InvalidHandleValue(0)]
[InvalidHandleValue(-1)]
[NativeTypedef]
public struct HWINEVENTHOOK
{
   public IntPtr Value;
}

Closes: #25, microsoft/windows-rs#1630

@AArnott

Copy link
Copy Markdown
Member

AllowMultiple=false

Shouldn't we allow multiple values? Quite a few Windows handles consider 0 or -1 to both be invalid. So much so that .NET even defines a SafeHandle type to represent it.

I think Windows documents a bunch of handles as invalid with -1 as a value. Why then did you choose to express this as a ulong, which forces a representation that doesn't resemble the docs and creates an very unrecognizable value? Why not long?

@riverar

Copy link
Copy Markdown
Collaborator Author

Andrew Arnott (@AArnott) All nods here, thanks! Will adjust now.

@riverar

Copy link
Copy Markdown
Collaborator Author

@sotteson1 Steve Otteson (sotteson1) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks Rafael Rivera (@riverar).

@sotteson1
Steve Otteson (sotteson1) merged commit 15eda95 into microsoft:master Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Release methods need semantic understanding

3 participants