Skip to content

SafeHandle marshaler should allow null handles #42550

Open
@stephentoub

Description

@stephentoub

Today, the SafeHandle marshaler prohibits null SafeHandle instances:

internal static IntPtr SafeHandleAddRef(SafeHandle pHandle, ref bool success)
{
if (pHandle == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.pHandle, ExceptionResource.ArgumentNull_SafeHandle);
}

but there are native methods that let you pass a null pointer to mean "I don't have one". To call such methods, you then either need to use an IntPtr directly or have multiple P/Invoke definitions (one for when you have a non-null SafeHandle and one for when you have null) or create a throw-away safe handle instance that contains IntPtr.Zero. It's not clear to me why the marshaler prohibits null; it could just marshal it as null / zero.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions