microsoft / microsoft/CsWin32

GetDC returns HDC but it should return a SafeHandle that calls ReleaseDC

Open
#209 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
2.5k
Forks
124
Avg merge
1d 3h
Merged PRs (30d)
9

Description

I would also really love to have a public HWnd property on the safe handle. It should be stored in the safe handle anyway in order to call ReleaseDC, and it's also very useful in cases like this:

windowDC.Dispose();
windowDC = User32.GetDC(windowDC.HWnd);

The current workaround is to say new ReleaseDCSafeHandle(hwnd, PInvoke.GetDC(hwnd)) instead of PInvoke.GetDC(hwnd):

internal sealed class ReleaseDCSafeHandle : DeleteDCSafeHandle
{
    public ReleaseDCSafeHandle(HWND hWnd, IntPtr handle)
        : base(handle)
    {
        HWnd = hWnd;
    }

    public HWND HWnd { get; }

    protected override bool ReleaseHandle() => ReleaseDC(HWnd, handle);

    /// <summary>
    /// <seealso href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-releasedc"/>
    /// </summary>
    [DllImport("user32.dll")]
    private static extern bool ReleaseDC(HWND hWnd, IntPtr hDC);
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the generated User32.GetDC entry point and the existing ReleaseDCSafeHandle workaround shown in the issue. Check how the returned handle is disposed and how ReleaseDC receives the window handle; done means GetDC returns a safe handle with a public HWnd property and disposal releases the DC correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop-dev, operating-systems
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.