Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Cleanup GDI interop in System.Drawing.Common #39727

Merged
merged 5 commits into from
Nov 1, 2019
Merged

Cleanup GDI interop in System.Drawing.Common #39727

merged 5 commits into from
Nov 1, 2019

Conversation

hughbe
Copy link

@hughbe hughbe commented Jul 24, 2019

No description provided.

@karelz karelz added this to the 5.0 milestone Aug 3, 2019
@danmoseley
Copy link
Member

@hughbe could you please resolve conflicts then ping Jeremy?

@hughbe
Copy link
Author

hughbe commented Sep 7, 2019

@JeremyKuhne ready for your eyes :)

Copy link
Member

@JeremyKuhne JeremyKuhne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! I left some comments on existing code that would be nice to clean up, but I don't think it's blocking. The try blocks in particular would be good to clean up, but can be done in another change. It would be nice to make the arguments for the imports match the current documentation, but again, I don't think it is blocking.

src/Common/src/Interop/Windows/Gdi32/Interop.CreateDC.cs Outdated Show resolved Hide resolved
src/Common/src/Interop/Windows/Gdi32/Interop.CombineRgn.cs Outdated Show resolved Hide resolved
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateFontIndirectW(ref User32.LOGFONT lf);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the argument is lplf

internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateICW(string lpszDriverName, string lpszDeviceName, string lpszOutput, IntPtr lpInitData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: arguments are pszDriver, pszDevice, pszPort, pdm

internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true)]
public static extern bool DeleteObject(IntPtr hObject);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument is ho

}

[DllImport(Libraries.Gdi32, ExactSpelling = true)]
public static extern IntPtr GetStockObject(StockObject nIndex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument is i

{
IntPtr hTempRgn = Interop.Gdi32.CreateRectRgn(0, 0, 0, 0);
IntPtr hSaveRgn = IntPtr.Zero;
try
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try..finally here isn't useful. There would have to be some catastrophic failure (no memory maybe) and at that point a dangled GDI object isn't a terrible thing, if we could manage to delete it anyway.

{
try
{
Interop.Gdi32.SelectClipRgn(new HandleRef(null, hDC), new HandleRef(null, hRgn));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandleRef with a null is pointless. The try block doesn't help here either as it shouldn't throw "normally" (see the comment above).

If these methods are only called in one place it might be better to move them inline.

{
return Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), capability);
}
catch (InvalidPrinterException)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't happen here- we should remove the try..catch.

}
#if TRACK_HDC
Debug.WriteLine( DbgUtil.StackTraceToStr( string.Format("DeviceContext( hDC=0x{0:X8}, Type={1} )", unchecked((int) hDC), dcType) ));
Debug.WriteLine(DbgUtil.StackTraceToStr($"DeviceContext(hDC=0x{unchecked((int)hDC):X8}, Type={dcType})"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unchecked isn't necessary

@safern
Copy link
Member

safern commented Nov 1, 2019

I pushed to address feedback and unblock this PR.

@safern safern merged commit f807df6 into dotnet:master Nov 1, 2019
@hughbe hughbe deleted the drawing-interop branch November 2, 2019 10:51
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Cleanup GDI object Interop

* Cleanup GDI region interop

* Cleanup GDI font interop

* Cleanup GDI DC interop

* Address PR feedback


Commit migrated from dotnet/corefx@f807df6
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants