Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make optional SafeHandle parameters accept null #129

Closed
AArnott opened this issue Feb 18, 2021 · 2 comments · Fixed by #143
Closed

Make optional SafeHandle parameters accept null #129

AArnott opened this issue Feb 18, 2021 · 2 comments · Fixed by #143
Assignees
Labels
enhancement New feature or request

Comments

@AArnott
Copy link
Member

AArnott commented Feb 18, 2021

While the .NET interop layer may not permit null for SafeHandle parameters, we can certainly support it by making the extern method private and exposing a wrapper to it that checks for null SafeHandle args and does the right thing.

Example:

[DllImport(Entrypoint = "CreateFileW")]
private static extern SafeFileHandle CreateFile_Private(char* path, [Optional, In] SafeHandle hTemplate);

internal static SafeFileHandle CreateFile(char* path, SafeHandle? hTemplate)
{
   return CreateFile_Private(path, hTemplate ?? NullSafeHandle.NullHandle);
}
@AArnott AArnott added the enhancement New feature or request label Feb 18, 2021
@jnm2
Copy link
Contributor

jnm2 commented Feb 18, 2021

When the project langversion is >= 8, could you also put #nullable enable or at least #nullable annotations in the file or around the parameter declaration and use SafeHandle? as the parameter type so that IntelliSense shows me that nulls are anticipated?

@AArnott
Copy link
Member Author

AArnott commented Feb 18, 2021

@jnm2 yes, I have that in mind to eventually do. I filed an issue to track.

@AArnott AArnott self-assigned this Feb 23, 2021
AArnott added a commit that referenced this issue Feb 23, 2021
This also:
* adds support for SafeHandles that are 32-bits long even in 64-bit processes (e.g. `MSIHANDLE`).
* removes `SafeHandle` from all `extern` methods. They only appear on helper methods now.
* removes the `NullSafeHandle` static class.

Closes #129
AArnott added a commit that referenced this issue May 6, 2022
Collect test diagnostic logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants