Skip to content

Leverage AlsoUsableForAttribute for more interoperable typedefs #199

Closed

Description

microsoft/win32metadata#251 was just merged which adds AlsoUsableForAttribute. Is that worth waiting for?

Originally posted by Joseph Musser (@jnm2) in #185 (comment)

Also to accept SafeHandle everywhere that accepts the base type struct (e.g. HGDIOBJ, as mentioned in #205).

Activity

  1. AArnott commented on Jan 24, 2022

    @AArnott
    MemberAuthor

    An interesting sanity check after this feature is implemented would be to see if this sample compiles without errors:

    using System.Drawing;
    using System.Runtime.InteropServices;
    using Windows.Win32;
    using Windows.Win32.Foundation;
    using Windows.Win32.Graphics.Gdi;
    using static Windows.Win32.PInvoke;
    
    class Helper
    {
        public Bitmap GetScreenCapture(HWND handle)
        {
            Bitmap bitmap;
    
            RECT region;
            GetWindowRect(handle, out region);
    
            HWND desktopWindowHandle = GetDesktopWindow();
            using SafeHandle windowDCHandle = GetWindowsDC_SafeHandle(desktopWindowHandle);
            using DeleteDCSafeHandle memoryDCHandle = CreateCompatibleDC(windowDCHandle);
            using DeleteObjectSafeHandle bitmapHandle = CreateCompatibleBitmap(windowDCHandle, region.right - region.left, region.bottom - region.top);
            HGDIOBJ bitmapOldHandle = SelectObject(memoryDCHandle, (HGDIOBJ)bitmapHandle.DangerousGetHandle());
    
            bool success = BitBlt(memoryDCHandle, 0, 0, region.right - region.left, region.bottom - region.top, windowDCHandle, region.left, region.top, ROP_CODE.SRCCOPY | ROP_CODE.CAPTUREBLT);
    
            try
            {
                bitmap = Image.FromHbitmap(bitmapHandle.DangerousGetHandle());
            }
            finally
            {
                SelectObject(memoryDCHandle, bitmapOldHandle);
            }
    
            return bitmap;
        }
    }

    NativeMethods.txt:

    GetWindowRect
    GetDesktopWindow
    GetWindowDC
    CreateCompatibleDC
    CreateCompatibleBitmap
    SelectObject
    BitBlt
    

    Thanks to dotnet/pinvoke#593 who provided the basis for this sample.

  2. AArnott commented on Jan 25, 2022

    @AArnott
    MemberAuthor

    See also #491 which may make sense to fix at the same time as this one.

  3. added
    enhancement
    metadata gem
    on Oct 27, 2022
  4. AArnott commented on May 9, 2023

    @AArnott
    MemberAuthor

    Reopening so I can review all the notes here to ensure everything is truly resolved.

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

    enhancementmetadata gem

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions