Skip to content

AlsoUsableForAttribute should drive handle struct conversions and SafeHandle overloads #205

Closed

Description

@jnm2

Using CsWin32 0.1.422-beta, not referencing the metadata package directly.
This seems to be the required workaround:

// Leaving out DangerousAddRef etc which should generally be added reflexively whenever DangerousGetHandle is used
PInvoke.SelectObject(bitmapDC, (HGDIOBJ)bitmap.DangerousGetHandle());

I'd expect DangerousGetHandle management to be done for me:

PInvoke.SelectObject(bitmapDC, bitmap);

bitmap is a DeleteObjectSafeHandle field that is assigned from a call to CreateDIBSection.

Activity

  1. AArnott commented on Mar 17, 2021

    @AArnott
    Member

    CreateDIBSection returns HBITMAP which is freeable via DeleteObject, so CreateDIBSection offers a SafeHandle.

    HGDIOBJ is not a releasable handle, per the metadata. That's why we don't expose a SafeHandle overload of this method.

    With the next metadata update, HGDIOBJ will be recognized as a kind of base type for several other typedef structs, including HBITMAP. At that point we should make some changes in the CsWin32 projection to not only add conversion operators between these structs, but also to accept SafeHandle everywhere that accepts HGDIOBJ.

  2. changed the title [-]SelectObject doesn't allow passing a SafeHandle to the HGDIOBJ parameter[/-] [+]AlsoUsableForAttribute should drive handle struct conversions and SafeHandle overloads[/+] on Mar 17, 2021
  3. AArnott commented on Mar 17, 2021

    @AArnott
    Member

    Duplicate of #199

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions