From f20d19d2d726146c2e840ffbc2d36d55188d9a0e Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Wed, 10 Mar 2021 18:02:10 -0800 Subject: [PATCH] Don't include windows PInvokes on Unix for System.Drawing (#49405) * Don't include windows PInvokes on Unix for System.Drawing * More interop cleanup to avoid building windows only codepaths on Unix * Make lower range use handletreshold to make test more resilient --- .../src/Interop/Unix/Interop.Libraries.cs | 10 + .../src/Interop/Windows/Interop.Comdlg32.cs | 65 ++++ .../src/Interop/Windows/Interop.Gdi32.cs | 195 ++++++++++++ .../src/Interop/Windows/Interop.Kernel32.cs | 25 ++ .../src/Interop/Windows/Interop.Libraries.cs | 13 + .../src/Interop/Windows/Interop.Shell32.cs | 14 + .../src/Interop/Windows/Interop.User32.cs | 42 +++ .../src/Interop/Windows/Interop.Winspool.cs | 23 ++ .../src/System.Drawing.Common.csproj | 13 +- .../BufferedGraphicsContext.Windows.cs | 22 +- .../src/System/Drawing/Font.Windows.cs | 2 +- .../src/System/Drawing/Gdiplus.cs | 282 +----------------- .../src/System/Drawing/Graphics.Unix.cs | 6 +- .../src/System/Drawing/Graphics.Windows.cs | 34 +++ .../src/System/Drawing/Graphics.cs | 34 --- .../src/System/Drawing/Icon.Windows.cs | 42 +-- .../src/System/Drawing/NativeMethods.cs | 19 -- .../Printing/DefaultPrintController.cs | 12 +- .../Drawing/Printing/PageSettings.Windows.cs | 20 +- .../Printing/PrinterSettings.Windows.cs | 60 ++-- .../src/System/Drawing/SystemFonts.Windows.cs | 4 +- .../src/System/Drawing/SystemIcons.Windows.cs | 2 +- .../Text/PrivateFontCollection.Windows.cs | 2 +- .../src/System/Drawing/UnsafeNativeMethods.cs | 16 - .../src/misc/ExternDll.Unix.cs | 18 -- .../src/misc/ExternDll.cs | 17 -- .../tests/GdiPlusHandlesTests.cs | 2 +- 27 files changed, 519 insertions(+), 475 deletions(-) create mode 100644 src/libraries/System.Drawing.Common/src/Interop/Unix/Interop.Libraries.cs create mode 100644 src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Comdlg32.cs create mode 100644 src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Gdi32.cs create mode 100644 src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Kernel32.cs create mode 100644 src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Libraries.cs create mode 100644 src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Shell32.cs create mode 100644 src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.User32.cs create mode 100644 src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Winspool.cs delete mode 100644 src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs delete mode 100644 src/libraries/System.Drawing.Common/src/misc/ExternDll.Unix.cs delete mode 100644 src/libraries/System.Drawing.Common/src/misc/ExternDll.cs diff --git a/src/libraries/System.Drawing.Common/src/Interop/Unix/Interop.Libraries.cs b/src/libraries/System.Drawing.Common/src/Interop/Unix/Interop.Libraries.cs new file mode 100644 index 0000000000000..b8330fee528b7 --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/Interop/Unix/Interop.Libraries.cs @@ -0,0 +1,10 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +internal static partial class Interop +{ + internal static partial class Libraries + { + internal const string Gdiplus = "gdiplus"; + } +} diff --git a/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Comdlg32.cs b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Comdlg32.cs new file mode 100644 index 0000000000000..57a538561aa4e --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Comdlg32.cs @@ -0,0 +1,65 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Comdlg32 + { + [DllImport(Libraries.Comdlg32, SetLastError = true, CharSet = CharSet.Auto)] + internal static extern bool PrintDlg([In, Out] PRINTDLG lppd); + + [DllImport(Libraries.Comdlg32, SetLastError = true, CharSet = CharSet.Auto)] + internal static extern bool PrintDlg([In, Out] PRINTDLGX86 lppd); + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + internal class PRINTDLG + { + internal int lStructSize; + internal IntPtr hwndOwner; + internal IntPtr hDevMode; + internal IntPtr hDevNames; + internal IntPtr hDC; + internal int Flags; + internal short nFromPage; + internal short nToPage; + internal short nMinPage; + internal short nMaxPage; + internal short nCopies; + internal IntPtr hInstance; + internal IntPtr lCustData; + internal IntPtr lpfnPrintHook; + internal IntPtr lpfnSetupHook; + internal string? lpPrintTemplateName; + internal string? lpSetupTemplateName; + internal IntPtr hPrintTemplate; + internal IntPtr hSetupTemplate; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)] + internal class PRINTDLGX86 + { + internal int lStructSize; + internal IntPtr hwndOwner; + internal IntPtr hDevMode; + internal IntPtr hDevNames; + internal IntPtr hDC; + internal int Flags; + internal short nFromPage; + internal short nToPage; + internal short nMinPage; + internal short nMaxPage; + internal short nCopies; + internal IntPtr hInstance; + internal IntPtr lCustData; + internal IntPtr lpfnPrintHook; + internal IntPtr lpfnSetupHook; + internal string? lpPrintTemplateName; + internal string? lpSetupTemplateName; + internal IntPtr hPrintTemplate; + internal IntPtr hSetupTemplate; + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Gdi32.cs b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Gdi32.cs new file mode 100644 index 0000000000000..875dabd9ef392 --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Gdi32.cs @@ -0,0 +1,195 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Gdi32 + { + internal const int QUERYESCSUPPORT = 8; + internal const int CHECKJPEGFORMAT = 4119; + internal const int CHECKPNGFORMAT = 4120; + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true)] + internal static extern IntPtr CreateCompatibleBitmap(HandleRef hDC, int width, int height); + + [DllImport(Libraries.Gdi32)] + internal static extern int GetDIBits(HandleRef hdc, HandleRef hbm, int arg1, int arg2, IntPtr arg3, ref BITMAPINFO_FLAT bmi, int arg5); + + [DllImport(Libraries.Gdi32)] + internal static extern uint GetPaletteEntries(HandleRef hpal, int iStartIndex, int nEntries, byte[] lppe); + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true)] + internal static extern IntPtr CreateDIBSection(HandleRef hdc, ref BITMAPINFO_FLAT bmi, int iUsage, ref IntPtr ppvBits, IntPtr hSection, int dwOffset); + + [DllImport(Libraries.Gdi32, SetLastError = true, CharSet = CharSet.Auto)] + internal static extern int StartDoc(HandleRef hDC, DOCINFO lpDocInfo); + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + internal static extern int StartPage(HandleRef hDC); + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + internal static extern int EndPage(HandleRef hDC); + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + internal static extern int AbortDoc(HandleRef hDC); + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + internal static extern int EndDoc(HandleRef hDC); + + [DllImport(Libraries.Gdi32, SetLastError = true, CharSet = CharSet.Auto)] + internal static extern IntPtr /*HDC*/ ResetDC(HandleRef hDC, HandleRef /*DEVMODE*/ lpDevMode); + + [DllImport(Libraries.Gdi32, SetLastError = true, CharSet = CharSet.Auto)] + internal static extern int AddFontResourceEx(string lpszFilename, int fl, IntPtr pdv); + + internal static int AddFontFile(string fileName) + { + return AddFontResourceEx(fileName, /*FR_PRIVATE*/ 0x10, IntPtr.Zero); + } + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + internal static extern int ExtEscape(HandleRef hDC, int nEscape, int cbInput, ref int inData, int cbOutput, [Out] out int outData); + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + internal static extern int ExtEscape(HandleRef hDC, int nEscape, int cbInput, byte[] inData, int cbOutput, [Out] out int outData); + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + internal static extern int IntersectClipRect(HandleRef hDC, int x1, int y1, int x2, int y2); + + [DllImport(Libraries.Gdi32, SetLastError = true)] + internal static extern int GetObject(HandleRef hObject, int nSize, ref BITMAP bm); + + [DllImport(Libraries.Gdi32, SetLastError = true, CharSet = CharSet.Unicode)] + internal static extern int GetObject(HandleRef hObject, int nSize, ref Interop.User32.LOGFONT lf); + + internal static unsafe int GetObject(HandleRef hObject, ref Interop.User32.LOGFONT lp) + => GetObject(hObject, sizeof(Interop.User32.LOGFONT), ref lp); + + [StructLayout(LayoutKind.Sequential)] + public struct BITMAP + { + public uint bmType; + public uint bmWidth; + public uint bmHeight; + public uint bmWidthBytes; + public ushort bmPlanes; + public ushort bmBitsPixel; + public IntPtr bmBits; + } + + internal const int BITMAPINFO_MAX_COLORSIZE = 256; + + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct BITMAPINFO_FLAT + { + public int bmiHeader_biSize; // = sizeof(BITMAPINFOHEADER) + public int bmiHeader_biWidth; + public int bmiHeader_biHeight; + public short bmiHeader_biPlanes; + public short bmiHeader_biBitCount; + public int bmiHeader_biCompression; + public int bmiHeader_biSizeImage; + public int bmiHeader_biXPelsPerMeter; + public int bmiHeader_biYPelsPerMeter; + public int bmiHeader_biClrUsed; + public int bmiHeader_biClrImportant; + + public fixed byte bmiColors[BITMAPINFO_MAX_COLORSIZE * 4]; // RGBQUAD structs... Blue-Green-Red-Reserved, repeat... + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + internal class DOCINFO + { + internal int cbSize = 20; + internal string? lpszDocName; + internal string? lpszOutput; + internal string? lpszDatatype; + internal int fwType; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + public class DEVMODE + { + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] + public string? dmDeviceName; + public short dmSpecVersion; + public short dmDriverVersion; + public short dmSize; + public short dmDriverExtra; + public int dmFields; + public short dmOrientation; + public short dmPaperSize; + public short dmPaperLength; + public short dmPaperWidth; + public short dmScale; + public short dmCopies; + public short dmDefaultSource; + public short dmPrintQuality; + public short dmColor; + public short dmDuplex; + public short dmYResolution; + public short dmTTOption; + public short dmCollate; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] + public string? dmFormName; + public short dmLogPixels; + public int dmBitsPerPel; + public int dmPelsWidth; + public int dmPelsHeight; + public int dmDisplayFlags; + public int dmDisplayFrequency; + public int dmICMMethod; + public int dmICMIntent; + public int dmMediaType; + public int dmDitherType; + public int dmICCManufacturer; + public int dmICCModel; + public int dmPanningWidth; + public int dmPanningHeight; + + + public override string ToString() + { + return "[DEVMODE: " + + "dmDeviceName=" + dmDeviceName + + ", dmSpecVersion=" + dmSpecVersion + + ", dmDriverVersion=" + dmDriverVersion + + ", dmSize=" + dmSize + + ", dmDriverExtra=" + dmDriverExtra + + ", dmFields=" + dmFields + + ", dmOrientation=" + dmOrientation + + ", dmPaperSize=" + dmPaperSize + + ", dmPaperLength=" + dmPaperLength + + ", dmPaperWidth=" + dmPaperWidth + + ", dmScale=" + dmScale + + ", dmCopies=" + dmCopies + + ", dmDefaultSource=" + dmDefaultSource + + ", dmPrintQuality=" + dmPrintQuality + + ", dmColor=" + dmColor + + ", dmDuplex=" + dmDuplex + + ", dmYResolution=" + dmYResolution + + ", dmTTOption=" + dmTTOption + + ", dmCollate=" + dmCollate + + ", dmFormName=" + dmFormName + + ", dmLogPixels=" + dmLogPixels + + ", dmBitsPerPel=" + dmBitsPerPel + + ", dmPelsWidth=" + dmPelsWidth + + ", dmPelsHeight=" + dmPelsHeight + + ", dmDisplayFlags=" + dmDisplayFlags + + ", dmDisplayFrequency=" + dmDisplayFrequency + + ", dmICMMethod=" + dmICMMethod + + ", dmICMIntent=" + dmICMIntent + + ", dmMediaType=" + dmMediaType + + ", dmDitherType=" + dmDitherType + + ", dmICCManufacturer=" + dmICCManufacturer + + ", dmICCModel=" + dmICCModel + + ", dmPanningWidth=" + dmPanningWidth + + ", dmPanningHeight=" + dmPanningHeight + + "]"; + } + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Kernel32.cs b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Kernel32.cs new file mode 100644 index 0000000000000..d61946a8d1d49 --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Kernel32.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Kernel32 + { + [DllImport(Libraries.Kernel32, SetLastError = true)] + public static extern int GetSystemDefaultLCID(); + + [DllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true, EntryPoint = "GlobalAlloc", CharSet = CharSet.Auto)] + internal static extern IntPtr IntGlobalAlloc(int uFlags, UIntPtr dwBytes); // size should be 32/64bits compatible + + internal static IntPtr GlobalAlloc(int uFlags, uint dwBytes) + { + return IntGlobalAlloc(uFlags, new UIntPtr(dwBytes)); + } + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + internal static extern IntPtr SelectObject(HandleRef hdc, HandleRef obj); + } +} \ No newline at end of file diff --git a/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Libraries.cs b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Libraries.cs new file mode 100644 index 0000000000000..9d7c8110206f1 --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Libraries.cs @@ -0,0 +1,13 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +internal static partial class Interop +{ + internal static partial class Libraries + { + internal const string Comdlg32 = "comdlg32.dll"; + internal const string Gdiplus = "gdiplus.dll"; + internal const string Oleaut32 = "oleaut32.dll"; + internal const string Winspool = "winspool.drv"; + } +} diff --git a/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Shell32.cs b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Shell32.cs new file mode 100644 index 0000000000000..8122f6234cc8a --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Shell32.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Shell32 + { + [DllImport(Libraries.Shell32, CharSet = CharSet.Unicode)] + internal static extern unsafe IntPtr ExtractAssociatedIcon(HandleRef hInst, char* iconPath, ref int index); + } +} \ No newline at end of file diff --git a/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.User32.cs b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.User32.cs new file mode 100644 index 0000000000000..e00bfa9e1a9b0 --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.User32.cs @@ -0,0 +1,42 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, SetLastError = true, CharSet = CharSet.Unicode)] + internal static extern IntPtr LoadIcon(HandleRef hInst, IntPtr iconId); + + [DllImport(Libraries.User32, SetLastError = true, ExactSpelling = true)] + internal static extern bool DestroyIcon(HandleRef hIcon); + + [DllImport(Libraries.User32, SetLastError = true, ExactSpelling = true)] + internal static extern IntPtr CopyImage(HandleRef hImage, int uType, int cxDesired, int cyDesired, int fuFlags); + + [DllImport(Libraries.User32, SetLastError = true, ExactSpelling = true)] + internal static extern bool GetIconInfo(HandleRef hIcon, ref ICONINFO info); + + [DllImport(Libraries.User32, SetLastError = true, ExactSpelling = true)] + public static extern int GetSystemMetrics(int nIndex); + + [DllImport(Libraries.User32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + internal static extern bool DrawIconEx(HandleRef hDC, int x, int y, HandleRef hIcon, int width, int height, int iStepIfAniCursor, HandleRef hBrushFlickerFree, int diFlags); + + [DllImport(Libraries.User32, ExactSpelling = true, SetLastError = true)] + internal static extern unsafe IntPtr CreateIconFromResourceEx(byte* pbIconBits, uint cbIconBits, bool fIcon, int dwVersion, int csDesired, int cyDesired, int flags); + + [StructLayout(LayoutKind.Sequential)] + internal struct ICONINFO + { + internal uint fIcon; + internal uint xHotspot; + internal uint yHotspot; + internal IntPtr hbmMask; + internal IntPtr hbmColor; + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Winspool.cs b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Winspool.cs new file mode 100644 index 0000000000000..05f50b02a5af2 --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Winspool.cs @@ -0,0 +1,23 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Winspool + { + [DllImport(Libraries.Winspool, SetLastError = true, CharSet = CharSet.Auto)] + internal static extern int DeviceCapabilities(string pDevice, string pPort, short fwCapabilities, IntPtr pOutput, IntPtr /*DEVMODE*/ pDevMode); + + [DllImport(Libraries.Winspool, SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)] + internal static extern int DocumentProperties(HandleRef hwnd, HandleRef hPrinter, string pDeviceName, IntPtr /*DEVMODE*/ pDevModeOutput, HandleRef /*DEVMODE*/ pDevModeInput, int fMode); + + [DllImport(Libraries.Winspool, SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)] + internal static extern int DocumentProperties(HandleRef hwnd, HandleRef hPrinter, string pDeviceName, IntPtr /*DEVMODE*/ pDevModeOutput, IntPtr /*DEVMODE*/ pDevModeInput, int fMode); + + [DllImport(Libraries.Winspool, SetLastError = true, CharSet = CharSet.Auto)] + internal static extern int EnumPrinters(int flags, string? name, int level, IntPtr pPrinterEnum/*buffer*/, int cbBuf, out int pcbNeeded, out int pcReturned); + } +} \ No newline at end of file diff --git a/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj b/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj index 28b06d57e22bd..70519aab76611 100644 --- a/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj +++ b/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj @@ -115,7 +115,6 @@ - @@ -222,13 +221,19 @@ - + + + + + + + - - + placeholder.ico diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/BufferedGraphicsContext.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/BufferedGraphicsContext.Windows.cs index 23919d31110d7..0f492fc7bbd3e 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/BufferedGraphicsContext.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/BufferedGraphicsContext.Windows.cs @@ -88,7 +88,7 @@ private BufferedGraphics AllocBuffer(Graphics? targetGraphics, IntPtr targetDC, /// table or bitmasks, as appropriate. /// /// True if successful, false otherwise. - private unsafe bool FillBitmapInfo(IntPtr hdc, IntPtr hpal, ref NativeMethods.BITMAPINFO_FLAT pbmi) + private unsafe bool FillBitmapInfo(IntPtr hdc, IntPtr hpal, ref Interop.Gdi32.BITMAPINFO_FLAT pbmi) { IntPtr hbm = IntPtr.Zero; bool bRet = false; @@ -96,7 +96,7 @@ private unsafe bool FillBitmapInfo(IntPtr hdc, IntPtr hpal, ref NativeMethods.BI { // Create a dummy bitmap from which we can query color format info // about the device surface. - hbm = SafeNativeMethods.CreateCompatibleBitmap(new HandleRef(null, hdc), 1, 1); + hbm = Interop.Gdi32.CreateCompatibleBitmap(new HandleRef(null, hdc), 1, 1); if (hbm == IntPtr.Zero) { @@ -106,7 +106,7 @@ private unsafe bool FillBitmapInfo(IntPtr hdc, IntPtr hpal, ref NativeMethods.BI pbmi.bmiHeader_biSize = sizeof(NativeMethods.BITMAPINFOHEADER); // Call first time to fill in BITMAPINFO header. - SafeNativeMethods.GetDIBits(new HandleRef(null, hdc), + Interop.Gdi32.GetDIBits(new HandleRef(null, hdc), new HandleRef(null, hbm), 0, 0, @@ -123,7 +123,7 @@ private unsafe bool FillBitmapInfo(IntPtr hdc, IntPtr hpal, ref NativeMethods.BI if (pbmi.bmiHeader_biCompression == NativeMethods.BI_BITFIELDS) { // Call a second time to get the color masks. - SafeNativeMethods.GetDIBits(new HandleRef(null, hdc), + Interop.Gdi32.GetDIBits(new HandleRef(null, hdc), new HandleRef(null, hbm), 0, pbmi.bmiHeader_biHeight, @@ -152,7 +152,7 @@ private unsafe bool FillBitmapInfo(IntPtr hdc, IntPtr hpal, ref NativeMethods.BI /// Note: call only valid for displays of 8bpp or less. /// /// True is successful, false otherwise. - private unsafe bool FillColorTable(IntPtr hdc, IntPtr hpal, ref NativeMethods.BITMAPINFO_FLAT pbmi) + private unsafe bool FillColorTable(IntPtr hdc, IntPtr hpal, ref Interop.Gdi32.BITMAPINFO_FLAT pbmi) { byte[] aj = new byte[sizeof(NativeMethods.PALETTEENTRY) * 256]; @@ -172,11 +172,11 @@ private unsafe bool FillColorTable(IntPtr hdc, IntPtr hpal, ref NativeMethods.BI if (hpal == IntPtr.Zero) { palHalftone = Graphics.GetHalftonePalette(); - palRet = SafeNativeMethods.GetPaletteEntries(new HandleRef(null, palHalftone), 0, cColors, aj); + palRet = Interop.Gdi32.GetPaletteEntries(new HandleRef(null, palHalftone), 0, cColors, aj); } else { - palRet = SafeNativeMethods.GetPaletteEntries(new HandleRef(null, hpal), 0, cColors, aj); + palRet = Interop.Gdi32.GetPaletteEntries(new HandleRef(null, hpal), 0, cColors, aj); } if (palRet != 0) @@ -225,7 +225,7 @@ private Graphics CreateBuffer(IntPtr src, int offsetX, int offsetY, int width, i } // Select the bitmap. - _oldBitmap = SafeNativeMethods.SelectObject(new HandleRef(this, _compatDC), new HandleRef(this, _dib)); + _oldBitmap = Interop.Kernel32.SelectObject(new HandleRef(this, _compatDC), new HandleRef(this, _dib)); // Create compat graphics. _compatGraphics = Graphics.FromHdcInternal(_compatDC); @@ -257,7 +257,7 @@ private IntPtr CreateCompatibleDIB(IntPtr hdc, IntPtr hpal, int ulWidth, int ulH } IntPtr hbmRet = IntPtr.Zero; - NativeMethods.BITMAPINFO_FLAT pbmi = default; + Interop.Gdi32.BITMAPINFO_FLAT pbmi = default; // Validate hdc. Interop.Gdi32.ObjectType objType = Interop.Gdi32.GetObjectType(hdc); @@ -301,7 +301,7 @@ private IntPtr CreateCompatibleDIB(IntPtr hdc, IntPtr hpal, int ulWidth, int ulH // Create the DIB section. Let Win32 allocate the memory and return // a pointer to the bitmap surface. - hbmRet = SafeNativeMethods.CreateDIBSection(new HandleRef(null, hdc), ref pbmi, NativeMethods.DIB_RGB_COLORS, ref ppvBits, IntPtr.Zero, 0); + hbmRet = Interop.Gdi32.CreateDIBSection(new HandleRef(null, hdc), ref pbmi, NativeMethods.DIB_RGB_COLORS, ref ppvBits, IntPtr.Zero, 0); Win32Exception? ex = null; if (hbmRet == IntPtr.Zero) { @@ -324,7 +324,7 @@ private void DisposeDC() { if (_oldBitmap != IntPtr.Zero && _compatDC != IntPtr.Zero) { - SafeNativeMethods.SelectObject(new HandleRef(this, _compatDC), new HandleRef(this, _oldBitmap)); + Interop.Kernel32.SelectObject(new HandleRef(this, _compatDC), new HandleRef(this, _oldBitmap)); _oldBitmap = IntPtr.Zero; } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Windows.cs index 178706128c748..46bca48be4c06 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Windows.cs @@ -227,7 +227,7 @@ private void Initialize(FontFamily family, float emSize, FontStyle style, Graphi public static Font FromHfont(IntPtr hfont) { Interop.User32.LOGFONT logFont = default; - SafeNativeMethods.GetObject(new HandleRef(null, hfont), ref logFont); + Interop.Gdi32.GetObject(new HandleRef(null, hfont), ref logFont); using (ScreenDC dc = ScreenDC.Create()) { diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs index ab106b168914b..f55599f6590c0 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs @@ -156,8 +156,6 @@ internal static Exception StatusException(int status) } } - public const int ERROR_CANCELLED = 1223; - public const int E_UNEXPECTED = unchecked((int)0x8000FFFF), E_NOTIMPL = unchecked((int)0x80004001), @@ -363,85 +361,10 @@ public const int SM_CYICON = 12, DEFAULT_CHARSET = 1; - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr CreateCompatibleBitmap(HandleRef hDC, int width, int height); - - [DllImport(ExternDll.Gdi32)] - public static extern int GetDIBits(HandleRef hdc, HandleRef hbm, int arg1, int arg2, IntPtr arg3, ref NativeMethods.BITMAPINFO_FLAT bmi, int arg5); - - [DllImport(ExternDll.Gdi32)] - public static extern uint GetPaletteEntries(HandleRef hpal, int iStartIndex, int nEntries, byte[] lppe); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr CreateDIBSection(HandleRef hdc, ref NativeMethods.BITMAPINFO_FLAT bmi, int iUsage, ref IntPtr ppvBits, IntPtr hSection, int dwOffset); - - [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Auto)] - public static extern int StartDoc(HandleRef hDC, DOCINFO lpDocInfo); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int StartPage(HandleRef hDC); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int EndPage(HandleRef hDC); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int AbortDoc(HandleRef hDC); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int EndDoc(HandleRef hDC); - - [DllImport(ExternDll.Comdlg32, SetLastError = true, CharSet = CharSet.Auto)] - public static extern bool PrintDlg([In, Out] PRINTDLG lppd); - - [DllImport(ExternDll.Comdlg32, SetLastError = true, CharSet = CharSet.Auto)] - public static extern bool PrintDlg([In, Out] PRINTDLGX86 lppd); - - [DllImport(ExternDll.Winspool, SetLastError = true, CharSet = CharSet.Auto)] - public static extern int DeviceCapabilities(string pDevice, string pPort, short fwCapabilities, IntPtr pOutput, IntPtr /*DEVMODE*/ pDevMode); - - [DllImport(ExternDll.Winspool, SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)] - public static extern int DocumentProperties(HandleRef hwnd, HandleRef hPrinter, string pDeviceName, IntPtr /*DEVMODE*/ pDevModeOutput, HandleRef /*DEVMODE*/ pDevModeInput, int fMode); - - [DllImport(ExternDll.Winspool, SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)] - public static extern int DocumentProperties(HandleRef hwnd, HandleRef hPrinter, string pDeviceName, IntPtr /*DEVMODE*/ pDevModeOutput, IntPtr /*DEVMODE*/ pDevModeInput, int fMode); - - [DllImport(ExternDll.Winspool, SetLastError = true, CharSet = CharSet.Auto)] - public static extern int EnumPrinters(int flags, string? name, int level, IntPtr pPrinterEnum/*buffer*/, - int cbBuf, out int pcbNeeded, out int pcReturned); - - [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Auto)] - public static extern IntPtr /*HDC*/ ResetDC(HandleRef hDC, HandleRef /*DEVMODE*/ lpDevMode); - - [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Auto)] - public static extern int AddFontResourceEx(string lpszFilename, int fl, IntPtr pdv); - - public static int AddFontFile(string fileName) - { - return AddFontResourceEx(fileName, /*FR_PRIVATE*/ 0x10, IntPtr.Zero); - } - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int ExtEscape(HandleRef hDC, int nEscape, int cbInput, ref int inData, int cbOutput, [Out] out int outData); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int ExtEscape(HandleRef hDC, int nEscape, int cbInput, byte[] inData, int cbOutput, [Out] out int outData); - - public const int QUERYESCSUPPORT = 8, CHECKJPEGFORMAT = 4119, CHECKPNGFORMAT = 4120; - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int IntersectClipRect(HandleRef hDC, int x1, int y1, int x2, int y2); - - [DllImport(ExternDll.Kernel32, SetLastError = true, ExactSpelling = true, EntryPoint = "GlobalAlloc", CharSet = CharSet.Auto)] - public static extern IntPtr IntGlobalAlloc(int uFlags, UIntPtr dwBytes); // size should be 32/64bits compatible - - public static IntPtr GlobalAlloc(int uFlags, uint dwBytes) - { - return IntGlobalAlloc(uFlags, new UIntPtr(dwBytes)); - } - public const int ERROR_ACCESS_DENIED = 5; public const int ERROR_INVALID_PARAMETER = 87; public const int ERROR_PROC_NOT_FOUND = 127; + public const int ERROR_CANCELLED = 1223; [StructLayout(LayoutKind.Sequential)] public class ENHMETAHEADER @@ -482,86 +405,6 @@ public class ENHMETAHEADER public int bOpenGL; } - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - public class DOCINFO - { - public int cbSize = 20; - public string? lpszDocName; - public string? lpszOutput; - public string? lpszDatatype; - public int fwType; - } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - public class PRINTDLG - { - public int lStructSize; - public IntPtr hwndOwner; - public IntPtr hDevMode; - public IntPtr hDevNames; - public IntPtr hDC; - public int Flags; - public short nFromPage; - public short nToPage; - public short nMinPage; - public short nMaxPage; - public short nCopies; - public IntPtr hInstance; - public IntPtr lCustData; - public IntPtr lpfnPrintHook; - public IntPtr lpfnSetupHook; - public string? lpPrintTemplateName; - public string? lpSetupTemplateName; - public IntPtr hPrintTemplate; - public IntPtr hSetupTemplate; - } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)] - public class PRINTDLGX86 - { - public int lStructSize; - public IntPtr hwndOwner; - public IntPtr hDevMode; - public IntPtr hDevNames; - public IntPtr hDC; - public int Flags; - public short nFromPage; - public short nToPage; - public short nMinPage; - public short nMaxPage; - public short nCopies; - public IntPtr hInstance; - public IntPtr lCustData; - public IntPtr lpfnPrintHook; - public IntPtr lpfnSetupHook; - public string? lpPrintTemplateName; - public string? lpSetupTemplateName; - public IntPtr hPrintTemplate; - public IntPtr hSetupTemplate; - } - - [StructLayout(LayoutKind.Sequential)] - public struct ICONINFO - { - public uint fIcon; - public uint xHotspot; - public uint yHotspot; - public IntPtr hbmMask; - public IntPtr hbmColor; - } - - [StructLayout(LayoutKind.Sequential)] - public struct BITMAP - { - public uint bmType; - public uint bmWidth; - public uint bmHeight; - public uint bmWidthBytes; - public ushort bmPlanes; - public ushort bmBitsPixel; - public IntPtr bmBits; - } - // https://devblogs.microsoft.com/oldnewthing/20101018-00/?p=12513 // https://devblogs.microsoft.com/oldnewthing/20120720-00/?p=7083 @@ -592,128 +435,5 @@ public struct ICONDIRENTRY public uint dwBytesInRes; public uint dwImageOffset; } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - public class DEVMODE - { - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] - public string? dmDeviceName; - public short dmSpecVersion; - public short dmDriverVersion; - public short dmSize; - public short dmDriverExtra; - public int dmFields; - public short dmOrientation; - public short dmPaperSize; - public short dmPaperLength; - public short dmPaperWidth; - public short dmScale; - public short dmCopies; - public short dmDefaultSource; - public short dmPrintQuality; - public short dmColor; - public short dmDuplex; - public short dmYResolution; - public short dmTTOption; - public short dmCollate; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] - public string? dmFormName; - public short dmLogPixels; - public int dmBitsPerPel; - public int dmPelsWidth; - public int dmPelsHeight; - public int dmDisplayFlags; - public int dmDisplayFrequency; - public int dmICMMethod; - public int dmICMIntent; - public int dmMediaType; - public int dmDitherType; - public int dmICCManufacturer; - public int dmICCModel; - public int dmPanningWidth; - public int dmPanningHeight; - - - public override string ToString() - { - return "[DEVMODE: " - + "dmDeviceName=" + dmDeviceName - + ", dmSpecVersion=" + dmSpecVersion - + ", dmDriverVersion=" + dmDriverVersion - + ", dmSize=" + dmSize - + ", dmDriverExtra=" + dmDriverExtra - + ", dmFields=" + dmFields - + ", dmOrientation=" + dmOrientation - + ", dmPaperSize=" + dmPaperSize - + ", dmPaperLength=" + dmPaperLength - + ", dmPaperWidth=" + dmPaperWidth - + ", dmScale=" + dmScale - + ", dmCopies=" + dmCopies - + ", dmDefaultSource=" + dmDefaultSource - + ", dmPrintQuality=" + dmPrintQuality - + ", dmColor=" + dmColor - + ", dmDuplex=" + dmDuplex - + ", dmYResolution=" + dmYResolution - + ", dmTTOption=" + dmTTOption - + ", dmCollate=" + dmCollate - + ", dmFormName=" + dmFormName - + ", dmLogPixels=" + dmLogPixels - + ", dmBitsPerPel=" + dmBitsPerPel - + ", dmPelsWidth=" + dmPelsWidth - + ", dmPelsHeight=" + dmPelsHeight - + ", dmDisplayFlags=" + dmDisplayFlags - + ", dmDisplayFrequency=" + dmDisplayFrequency - + ", dmICMMethod=" + dmICMMethod - + ", dmICMIntent=" + dmICMIntent - + ", dmMediaType=" + dmMediaType - + ", dmDitherType=" + dmDitherType - + ", dmICCManufacturer=" + dmICCManufacturer - + ", dmICCModel=" + dmICCModel - + ", dmPanningWidth=" + dmPanningWidth - + ", dmPanningHeight=" + dmPanningHeight - + "]"; - } - } - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr SelectObject(HandleRef hdc, HandleRef obj); - - [DllImport(ExternDll.User32, ExactSpelling = true, SetLastError = true)] - public static extern unsafe IntPtr CreateIconFromResourceEx( - byte* pbIconBits, - uint cbIconBits, - bool fIcon, - int dwVersion, - int csDesired, - int cyDesired, - int flags); - - [DllImport(ExternDll.Shell32, CharSet = CharSet.Unicode)] - public static extern unsafe IntPtr ExtractAssociatedIcon(HandleRef hInst, char* iconPath, ref int index); - - [DllImport(ExternDll.User32, SetLastError = true, CharSet = CharSet.Unicode)] - public static extern IntPtr LoadIcon(HandleRef hInst, IntPtr iconId); - - [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] - public static extern bool DestroyIcon(HandleRef hIcon); - - [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr CopyImage(HandleRef hImage, int uType, int cxDesired, int cyDesired, int fuFlags); - - // GetObject stuff - [DllImport(ExternDll.Gdi32, SetLastError = true)] - public static extern int GetObject(HandleRef hObject, int nSize, ref BITMAP bm); - - [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Unicode)] - public static extern int GetObject(HandleRef hObject, int nSize, ref Interop.User32.LOGFONT lf); - - public static unsafe int GetObject(HandleRef hObject, ref Interop.User32.LOGFONT lp) - => GetObject(hObject, sizeof(Interop.User32.LOGFONT), ref lp); - - [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] - public static extern bool GetIconInfo(HandleRef hIcon, ref ICONINFO info); - - [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool DrawIconEx(HandleRef hDC, int x, int y, HandleRef hIcon, int width, int height, int iStepIfAniCursor, HandleRef hBrushFlickerFree, int diFlags); } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Unix.cs index a8492d7ffcab7..3da1617e5979b 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Unix.cs @@ -578,8 +578,12 @@ public RectangleF VisibleClipBounds [EditorBrowsable(EditorBrowsableState.Never)] public object GetContextInfo() { - // only known source of information @ http://blogs.wdevs.com/jdunlap/Default.aspx throw new NotImplementedException(); } + + private void CheckErrorStatus(int status) + { + Gdip.CheckStatus(status); + } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Windows.cs index e78929e19379c..f523e1c4e58b5 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Windows.cs @@ -918,5 +918,39 @@ private static void OnDomainUnload(object? sender, EventArgs e) s_halftonePalette = IntPtr.Zero; } } + + /// + /// GDI+ will return a 'generic error' with specific win32 last error codes when + /// a terminal server session has been closed, minimized, etc... We don't want + /// to throw when this happens, so we'll guard against this by looking at the + /// 'last win32 error code' and checking to see if it is either 1) access denied + /// or 2) proc not found and then ignore it. + /// + /// The problem is that when you lock the machine, the secure desktop is enabled and + /// rendering fails which is expected (since the app doesn't have permission to draw + /// on the secure desktop). Not sure if there's anything you can do, short of catching + /// the desktop switch message and absorbing all the exceptions that get thrown while + /// it's the secure desktop. + /// + private void CheckErrorStatus(int status) + { + if (status == Gdip.Ok) + return; + + // Generic error from GDI+ can be GenericError or Win32Error. + if (status == Gdip.GenericError || status == Gdip.Win32Error) + { + int error = Marshal.GetLastWin32Error(); + if (error == SafeNativeMethods.ERROR_ACCESS_DENIED || error == SafeNativeMethods.ERROR_PROC_NOT_FOUND || + // Here, we'll check to see if we are in a terminal services session... + (((Interop.User32.GetSystemMetrics(NativeMethods.SM_REMOTESESSION) & 0x00000001) != 0) && (error == 0))) + { + return; + } + } + + // Legitimate error, throw our status exception. + throw Gdip.StatusException(status); + } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs index ba03870d89468..9ab0ebe423784 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs @@ -2453,40 +2453,6 @@ public unsafe void TransformPoints(CoordinateSpace destSpace, CoordinateSpace sr } } - /// - /// GDI+ will return a 'generic error' with specific win32 last error codes when - /// a terminal server session has been closed, minimized, etc... We don't want - /// to throw when this happens, so we'll guard against this by looking at the - /// 'last win32 error code' and checking to see if it is either 1) access denied - /// or 2) proc not found and then ignore it. - /// - /// The problem is that when you lock the machine, the secure desktop is enabled and - /// rendering fails which is expected (since the app doesn't have permission to draw - /// on the secure desktop). Not sure if there's anything you can do, short of catching - /// the desktop switch message and absorbing all the exceptions that get thrown while - /// it's the secure desktop. - /// - private void CheckErrorStatus(int status) - { - if (status == Gdip.Ok) - return; - - // Generic error from GDI+ can be GenericError or Win32Error. - if (status == Gdip.GenericError || status == Gdip.Win32Error) - { - int error = Marshal.GetLastWin32Error(); - if (error == SafeNativeMethods.ERROR_ACCESS_DENIED || error == SafeNativeMethods.ERROR_PROC_NOT_FOUND || - // Here, we'll check to see if we are in a terminal services session... - (((UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_REMOTESESSION) & 0x00000001) != 0) && (error == 0))) - { - return; - } - } - - // Legitimate error, throw our status exception. - throw Gdip.StatusException(status); - } - /// /// GDI+ will return a 'generic error' when we attempt to draw an Emf /// image with width/height == 1. Here, we will hack around this by diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs index 2b38e96ae844d..35dd66a8f282b 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs @@ -94,7 +94,7 @@ public Icon(Icon original, int width, int height) : this() if (_iconData == null) { _iconSize = original.Size; - _handle = SafeNativeMethods.CopyImage(new HandleRef(original, original.Handle), SafeNativeMethods.IMAGE_ICON, _iconSize.Width, _iconSize.Height, 0); + _handle = Interop.User32.CopyImage(new HandleRef(original, original.Handle), SafeNativeMethods.IMAGE_ICON, _iconSize.Width, _iconSize.Height, 0); } else { @@ -190,7 +190,7 @@ void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context) fixed (char* b = buffer) { - IntPtr hIcon = SafeNativeMethods.ExtractAssociatedIcon(NativeMethods.NullHandleRef, b, ref index); + IntPtr hIcon = Interop.Shell32.ExtractAssociatedIcon(NativeMethods.NullHandleRef, b, ref index); ArrayPool.Shared.Return(buffer); if (hIcon != IntPtr.Zero) { @@ -223,24 +223,24 @@ public unsafe Size Size { if (_iconSize.IsEmpty) { - SafeNativeMethods.ICONINFO info = default; - SafeNativeMethods.GetIconInfo(new HandleRef(this, Handle), ref info); - SafeNativeMethods.BITMAP bitmap = default; + Interop.User32.ICONINFO info = default; + Interop.User32.GetIconInfo(new HandleRef(this, Handle), ref info); + Interop.Gdi32.BITMAP bitmap = default; if (info.hbmColor != IntPtr.Zero) { - SafeNativeMethods.GetObject( + Interop.Gdi32.GetObject( new HandleRef(null, info.hbmColor), - sizeof(SafeNativeMethods.BITMAP), + sizeof(Interop.Gdi32.BITMAP), ref bitmap); Interop.Gdi32.DeleteObject(info.hbmColor); _iconSize = new Size((int)bitmap.bmWidth, (int)bitmap.bmHeight); } else if (info.hbmMask != IntPtr.Zero) { - SafeNativeMethods.GetObject( + Interop.Gdi32.GetObject( new HandleRef(null, info.hbmMask), - sizeof(SafeNativeMethods.BITMAP), + sizeof(Interop.Gdi32.BITMAP), ref bitmap); _iconSize = new Size((int)bitmap.bmWidth, (int)(bitmap.bmHeight / 2)); } @@ -274,7 +274,7 @@ internal void DestroyHandle() { if (_ownHandle) { - SafeNativeMethods.DestroyIcon(new HandleRef(this, _handle)); + Interop.User32.DestroyIcon(new HandleRef(this, _handle)); _handle = IntPtr.Zero; } } @@ -367,8 +367,8 @@ private void DrawIcon(IntPtr dc, Rectangle imageRect, Rectangle targetRect, bool IntPtr hSaveRgn = SaveClipRgn(dc); try { - SafeNativeMethods.IntersectClipRect(new HandleRef(this, dc), targetX, targetY, targetX + clipWidth, targetY + clipHeight); - SafeNativeMethods.DrawIconEx(new HandleRef(null, dc), + Interop.Gdi32.IntersectClipRect(new HandleRef(this, dc), targetX, targetY, targetX + clipWidth, targetY + clipHeight); + Interop.User32.DrawIconEx(new HandleRef(null, dc), targetX - imageX, targetY - imageY, new HandleRef(this, _handle), @@ -479,12 +479,12 @@ private unsafe void Initialize(int width, int height) // Get the correct width and height. if (width == 0) { - width = UnsafeNativeMethods.GetSystemMetrics(SafeNativeMethods.SM_CXICON); + width = Interop.User32.GetSystemMetrics(SafeNativeMethods.SM_CXICON); } if (height == 0) { - height = UnsafeNativeMethods.GetSystemMetrics(SafeNativeMethods.SM_CYICON); + height = Interop.User32.GetSystemMetrics(SafeNativeMethods.SM_CYICON); } if (s_bitDepth == 0) @@ -614,7 +614,7 @@ private unsafe void Initialize(int width, int height) fixed (byte* pbAlignedBuffer = alignedBuffer) { - _handle = SafeNativeMethods.CreateIconFromResourceEx(pbAlignedBuffer, _bestBytesInRes, true, 0x00030000, 0, 0, 0); + _handle = Interop.User32.CreateIconFromResourceEx(pbAlignedBuffer, _bestBytesInRes, true, 0x00030000, 0, 0, 0); } ArrayPool.Shared.Return(alignedBuffer); } @@ -622,7 +622,7 @@ private unsafe void Initialize(int width, int height) { try { - _handle = SafeNativeMethods.CreateIconFromResourceEx(checked(b + _bestImageOffset), _bestBytesInRes, true, 0x00030000, 0, 0, 0); + _handle = Interop.User32.CreateIconFromResourceEx(checked(b + _bestImageOffset), _bestBytesInRes, true, 0x00030000, 0, 0, 0); } catch (OverflowException) { @@ -770,14 +770,14 @@ private unsafe Bitmap BmpFrame() else if (_bestBitDepth == 0 || _bestBitDepth == 32) { // This may be a 32bpp icon or an icon without any data. - SafeNativeMethods.ICONINFO info = default; - SafeNativeMethods.GetIconInfo(new HandleRef(this, _handle), ref info); - SafeNativeMethods.BITMAP bmp = default; + Interop.User32.ICONINFO info = default; + Interop.User32.GetIconInfo(new HandleRef(this, _handle), ref info); + Interop.Gdi32.BITMAP bmp = default; try { if (info.hbmColor != IntPtr.Zero) { - SafeNativeMethods.GetObject(new HandleRef(null, info.hbmColor), sizeof(SafeNativeMethods.BITMAP), ref bmp); + Interop.Gdi32.GetObject(new HandleRef(null, info.hbmColor), sizeof(Interop.Gdi32.BITMAP), ref bmp); if (bmp.bmBitsPixel == 32) { Bitmap? tmpBitmap = null; @@ -905,7 +905,7 @@ private bool HasPngSignature() public override string ToString() => SR.toStringIcon; - [DllImport(ExternDll.Oleaut32, PreserveSig = false)] + [DllImport(Interop.Libraries.Oleaut32, PreserveSig = false)] internal static extern IPicture OleCreatePictureIndirect(PICTDESC pictdesc, [In]ref Guid refiid, bool fOwn); [ComImport] diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/NativeMethods.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/NativeMethods.cs index 0c34e3a735548..9d08ae770b0d4 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/NativeMethods.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/NativeMethods.cs @@ -16,25 +16,6 @@ internal class NativeMethods internal const int DIB_RGB_COLORS = 0; internal const int BI_BITFIELDS = 3; internal const int BI_RGB = 0; - internal const int BITMAPINFO_MAX_COLORSIZE = 256; - - [StructLayout(LayoutKind.Sequential)] - internal unsafe struct BITMAPINFO_FLAT - { - public int bmiHeader_biSize; // = sizeof(BITMAPINFOHEADER) - public int bmiHeader_biWidth; - public int bmiHeader_biHeight; - public short bmiHeader_biPlanes; - public short bmiHeader_biBitCount; - public int bmiHeader_biCompression; - public int bmiHeader_biSizeImage; - public int bmiHeader_biXPelsPerMeter; - public int bmiHeader_biYPelsPerMeter; - public int bmiHeader_biClrUsed; - public int bmiHeader_biClrImportant; - - public fixed byte bmiColors[BITMAPINFO_MAX_COLORSIZE * 4]; // RGBQUAD structs... Blue-Green-Red-Reserved, repeat... - } [StructLayout(LayoutKind.Sequential)] internal struct BITMAPINFOHEADER diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs index bc7719a0160bd..5ae7765b11ef8 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs @@ -30,7 +30,7 @@ public override void OnStartPrint(PrintDocument document, PrintEventArgs e) throw new InvalidPrinterException(document.PrinterSettings); _dc = document.PrinterSettings.CreateDeviceContext(_modeHandle); - SafeNativeMethods.DOCINFO info = new SafeNativeMethods.DOCINFO(); + Interop.Gdi32.DOCINFO info = new Interop.Gdi32.DOCINFO(); info.lpszDocName = document.DocumentName; if (document.PrinterSettings.PrintToFile) info.lpszOutput = document.PrinterSettings.OutputPort; //This will be "FILE:" @@ -39,7 +39,7 @@ public override void OnStartPrint(PrintDocument document, PrintEventArgs e) info.lpszDatatype = null; info.fwType = 0; - int result = SafeNativeMethods.StartDoc(new HandleRef(_dc, _dc.Hdc), info); + int result = Interop.Gdi32.StartDoc(new HandleRef(_dc, _dc.Hdc), info); if (result <= 0) { int error = Marshal.GetLastWin32Error(); @@ -67,7 +67,7 @@ public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs IntPtr modePointer = Interop.Kernel32.GlobalLock(new HandleRef(this, _modeHandle)); try { - IntPtr result = SafeNativeMethods.ResetDC(new HandleRef(_dc, _dc.Hdc), new HandleRef(null, modePointer)); + IntPtr result = Interop.Gdi32.ResetDC(new HandleRef(_dc, _dc.Hdc), new HandleRef(null, modePointer)); Debug.Assert(result == _dc.Hdc, "ResetDC didn't return the same handle I gave it"); } finally @@ -94,7 +94,7 @@ public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs } - int result2 = SafeNativeMethods.StartPage(new HandleRef(_dc, _dc.Hdc)); + int result2 = Interop.Gdi32.StartPage(new HandleRef(_dc, _dc.Hdc)); if (result2 <= 0) throw new Win32Exception(); return _graphics; @@ -109,7 +109,7 @@ public override void OnEndPage(PrintDocument document, PrintPageEventArgs e) try { - int result = SafeNativeMethods.EndPage(new HandleRef(_dc, _dc.Hdc)); + int result = Interop.Gdi32.EndPage(new HandleRef(_dc, _dc.Hdc)); if (result <= 0) throw new Win32Exception(); } @@ -132,7 +132,7 @@ public override void OnEndPrint(PrintDocument document, PrintEventArgs e) { try { - int result = (e.Cancel) ? SafeNativeMethods.AbortDoc(new HandleRef(_dc, _dc.Hdc)) : SafeNativeMethods.EndDoc(new HandleRef(_dc, _dc.Hdc)); + int result = (e.Cancel) ? Interop.Gdi32.AbortDoc(new HandleRef(_dc, _dc.Hdc)) : Interop.Gdi32.EndDoc(new HandleRef(_dc, _dc.Hdc)); if (result <= 0) throw new Win32Exception(); } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.Windows.cs index e175f515040cc..8ac8e9ce247ac 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.Windows.cs @@ -163,7 +163,7 @@ public PaperSource PaperSource { IntPtr modeHandle = printerSettings.GetHdevmode(); IntPtr modePointer = Interop.Kernel32.GlobalLock(new HandleRef(this, modeHandle)); - SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; PaperSource result = PaperSourceFromMode(mode); @@ -232,7 +232,7 @@ public PrinterResolution PrinterResolution { IntPtr modeHandle = printerSettings.GetHdevmode(); IntPtr modePointer = Interop.Kernel32.GlobalLock(new HandleRef(this, modeHandle)); - SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; PrinterResolution result = PrinterResolutionFromMode(mode); @@ -280,7 +280,7 @@ public object Clone() public void CopyToHdevmode(IntPtr hdevmode) { IntPtr modePointer = Interop.Kernel32.GlobalLock(hdevmode); - SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; if (_color.IsNotDefault && ((mode.dmFields & SafeNativeMethods.DM_COLOR) == SafeNativeMethods.DM_COLOR)) mode.dmColor = unchecked((short)(((bool)_color) ? SafeNativeMethods.DMCOLOR_COLOR : SafeNativeMethods.DMCOLOR_MONOCHROME)); @@ -365,7 +365,7 @@ public void CopyToHdevmode(IntPtr hdevmode) // a buffer overrun if (mode.dmDriverExtra >= ExtraBytes) { - int retCode = SafeNativeMethods.DocumentProperties(NativeMethods.NullHandleRef, NativeMethods.NullHandleRef, printerSettings.PrinterName, modePointer, modePointer, SafeNativeMethods.DM_IN_BUFFER | SafeNativeMethods.DM_OUT_BUFFER); + int retCode = Interop.Winspool.DocumentProperties(NativeMethods.NullHandleRef, NativeMethods.NullHandleRef, printerSettings.PrinterName, modePointer, modePointer, SafeNativeMethods.DM_IN_BUFFER | SafeNativeMethods.DM_OUT_BUFFER); if (retCode < 0) { Interop.Kernel32.GlobalFree(modePointer); @@ -381,7 +381,7 @@ private short ExtraBytes { IntPtr modeHandle = printerSettings.GetHdevmodeInternal(); IntPtr modePointer = Interop.Kernel32.GlobalLock(new HandleRef(this, modeHandle)); - SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; short result = mode?.dmDriverExtra ?? 0; @@ -426,7 +426,7 @@ private PaperSize GetPaperSize(IntPtr modeHandle) } IntPtr modePointer = Interop.Kernel32.GlobalLock(modeHandle); - SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; PaperSize result = PaperSizeFromMode(mode); @@ -443,7 +443,7 @@ private PaperSize GetPaperSize(IntPtr modeHandle) return _paperSize; } - private PaperSize PaperSizeFromMode(SafeNativeMethods.DEVMODE mode) + private PaperSize PaperSizeFromMode(Interop.Gdi32.DEVMODE mode) { PaperSize[] sizes = printerSettings.Get_PaperSizes(); if ((mode.dmFields & SafeNativeMethods.DM_PAPERSIZE) == SafeNativeMethods.DM_PAPERSIZE) @@ -460,7 +460,7 @@ private PaperSize PaperSizeFromMode(SafeNativeMethods.DEVMODE mode) PrinterUnitConvert.Convert(mode.dmPaperLength, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display)); } - private PaperSource PaperSourceFromMode(SafeNativeMethods.DEVMODE mode) + private PaperSource PaperSourceFromMode(Interop.Gdi32.DEVMODE mode) { PaperSource[] sources = printerSettings.Get_PaperSources(); if ((mode.dmFields & SafeNativeMethods.DM_DEFAULTSOURCE) == SafeNativeMethods.DM_DEFAULTSOURCE) @@ -478,7 +478,7 @@ private PaperSource PaperSourceFromMode(SafeNativeMethods.DEVMODE mode) return new PaperSource((PaperSourceKind)mode.dmDefaultSource, "unknown"); } - private PrinterResolution PrinterResolutionFromMode(SafeNativeMethods.DEVMODE mode) + private PrinterResolution PrinterResolutionFromMode(Interop.Gdi32.DEVMODE mode) { PrinterResolution[] resolutions = printerSettings.Get_PrinterResolutions(); for (int i = 0; i < resolutions.Length; i++) @@ -514,7 +514,7 @@ public void SetHdevmode(IntPtr hdevmode) } IntPtr pointer = Interop.Kernel32.GlobalLock(hdevmode); - SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(pointer, typeof(SafeNativeMethods.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(pointer, typeof(Interop.Gdi32.DEVMODE))!; if ((mode.dmFields & SafeNativeMethods.DM_COLOR) == SafeNativeMethods.DM_COLOR) { diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs index e2b3311bc4567..853639a6e5430 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs @@ -188,10 +188,10 @@ public static StringCollection InstalledPrinters int bufferSize; int count; - SafeNativeMethods.EnumPrinters(SafeNativeMethods.PRINTER_ENUM_LOCAL | SafeNativeMethods.PRINTER_ENUM_CONNECTIONS, null, Level, IntPtr.Zero, 0, out bufferSize, out count); + Interop.Winspool.EnumPrinters(SafeNativeMethods.PRINTER_ENUM_LOCAL | SafeNativeMethods.PRINTER_ENUM_CONNECTIONS, null, Level, IntPtr.Zero, 0, out bufferSize, out count); IntPtr buffer = Marshal.AllocCoTaskMem(bufferSize); - int returnCode = SafeNativeMethods.EnumPrinters(SafeNativeMethods.PRINTER_ENUM_LOCAL | SafeNativeMethods.PRINTER_ENUM_CONNECTIONS, + int returnCode = Interop.Winspool.EnumPrinters(SafeNativeMethods.PRINTER_ENUM_LOCAL | SafeNativeMethods.PRINTER_ENUM_CONNECTIONS, null, Level, buffer, bufferSize, out bufferSize, out count); var array = new string[count]; @@ -447,13 +447,13 @@ public bool IsDirectPrintingSupported(ImageFormat imageFormat) bool isDirectPrintingSupported = false; if (imageFormat.Equals(ImageFormat.Jpeg) || imageFormat.Equals(ImageFormat.Png)) { - int nEscape = imageFormat.Equals(ImageFormat.Jpeg) ? SafeNativeMethods.CHECKJPEGFORMAT : SafeNativeMethods.CHECKPNGFORMAT; + int nEscape = imageFormat.Equals(ImageFormat.Jpeg) ? Interop.Gdi32.CHECKJPEGFORMAT : Interop.Gdi32.CHECKPNGFORMAT; int outData = 0; DeviceContext dc = CreateInformationContext(DefaultPageSettings); HandleRef hdc = new HandleRef(dc, dc.Hdc); try { - isDirectPrintingSupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, sizeof(int), ref nEscape, 0, out outData) > 0; + isDirectPrintingSupported = Interop.Gdi32.ExtEscape(hdc, Interop.Gdi32.QUERYESCSUPPORT, sizeof(int), ref nEscape, 0, out outData) > 0; } finally { @@ -482,17 +482,17 @@ public bool IsDirectPrintingSupported(Image image) byte[] pvImage = stream.ToArray(); - int nEscape = image.RawFormat.Equals(ImageFormat.Jpeg) ? SafeNativeMethods.CHECKJPEGFORMAT : SafeNativeMethods.CHECKPNGFORMAT; + int nEscape = image.RawFormat.Equals(ImageFormat.Jpeg) ? Interop.Gdi32.CHECKJPEGFORMAT : Interop.Gdi32.CHECKPNGFORMAT; int outData = 0; DeviceContext dc = CreateInformationContext(DefaultPageSettings); HandleRef hdc = new HandleRef(dc, dc.Hdc); try { - bool querySupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, sizeof(int), ref nEscape, 0, out outData) > 0; + bool querySupported = Interop.Gdi32.ExtEscape(hdc, Interop.Gdi32.QUERYESCSUPPORT, sizeof(int), ref nEscape, 0, out outData) > 0; if (querySupported) { - isDirectPrintingSupported = (SafeNativeMethods.ExtEscape(hdc, nEscape, pvImage.Length, pvImage, sizeof(int), out outData) > 0) + isDirectPrintingSupported = (Interop.Gdi32.ExtEscape(hdc, nEscape, pvImage.Length, pvImage, sizeof(int), out outData) > 0) && (outData == 1); } } @@ -647,10 +647,10 @@ public Graphics CreateMeasurementGraphics(PageSettings pageSettings, bool honorO // Create a PRINTDLG with a few useful defaults. // Try to keep this consistent with PrintDialog.CreatePRINTDLG. - private static SafeNativeMethods.PRINTDLGX86 CreatePRINTDLGX86() + private static Interop.Comdlg32.PRINTDLGX86 CreatePRINTDLGX86() { - SafeNativeMethods.PRINTDLGX86 data = new SafeNativeMethods.PRINTDLGX86(); - data.lStructSize = Marshal.SizeOf(typeof(SafeNativeMethods.PRINTDLGX86)); + Interop.Comdlg32.PRINTDLGX86 data = new Interop.Comdlg32.PRINTDLGX86(); + data.lStructSize = Marshal.SizeOf(typeof(Interop.Comdlg32.PRINTDLGX86)); data.hwndOwner = IntPtr.Zero; data.hDevMode = IntPtr.Zero; data.hDevNames = IntPtr.Zero; @@ -676,10 +676,10 @@ private static SafeNativeMethods.PRINTDLGX86 CreatePRINTDLGX86() // Create a PRINTDLG with a few useful defaults. // Try to keep this consistent with PrintDialog.CreatePRINTDLG. - private static SafeNativeMethods.PRINTDLG CreatePRINTDLG() + private static Interop.Comdlg32.PRINTDLG CreatePRINTDLG() { - SafeNativeMethods.PRINTDLG data = new SafeNativeMethods.PRINTDLG(); - data.lStructSize = Marshal.SizeOf(typeof(SafeNativeMethods.PRINTDLG)); + Interop.Comdlg32.PRINTDLG data = new Interop.Comdlg32.PRINTDLG(); + data.lStructSize = Marshal.SizeOf(typeof(Interop.Comdlg32.PRINTDLG)); data.hwndOwner = IntPtr.Zero; data.hDevMode = IntPtr.Zero; data.hDevNames = IntPtr.Zero; @@ -713,7 +713,7 @@ private int DeviceCapabilities(short capability, IntPtr pointerToBuffer, int def // We need to pass IntPtr.Zero since passing HDevMode is non-performant. private static int FastDeviceCapabilities(short capability, IntPtr pointerToBuffer, int defaultValue, string printerName) { - int result = SafeNativeMethods.DeviceCapabilities(printerName, GetOutputPort(), + int result = Interop.Winspool.DeviceCapabilities(printerName, GetOutputPort(), capability, pointerToBuffer, IntPtr.Zero); if (result == -1) return defaultValue; @@ -725,9 +725,9 @@ private static string GetDefaultPrinterName() { if (IntPtr.Size == 8) { - SafeNativeMethods.PRINTDLG data = CreatePRINTDLG(); + Interop.Comdlg32.PRINTDLG data = CreatePRINTDLG(); data.Flags = SafeNativeMethods.PD_RETURNDEFAULT; - bool status = SafeNativeMethods.PrintDlg(data); + bool status = Interop.Comdlg32.PrintDlg(data); if (!status) return SR.NoDefaultPrinter; @@ -749,9 +749,9 @@ private static string GetDefaultPrinterName() } else { - SafeNativeMethods.PRINTDLGX86 data = CreatePRINTDLGX86(); + Interop.Comdlg32.PRINTDLGX86 data = CreatePRINTDLGX86(); data.Flags = SafeNativeMethods.PD_RETURNDEFAULT; - bool status = SafeNativeMethods.PrintDlg(data); + bool status = Interop.Comdlg32.PrintDlg(data); if (!status) return SR.NoDefaultPrinter; @@ -779,9 +779,9 @@ private static string GetOutputPort() { if (IntPtr.Size == 8) { - SafeNativeMethods.PRINTDLG data = CreatePRINTDLG(); + Interop.Comdlg32.PRINTDLG data = CreatePRINTDLG(); data.Flags = SafeNativeMethods.PD_RETURNDEFAULT; - bool status = SafeNativeMethods.PrintDlg(data); + bool status = Interop.Comdlg32.PrintDlg(data); if (!status) return SR.NoDefaultPrinter; @@ -803,9 +803,9 @@ private static string GetOutputPort() } else { - SafeNativeMethods.PRINTDLGX86 data = CreatePRINTDLGX86(); + Interop.Comdlg32.PRINTDLGX86 data = CreatePRINTDLGX86(); data.Flags = SafeNativeMethods.PD_RETURNDEFAULT; - bool status = SafeNativeMethods.PrintDlg(data); + bool status = Interop.Comdlg32.PrintDlg(data); if (!status) return SR.NoDefaultPrinter; @@ -859,12 +859,12 @@ internal IntPtr GetHdevmodeInternal() private IntPtr GetHdevmodeInternal(string printer) { // Create DEVMODE - int modeSize = SafeNativeMethods.DocumentProperties(NativeMethods.NullHandleRef, NativeMethods.NullHandleRef, printer, IntPtr.Zero, NativeMethods.NullHandleRef, 0); + int modeSize = Interop.Winspool.DocumentProperties(NativeMethods.NullHandleRef, NativeMethods.NullHandleRef, printer, IntPtr.Zero, NativeMethods.NullHandleRef, 0); if (modeSize < 1) { throw new InvalidPrinterException(this); } - IntPtr handle = SafeNativeMethods.GlobalAlloc(SafeNativeMethods.GMEM_MOVEABLE, (uint)modeSize); // cannot be <0 anyway + IntPtr handle = Interop.Kernel32.GlobalAlloc(SafeNativeMethods.GMEM_MOVEABLE, (uint)modeSize); // cannot be <0 anyway IntPtr pointer = Interop.Kernel32.GlobalLock(handle); //Get the DevMode only if its not cached.... @@ -874,14 +874,14 @@ private IntPtr GetHdevmodeInternal(string printer) } else { - int returnCode = SafeNativeMethods.DocumentProperties(NativeMethods.NullHandleRef, NativeMethods.NullHandleRef, printer, pointer, NativeMethods.NullHandleRef, SafeNativeMethods.DM_OUT_BUFFER); + int returnCode = Interop.Winspool.DocumentProperties(NativeMethods.NullHandleRef, NativeMethods.NullHandleRef, printer, pointer, NativeMethods.NullHandleRef, SafeNativeMethods.DM_OUT_BUFFER); if (returnCode < 0) { throw new Win32Exception(); } } - SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(pointer, typeof(SafeNativeMethods.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(pointer, typeof(Interop.Gdi32.DEVMODE))!; if (_extrainfo != null) @@ -914,7 +914,7 @@ private IntPtr GetHdevmodeInternal(string printer) Marshal.StructureToPtr(mode, pointer, false); - int retCode = SafeNativeMethods.DocumentProperties(NativeMethods.NullHandleRef, NativeMethods.NullHandleRef, printer, pointer, pointer, SafeNativeMethods.DM_IN_BUFFER | SafeNativeMethods.DM_OUT_BUFFER); + int retCode = Interop.Winspool.DocumentProperties(NativeMethods.NullHandleRef, NativeMethods.NullHandleRef, printer, pointer, pointer, SafeNativeMethods.DM_IN_BUFFER | SafeNativeMethods.DM_OUT_BUFFER); if (retCode < 0) { Interop.Kernel32.GlobalFree(handle); @@ -960,7 +960,7 @@ public IntPtr GetHdevnames() // 8 = size of fixed portion of DEVNAMES short offset = (short)(8 / Marshal.SystemDefaultCharSize); // Offsets are in characters, not bytes uint namesSize = (uint)checked(Marshal.SystemDefaultCharSize * (offset + namesCharacters)); // always >0 - IntPtr handle = SafeNativeMethods.GlobalAlloc(SafeNativeMethods.GMEM_MOVEABLE | SafeNativeMethods.GMEM_ZEROINIT, namesSize); + IntPtr handle = Interop.Kernel32.GlobalAlloc(SafeNativeMethods.GMEM_MOVEABLE | SafeNativeMethods.GMEM_ZEROINIT, namesSize); IntPtr namesPointer = Interop.Kernel32.GlobalLock(handle); Marshal.WriteInt16(namesPointer, offset); // wDriverOffset @@ -1001,7 +1001,7 @@ internal short GetModeField(ModeField field, short defaultValue, IntPtr modeHand } IntPtr modePointer = Interop.Kernel32.GlobalLock(new HandleRef(this, modeHandle)); - SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; switch (field) { case ModeField.Orientation: @@ -1194,7 +1194,7 @@ public void SetHdevmode(IntPtr hdevmode) throw new ArgumentException(SR.Format(SR.InvalidPrinterHandle, hdevmode)); IntPtr pointer = Interop.Kernel32.GlobalLock(hdevmode); - SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(pointer, typeof(SafeNativeMethods.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(pointer, typeof(Interop.Gdi32.DEVMODE))!; //Copy entire public devmode as a byte array... _devmodebytes = mode.dmSize; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/SystemFonts.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/SystemFonts.Windows.cs index 8cbc2195089a1..67ec943025754 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/SystemFonts.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/SystemFonts.Windows.cs @@ -134,7 +134,7 @@ public static Font DefaultFont Font? defaultFont = null; // For Arabic systems, always return Tahoma 8. - if ((ushort)UnsafeNativeMethods.GetSystemDefaultLCID() == 0x0001) + if ((ushort)Interop.Kernel32.GetSystemDefaultLCID() == 0x0001) { try { @@ -196,7 +196,7 @@ public static Font DialogFont { Font? dialogFont = null; - if ((ushort)UnsafeNativeMethods.GetSystemDefaultLCID() == 0x0011) + if ((ushort)Interop.Kernel32.GetSystemDefaultLCID() == 0x0011) { // Always return DefaultFont for Japanese cultures. dialogFont = DefaultFont; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/SystemIcons.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/SystemIcons.Windows.cs index 7c0b56ef4c4e4..eda460dfb7c30 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/SystemIcons.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/SystemIcons.Windows.cs @@ -52,7 +52,7 @@ public static Icon Shield private static Icon GetIcon(ref Icon? icon, int iconId) { - return icon ?? (icon = new Icon(SafeNativeMethods.LoadIcon(NativeMethods.NullHandleRef, (IntPtr)iconId))); + return icon ?? (icon = new Icon(Interop.User32.LoadIcon(NativeMethods.NullHandleRef, (IntPtr)iconId))); } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Text/PrivateFontCollection.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Text/PrivateFontCollection.Windows.cs index 7f6e1e335c4ef..e84b0bfe7405e 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Text/PrivateFontCollection.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Text/PrivateFontCollection.Windows.cs @@ -7,7 +7,7 @@ public partial class PrivateFontCollection { private void GdiAddFontFile(string filename) { - SafeNativeMethods.AddFontFile(filename); + Interop.Gdi32.AddFontFile(filename); } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs deleted file mode 100644 index 00aa1233af043..0000000000000 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs +++ /dev/null @@ -1,16 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.InteropServices; - -namespace System.Drawing -{ - internal class UnsafeNativeMethods - { - [DllImport(ExternDll.Kernel32, SetLastError = true)] - public static extern int GetSystemDefaultLCID(); - - [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] - public static extern int GetSystemMetrics(int nIndex); - } -} diff --git a/src/libraries/System.Drawing.Common/src/misc/ExternDll.Unix.cs b/src/libraries/System.Drawing.Common/src/misc/ExternDll.Unix.cs deleted file mode 100644 index 0976a48d1163a..0000000000000 --- a/src/libraries/System.Drawing.Common/src/misc/ExternDll.Unix.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System -{ - internal static class ExternDll - { - public const string Gdiplus = "gdiplus"; - public const string User32 = "user32"; - public const string Gdi32 = "gdi32"; - public const string Kernel32 = "kernel32"; - public const string Winspool = "winspool.drv"; - public const string Comdlg32 = "comdlg32.dll"; - public const string Comctl32 = "comctl32.dll"; - public const string Shell32 = "shell32.dll"; - public const string Oleaut32 = "oleaut32.dll"; - } -} diff --git a/src/libraries/System.Drawing.Common/src/misc/ExternDll.cs b/src/libraries/System.Drawing.Common/src/misc/ExternDll.cs deleted file mode 100644 index 739b3233e710a..0000000000000 --- a/src/libraries/System.Drawing.Common/src/misc/ExternDll.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System -{ - internal static class ExternDll - { - public const string Comdlg32 = "comdlg32.dll"; - public const string Gdi32 = "gdi32.dll"; - public const string Gdiplus = "gdiplus.dll"; - public const string Kernel32 = "kernel32.dll"; - public const string Oleaut32 = "oleaut32.dll"; - public const string Shell32 = "shell32.dll"; - public const string User32 = "user32.dll"; - public const string Winspool = "winspool.drv"; - } -} diff --git a/src/libraries/System.Drawing.Common/tests/GdiPlusHandlesTests.cs b/src/libraries/System.Drawing.Common/tests/GdiPlusHandlesTests.cs index 7fdc52d52ccc0..5b66bae38beee 100644 --- a/src/libraries/System.Drawing.Common/tests/GdiPlusHandlesTests.cs +++ b/src/libraries/System.Drawing.Common/tests/GdiPlusHandlesTests.cs @@ -40,7 +40,7 @@ public static void GraphicsDrawIconDoesNotLeakHandles() int finalHandles = Helpers.GetGuiResources(processHandle, 0); ValidateNoWin32Error(finalHandles); - Assert.InRange(finalHandles, initialHandles, initialHandles + handleTreshold); + Assert.InRange(finalHandles, initialHandles - handleTreshold, initialHandles + handleTreshold); }).Dispose(); }