From 6b224f3d6bd7d1937285646c7e3c4d51b03fd835 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Tue, 14 Dec 2021 23:21:50 +0600 Subject: [PATCH] Remove AOT warnings for S.Drawing.Common (#61567) * Remove AOT warnings for S.Drawing.Common - `Marshal.SizeOf(Type)` replaced with `Marshal.SizeOt()` - `Marshal.PtrToStructure(IntrPtr, Type)` replaced with `Marshal.PtrToStructure(IntPtr)` * Use pointers where possible * Ref return PRINTDLG * Use GeneratedDllImport Co-authored-by: Jan Kotas --- .../src/Interop/Windows/Interop.Comdlg32.cs | 27 ++--- .../src/System.Drawing.Common.csproj | 1 + .../src/System/Drawing/Gdiplus.cs | 4 +- .../src/System/Drawing/Graphics.Unix.cs | 2 +- .../Drawing/Imaging/EncoderParameter.cs | 3 - .../Imaging/EncoderParameterPrivate.cs | 16 +++ .../Drawing/Imaging/EncoderParameters.cs | 20 ++-- .../System/Drawing/Imaging/ImageCodecInfo.cs | 39 +++---- .../Drawing/Imaging/ImageCodecInfoPrivate.cs | 23 ++-- .../System/Drawing/Imaging/Metafile.Unix.cs | 10 +- .../Drawing/Imaging/Metafile.Windows.cs | 18 +-- .../Drawing/Imaging/MetafileHeader.Unix.cs | 2 +- .../Drawing/Imaging/MetafileHeaderWmf.cs | 2 +- .../src/System/Drawing/MarshallingHelpers.cs | 42 +++---- .../Drawing/Printing/PageSettings.Windows.cs | 12 +- .../Printing/PrinterSettings.Windows.cs | 103 ++++++------------ .../Drawing/Printing/PrintingServices.Unix.cs | 34 +++--- 17 files changed, 155 insertions(+), 203 deletions(-) create mode 100644 src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameterPrivate.cs 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 index b6ed3098be47e..6f11330e6228a 100644 --- a/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Comdlg32.cs +++ b/src/libraries/System.Drawing.Common/src/Interop/Windows/Interop.Comdlg32.cs @@ -8,17 +8,14 @@ internal static partial class Interop { internal static partial class Comdlg32 { -#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time - // TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we support non-blittable structs. - [DllImport(Libraries.Comdlg32, CharSet = CharSet.Auto, SetLastError = true)] - internal static extern bool PrintDlg([In, Out] PRINTDLG lppd); + [GeneratedDllImport(Libraries.Comdlg32, CharSet = CharSet.Auto, SetLastError = true)] + internal static partial bool PrintDlg(ref PRINTDLG lppd); - [DllImport(Libraries.Comdlg32, CharSet = CharSet.Auto, SetLastError = true)] - internal static extern bool PrintDlg([In, Out] PRINTDLGX86 lppd); -#pragma warning restore DLLIMPORTGENANALYZER015 + [GeneratedDllImport(Libraries.Comdlg32, CharSet = CharSet.Auto, SetLastError = true)] + internal static partial bool PrintDlg(ref PRINTDLGX86 lppd); - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - internal sealed class PRINTDLG + [StructLayout(LayoutKind.Sequential)] + internal struct PRINTDLG { internal int lStructSize; internal IntPtr hwndOwner; @@ -35,14 +32,14 @@ internal sealed class PRINTDLG internal IntPtr lCustData; internal IntPtr lpfnPrintHook; internal IntPtr lpfnSetupHook; - internal string? lpPrintTemplateName; - internal string? lpSetupTemplateName; + internal IntPtr lpPrintTemplateName; + internal IntPtr lpSetupTemplateName; internal IntPtr hPrintTemplate; internal IntPtr hSetupTemplate; } - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)] - internal sealed class PRINTDLGX86 + [StructLayout(LayoutKind.Sequential, Pack = 1)] + internal struct PRINTDLGX86 { internal int lStructSize; internal IntPtr hwndOwner; @@ -59,8 +56,8 @@ internal sealed class PRINTDLGX86 internal IntPtr lCustData; internal IntPtr lpfnPrintHook; internal IntPtr lpfnSetupHook; - internal string? lpPrintTemplateName; - internal string? lpSetupTemplateName; + internal IntPtr lpPrintTemplateName; + internal IntPtr lpSetupTemplateName; internal IntPtr hPrintTemplate; internal IntPtr hSetupTemplate; } 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 8205814695266..2870372f9730d 100644 --- a/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj +++ b/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj @@ -174,6 +174,7 @@ Unix support is disabled by default. See https://aka.ms/systemdrawingnonwindows + 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 f55599f6590c0..be0dd1f4b0a27 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs @@ -367,14 +367,14 @@ public const int public const int ERROR_CANCELLED = 1223; [StructLayout(LayoutKind.Sequential)] - public class ENHMETAHEADER + public struct ENHMETAHEADER { /// The ENHMETAHEADER structure is defined natively as a union with WmfHeader. /// Extreme care should be taken if changing the layout of the corresponding managed /// structures to minimize the risk of buffer overruns. The affected managed classes /// are the following: ENHMETAHEADER, MetaHeader, MetafileHeaderWmf, MetafileHeaderEmf. public int iType; - public int nSize = 40; // ndirect.DllLib.sizeOf( this ) + public int nSize; // rclBounds was a by-value RECTL structure public int rclBounds_left; public int rclBounds_top; 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 4baa41dad2449..eb7135c708213 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 @@ -167,7 +167,7 @@ private void CopyFromScreenX11(int sourceX, int sourceY, int destinationX, int d /* Get XVisualInfo for this visual */ visual.visualid = LibX11Functions.XVisualIDFromVisual(defvisual); vPtr = LibX11Functions.XGetVisualInfo(Gdip.Display, 0x1 /* VisualIDMask */, ref visual, ref nitems); - visual = (XVisualInfo)Marshal.PtrToStructure(vPtr, typeof(XVisualInfo))!; + visual = Marshal.PtrToStructure(vPtr)!; image = LibX11Functions.XGetImage(Gdip.Display, window, sourceX, sourceY, blockRegionSize.Width, blockRegionSize.Height, AllPlanes, 2 /* ZPixmap*/); if (image == IntPtr.Zero) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameter.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameter.cs index c20ae2d00ad08..5ce87673c2e97 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameter.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameter.cs @@ -9,9 +9,6 @@ namespace System.Drawing.Imaging [StructLayout(LayoutKind.Sequential)] public sealed unsafe class EncoderParameter : IDisposable { -#pragma warning disable CS0618 // Legacy code: We don't care about using obsolete API's. - [MarshalAs(UnmanagedType.Struct)] -#pragma warning restore CS0618 private Guid _parameterGuid; // GUID of the parameter private readonly int _numberOfValues; // Number of the parameter values private readonly EncoderParameterValueType _parameterValueType; // Value type, like ValueTypeLONG etc. diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameterPrivate.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameterPrivate.cs new file mode 100644 index 0000000000000..994ac2e1e46b9 --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameterPrivate.cs @@ -0,0 +1,16 @@ +// 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.Imaging +{ + [StructLayout(LayoutKind.Sequential)] + internal struct EncoderParameterPrivate + { + public Guid ParameterGuid; // GUID of the parameter + public int NumberOfValues; // Number of the parameter values + public EncoderParameterValueType ParameterValueType; // Value type, like ValueTypeLONG etc. + public IntPtr ParameterValue; // A pointer to the parameter values + } +} diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameters.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameters.cs index 3562cc07b07f6..c2cbd9d771cf3 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameters.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/EncoderParameters.cs @@ -51,9 +51,9 @@ public EncoderParameter[] Param /// Also, in 64-bit platforms, 'Count' is aligned in 8 bytes (4 extra padding bytes) so we use IntPtr instead of Int32 to account for /// that. /// - internal IntPtr ConvertToMemory() + internal unsafe IntPtr ConvertToMemory() { - int size = Marshal.SizeOf(typeof(EncoderParameter)); + int size = sizeof(EncoderParameterPrivate); int length = _param.Length; IntPtr memory = Marshal.AllocHGlobal(checked(length * size + IntPtr.Size)); @@ -74,27 +74,21 @@ internal IntPtr ConvertToMemory() /// Copy the native GDI+ EncoderParameters data from a chunk of memory into a managed EncoderParameters object. /// See ConvertToMemory for more info. /// - internal static EncoderParameters ConvertFromMemory(IntPtr memory) + internal static unsafe EncoderParameters ConvertFromMemory(IntPtr memory) { if (memory == IntPtr.Zero) { throw Gdip.StatusException(Gdip.InvalidParameter); } - int count = Marshal.ReadInt32(memory); - + int count = *(int*)memory; + EncoderParameterPrivate* parameters = (EncoderParameterPrivate*)((byte*)memory + IntPtr.Size); EncoderParameters p = new EncoderParameters(count); - int size = Marshal.SizeOf(typeof(EncoderParameter)); - long arrayOffset = (long)memory + IntPtr.Size; - for (int i = 0; i < count; i++) { - Guid guid = (Guid)Marshal.PtrToStructure((IntPtr)(i * size + arrayOffset), typeof(Guid))!; - int numberOfValues = Marshal.ReadInt32((IntPtr)(i * size + arrayOffset + 16)); - EncoderParameterValueType type = (EncoderParameterValueType)Marshal.ReadInt32((IntPtr)(i * size + arrayOffset + 20)); - IntPtr value = Marshal.ReadIntPtr((IntPtr)(i * size + arrayOffset + 24)); + ref readonly EncoderParameterPrivate param = ref parameters[i]; - p._param[i] = new EncoderParameter(new Encoder(guid), numberOfValues, type, value); + p._param[i] = new EncoderParameter(new Encoder(param.ParameterGuid), param.NumberOfValues, param.ParameterValueType, param.ParameterValue); } return p; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/ImageCodecInfo.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/ImageCodecInfo.cs index 35f072f6ce055..750fd9229ad42 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/ImageCodecInfo.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/ImageCodecInfo.cs @@ -169,7 +169,7 @@ public static ImageCodecInfo[] GetImageEncoders() return imageCodecs; } - private static ImageCodecInfo[] ConvertFromMemory(IntPtr memoryStart, int numCodecs) + private static unsafe ImageCodecInfo[] ConvertFromMemory(IntPtr memoryStart, int numCodecs) { ImageCodecInfo[] codecs = new ImageCodecInfo[numCodecs]; @@ -177,33 +177,30 @@ private static ImageCodecInfo[] ConvertFromMemory(IntPtr memoryStart, int numCod for (index = 0; index < numCodecs; index++) { - IntPtr curcodec = (IntPtr)((long)memoryStart + (int)Marshal.SizeOf(typeof(ImageCodecInfoPrivate)) * index); - ImageCodecInfoPrivate codecp = new ImageCodecInfoPrivate(); - Marshal.PtrToStructure(curcodec, codecp); + ref readonly ImageCodecInfoPrivate codecp = ref ((ImageCodecInfoPrivate*)memoryStart)[index]; - codecs[index] = new ImageCodecInfo(); - codecs[index].Clsid = codecp.Clsid; - codecs[index].FormatID = codecp.FormatID; - codecs[index].CodecName = Marshal.PtrToStringUni(codecp.CodecName); - codecs[index].DllName = Marshal.PtrToStringUni(codecp.DllName); - codecs[index].FormatDescription = Marshal.PtrToStringUni(codecp.FormatDescription); - codecs[index].FilenameExtension = Marshal.PtrToStringUni(codecp.FilenameExtension); - codecs[index].MimeType = Marshal.PtrToStringUni(codecp.MimeType); + var codec = new ImageCodecInfo(); + codec.Clsid = codecp.Clsid; + codec.FormatID = codecp.FormatID; + codec.CodecName = Marshal.PtrToStringUni(codecp.CodecName); + codec.DllName = Marshal.PtrToStringUni(codecp.DllName); + codec.FormatDescription = Marshal.PtrToStringUni(codecp.FormatDescription); + codec.FilenameExtension = Marshal.PtrToStringUni(codecp.FilenameExtension); + codec.MimeType = Marshal.PtrToStringUni(codecp.MimeType); - codecs[index].Flags = (ImageCodecFlags)codecp.Flags; - codecs[index].Version = (int)codecp.Version; + codec.Flags = (ImageCodecFlags)codecp.Flags; + codec.Version = (int)codecp.Version; - codecs[index].SignaturePatterns = new byte[codecp.SigCount][]; - codecs[index].SignatureMasks = new byte[codecp.SigCount][]; + codec.SignaturePatterns = new byte[codecp.SigCount][]; + codec.SignatureMasks = new byte[codecp.SigCount][]; for (int j = 0; j < codecp.SigCount; j++) { - codecs[index].SignaturePatterns![j] = new byte[codecp.SigSize]; - codecs[index].SignatureMasks![j] = new byte[codecp.SigSize]; - - Marshal.Copy((IntPtr)((long)codecp.SigMask + j * codecp.SigSize), codecs[index].SignatureMasks![j], 0, codecp.SigSize); - Marshal.Copy((IntPtr)((long)codecp.SigPattern + j * codecp.SigSize), codecs[index].SignaturePatterns![j], 0, codecp.SigSize); + codec.SignaturePatterns[j] = new ReadOnlySpan((byte*)codecp.SigPattern + j * codecp.SigSize, codecp.SigSize).ToArray(); + codec.SignatureMasks[j] = new ReadOnlySpan((byte*)codecp.SigMask + j * codecp.SigSize, codecp.SigSize).ToArray(); } + + codecs[index] = codec; } return codecs; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/ImageCodecInfoPrivate.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/ImageCodecInfoPrivate.cs index d47f98c9f1523..168c4ec56c1d4 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/ImageCodecInfoPrivate.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/ImageCodecInfoPrivate.cs @@ -7,29 +7,24 @@ namespace System.Drawing.Imaging // sdkinc\imaging.h [StructLayout(LayoutKind.Sequential, Pack = 8)] - internal sealed class ImageCodecInfoPrivate + internal struct ImageCodecInfoPrivate { -#pragma warning disable CS0618 // Legacy code: We don't care about using obsolete API's. - [MarshalAs(UnmanagedType.Struct)] -#pragma warning restore CS0618 public Guid Clsid; -#pragma warning disable CS0618 // Legacy code: We don't care about using obsolete API's. - [MarshalAs(UnmanagedType.Struct)] -#pragma warning restore CS0618 + public Guid FormatID; - public IntPtr CodecName = IntPtr.Zero; - public IntPtr DllName = IntPtr.Zero; - public IntPtr FormatDescription = IntPtr.Zero; - public IntPtr FilenameExtension = IntPtr.Zero; - public IntPtr MimeType = IntPtr.Zero; + public IntPtr CodecName; + public IntPtr DllName; + public IntPtr FormatDescription; + public IntPtr FilenameExtension; + public IntPtr MimeType; public int Flags; public int Version; public int SigCount; public int SigSize; - public IntPtr SigPattern = IntPtr.Zero; - public IntPtr SigMask = IntPtr.Zero; + public IntPtr SigPattern; + public IntPtr SigMask; } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Unix.cs index 361565d3f1664..06ce78264e8c7 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Unix.cs @@ -255,7 +255,7 @@ public IntPtr GetHenhmetafile() public MetafileHeader GetMetafileHeader() { - IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeader))); + IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf()); try { int status = Gdip.GdipGetMetafileHeaderFromMetafile(nativeImage, header); @@ -270,7 +270,7 @@ public MetafileHeader GetMetafileHeader() public static MetafileHeader GetMetafileHeader(IntPtr henhmetafile) { - IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeader))); + IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf()); try { int status = Gdip.GdipGetMetafileHeaderFromEmf(henhmetafile, header); @@ -288,7 +288,7 @@ public static MetafileHeader GetMetafileHeader(Stream stream) if (stream == null) throw new NullReferenceException(nameof(stream)); - IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeader))); + IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf()); try { // With libgdiplus we use a custom API for this, because there's no easy way @@ -315,7 +315,7 @@ public static MetafileHeader GetMetafileHeader(string fileName) // Called in order to emulate exception behavior from .NET Framework related to invalid file paths. Path.GetFullPath(fileName); - IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeader))); + IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf()); try { int status = Gdip.GdipGetMetafileHeaderFromFile(fileName, header); @@ -330,7 +330,7 @@ public static MetafileHeader GetMetafileHeader(string fileName) public static MetafileHeader GetMetafileHeader(IntPtr hmetafile, WmfPlaceableFileHeader wmfHeader) { - IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeader))); + IntPtr header = Marshal.AllocHGlobal(Marshal.SizeOf()); try { int status = Gdip.GdipGetMetafileHeaderFromEmf(hmetafile, header); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Windows.cs index 4d8dccb01e71f..3e23c5226b3bb 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Windows.cs @@ -257,7 +257,7 @@ public static MetafileHeader GetMetafileHeader(string fileName) MetafileHeader header = new MetafileHeader(); - IntPtr memory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeaderEmf))); + IntPtr memory = Marshal.AllocHGlobal(Marshal.SizeOf()); try { @@ -273,14 +273,14 @@ public static MetafileHeader GetMetafileHeader(string fileName) metafileType == MetafileType.WmfPlaceable) { // WMF header - header.wmf = (MetafileHeaderWmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderWmf))!; + header.wmf = Marshal.PtrToStructure(memory)!; header.emf = null; } else { // EMF header header.wmf = null; - header.emf = (MetafileHeaderEmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderEmf))!; + header.emf = Marshal.PtrToStructure(memory)!; } } finally @@ -298,7 +298,7 @@ public static MetafileHeader GetMetafileHeader(Stream stream) { MetafileHeader header; - IntPtr memory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeaderEmf))); + IntPtr memory = Marshal.AllocHGlobal(Marshal.SizeOf()); try { @@ -317,14 +317,14 @@ public static MetafileHeader GetMetafileHeader(Stream stream) metafileType == MetafileType.WmfPlaceable) { // WMF header - header.wmf = (MetafileHeaderWmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderWmf))!; + header.wmf = Marshal.PtrToStructure(memory)!; header.emf = null; } else { // EMF header header.wmf = null; - header.emf = (MetafileHeaderEmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderEmf))!; + header.emf = Marshal.PtrToStructure(memory)!; } } finally @@ -342,7 +342,7 @@ public MetafileHeader GetMetafileHeader() { MetafileHeader header; - IntPtr memory = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MetafileHeaderEmf))); + IntPtr memory = Marshal.AllocHGlobal(Marshal.SizeOf()); try { @@ -360,14 +360,14 @@ public MetafileHeader GetMetafileHeader() metafileType == MetafileType.WmfPlaceable) { // WMF header - header.wmf = (MetafileHeaderWmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderWmf))!; + header.wmf = Marshal.PtrToStructure(memory)!; header.emf = null; } else { // EMF header header.wmf = null; - header.emf = (MetafileHeaderEmf)Marshal.PtrToStructure(memory, typeof(MetafileHeaderEmf))!; + header.emf = Marshal.PtrToStructure(memory)!; } } finally diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/MetafileHeader.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/MetafileHeader.Unix.cs index 2d5c1fb6800f2..2770f04c159f6 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/MetafileHeader.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/MetafileHeader.Unix.cs @@ -103,7 +103,7 @@ public sealed class MetafileHeader internal MetafileHeader(IntPtr henhmetafile) { - Marshal.PtrToStructure(henhmetafile, this); + Marshal.PtrToStructure(henhmetafile, this); } // methods diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/MetafileHeaderWmf.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/MetafileHeaderWmf.cs index d42e1ceaea7b2..0e95dacbc831e 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/MetafileHeaderWmf.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/MetafileHeaderWmf.cs @@ -13,7 +13,7 @@ internal sealed class MetafileHeaderWmf /// structures to minimize the risk of buffer overruns. The affected managed classes /// are the following: ENHMETAHEADER, MetaHeader, MetafileHeaderWmf, MetafileHeaderEmf. public MetafileType type = MetafileType.Invalid; - public int size = Marshal.SizeOf(typeof(MetafileHeaderWmf)); + public int size = Marshal.SizeOf(); public int version; public EmfPlusFlags emfPlusFlags; public float dpiX; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/MarshallingHelpers.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/MarshallingHelpers.cs index f95875159844c..7c265b7ef9233 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/MarshallingHelpers.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/MarshallingHelpers.cs @@ -36,51 +36,39 @@ namespace System.Drawing { - internal static class MarshallingHelpers + internal static unsafe class MarshallingHelpers { // Copies a Ptr to an array of Points and releases the memory - public static void FromUnManagedMemoryToPointI(IntPtr prt, Point[] pts) + public static void FromUnManagedMemoryToPointI(IntPtr ptr, Point[] pts) { - int nPointSize = Marshal.SizeOf(pts[0]); - IntPtr pos = prt; - for (int i = 0; i < pts.Length; i++, pos = new IntPtr(pos.ToInt64() + nPointSize)) - pts[i] = (Point)Marshal.PtrToStructure(pos, typeof(Point))!; - - Marshal.FreeHGlobal(prt); + var sourceSpan = new Span((void*)ptr, pts.Length); + sourceSpan.CopyTo(new Span(pts)); + Marshal.FreeHGlobal(ptr); } // Copies a Ptr to an array of Points and releases the memory - public static void FromUnManagedMemoryToPoint(IntPtr prt, PointF[] pts) + public static void FromUnManagedMemoryToPoint(IntPtr ptr, PointF[] pts) { - int nPointSize = Marshal.SizeOf(pts[0]); - IntPtr pos = prt; - for (int i = 0; i < pts.Length; i++, pos = new IntPtr(pos.ToInt64() + nPointSize)) - pts[i] = (PointF)Marshal.PtrToStructure(pos, typeof(PointF))!; - - Marshal.FreeHGlobal(prt); + var sourceSpan = new Span((void*)ptr, pts.Length); + sourceSpan.CopyTo(new Span(pts)); + Marshal.FreeHGlobal(ptr); } // Copies an array of Points to unmanaged memory public static IntPtr FromPointToUnManagedMemoryI(Point[] pts) { - int nPointSize = Marshal.SizeOf(pts[0]); - IntPtr dest = Marshal.AllocHGlobal(nPointSize * pts.Length); - IntPtr pos = dest; - for (int i = 0; i < pts.Length; i++, pos = new IntPtr(pos.ToInt64() + nPointSize)) - Marshal.StructureToPtr(pts[i], pos, false); - + IntPtr dest = Marshal.AllocHGlobal(sizeof(Point) * pts.Length); + var destinationSpan = new Span((void*)dest, pts.Length); + pts.CopyTo(destinationSpan); return dest; } // Copies an array of Points to unmanaged memory public static IntPtr FromPointToUnManagedMemory(PointF[] pts) { - int nPointSize = Marshal.SizeOf(pts[0]); - IntPtr dest = Marshal.AllocHGlobal(nPointSize * pts.Length); - IntPtr pos = dest; - for (int i = 0; i < pts.Length; i++, pos = new IntPtr(pos.ToInt64() + nPointSize)) - Marshal.StructureToPtr(pts[i], pos, false); - + IntPtr dest = Marshal.AllocHGlobal(sizeof(PointF) * pts.Length); + var destinationSpan = new Span((void*)dest, pts.Length); + pts.CopyTo(destinationSpan); return dest; } } 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 f0e41dbc667db..c94f82a56f664 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)); - Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = Marshal.PtrToStructure(modePointer)!; PaperSource result = PaperSourceFromMode(mode); @@ -232,7 +232,7 @@ public PrinterResolution PrinterResolution { IntPtr modeHandle = printerSettings.GetHdevmode(); IntPtr modePointer = Interop.Kernel32.GlobalLock(new HandleRef(this, modeHandle)); - Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = Marshal.PtrToStructure(modePointer)!; PrinterResolution result = PrinterResolutionFromMode(mode); @@ -280,7 +280,7 @@ public object Clone() public void CopyToHdevmode(IntPtr hdevmode) { IntPtr modePointer = Interop.Kernel32.GlobalLock(hdevmode); - Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = Marshal.PtrToStructure(modePointer)!; if (_color.IsNotDefault && ((mode.dmFields & SafeNativeMethods.DM_COLOR) == SafeNativeMethods.DM_COLOR)) mode.dmColor = unchecked((short)(((bool)_color) ? SafeNativeMethods.DMCOLOR_COLOR : SafeNativeMethods.DMCOLOR_MONOCHROME)); @@ -381,7 +381,7 @@ private short ExtraBytes { IntPtr modeHandle = printerSettings.GetHdevmodeInternal(); IntPtr modePointer = Interop.Kernel32.GlobalLock(new HandleRef(this, modeHandle)); - Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = Marshal.PtrToStructure(modePointer)!; short result = mode?.dmDriverExtra ?? 0; @@ -426,7 +426,7 @@ private PaperSize GetPaperSize(IntPtr modeHandle) } IntPtr modePointer = Interop.Kernel32.GlobalLock(modeHandle); - Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = Marshal.PtrToStructure(modePointer)!; PaperSize result = PaperSizeFromMode(mode); @@ -514,7 +514,7 @@ public void SetHdevmode(IntPtr hdevmode) } IntPtr pointer = Interop.Kernel32.GlobalLock(hdevmode); - Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(pointer, typeof(Interop.Gdi32.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = Marshal.PtrToStructure(pointer)!; 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 d750390d07d6b..47bb8a61937b1 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 @@ -644,62 +644,30 @@ public Graphics CreateMeasurementGraphics(PageSettings pageSettings, bool honorO return g; } - // Create a PRINTDLG with a few useful defaults. // Try to keep this consistent with PrintDialog.CreatePRINTDLG. - private static Interop.Comdlg32.PRINTDLGX86 CreatePRINTDLGX86() - { - 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; - data.Flags = 0; - data.hwndOwner = IntPtr.Zero; - data.hDC = IntPtr.Zero; + private static unsafe void CreatePRINTDLGX86(out Interop.Comdlg32.PRINTDLGX86 data) + { + data = default; + data.lStructSize = sizeof(Interop.Comdlg32.PRINTDLGX86); data.nFromPage = 1; data.nToPage = 1; data.nMinPage = 0; data.nMaxPage = 9999; data.nCopies = 1; - data.hInstance = IntPtr.Zero; - data.lCustData = IntPtr.Zero; - data.lpfnPrintHook = IntPtr.Zero; - data.lpfnSetupHook = IntPtr.Zero; - data.lpPrintTemplateName = null; - data.lpSetupTemplateName = null; - data.hPrintTemplate = IntPtr.Zero; - data.hSetupTemplate = IntPtr.Zero; - return data; } - // Create a PRINTDLG with a few useful defaults. // Try to keep this consistent with PrintDialog.CreatePRINTDLG. - private static Interop.Comdlg32.PRINTDLG CreatePRINTDLG() - { - 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; - data.Flags = 0; - data.hwndOwner = IntPtr.Zero; - data.hDC = IntPtr.Zero; + private static unsafe void CreatePRINTDLG(out Interop.Comdlg32.PRINTDLG data) + { + data = default; + data.lStructSize = sizeof(Interop.Comdlg32.PRINTDLG); data.nFromPage = 1; data.nToPage = 1; data.nMinPage = 0; data.nMaxPage = 9999; data.nCopies = 1; - data.hInstance = IntPtr.Zero; - data.lCustData = IntPtr.Zero; - data.lpfnPrintHook = IntPtr.Zero; - data.lpfnSetupHook = IntPtr.Zero; - data.lpPrintTemplateName = null; - data.lpSetupTemplateName = null; - data.hPrintTemplate = IntPtr.Zero; - data.hSetupTemplate = IntPtr.Zero; - return data; } // Use FastDeviceCapabilities where possible -- computing PrinterName is quite slow @@ -725,49 +693,49 @@ private static string GetDefaultPrinterName() { if (IntPtr.Size == 8) { - Interop.Comdlg32.PRINTDLG data = CreatePRINTDLG(); + CreatePRINTDLG(out Interop.Comdlg32.PRINTDLG data); data.Flags = SafeNativeMethods.PD_RETURNDEFAULT; - bool status = Interop.Comdlg32.PrintDlg(data); + bool status = Interop.Comdlg32.PrintDlg(ref data); if (!status) return SR.NoDefaultPrinter; IntPtr handle = data.hDevNames; - IntPtr names = Interop.Kernel32.GlobalLock(new HandleRef(data, handle)); + IntPtr names = Interop.Kernel32.GlobalLock(handle); if (names == IntPtr.Zero) throw new Win32Exception(); string name = ReadOneDEVNAME(names, 1); - Interop.Kernel32.GlobalUnlock(new HandleRef(data, handle)); + Interop.Kernel32.GlobalUnlock(handle); names = IntPtr.Zero; // Windows allocates them, but we have to free them - Interop.Kernel32.GlobalFree(new HandleRef(data, data.hDevNames)); - Interop.Kernel32.GlobalFree(new HandleRef(data, data.hDevMode)); + Interop.Kernel32.GlobalFree(data.hDevNames); + Interop.Kernel32.GlobalFree(data.hDevMode); return name; } else { - Interop.Comdlg32.PRINTDLGX86 data = CreatePRINTDLGX86(); + CreatePRINTDLGX86(out Interop.Comdlg32.PRINTDLGX86 data); data.Flags = SafeNativeMethods.PD_RETURNDEFAULT; - bool status = Interop.Comdlg32.PrintDlg(data); + bool status = Interop.Comdlg32.PrintDlg(ref data); if (!status) return SR.NoDefaultPrinter; IntPtr handle = data.hDevNames; - IntPtr names = Interop.Kernel32.GlobalLock(new HandleRef(data, handle)); + IntPtr names = Interop.Kernel32.GlobalLock(handle); if (names == IntPtr.Zero) throw new Win32Exception(); string name = ReadOneDEVNAME(names, 1); - Interop.Kernel32.GlobalUnlock(new HandleRef(data, handle)); + Interop.Kernel32.GlobalUnlock(handle); names = IntPtr.Zero; // Windows allocates them, but we have to free them - Interop.Kernel32.GlobalFree(new HandleRef(data, data.hDevNames)); - Interop.Kernel32.GlobalFree(new HandleRef(data, data.hDevMode)); + Interop.Kernel32.GlobalFree(data.hDevNames); + Interop.Kernel32.GlobalFree(data.hDevMode); return name; } @@ -779,50 +747,50 @@ private static string GetOutputPort() { if (IntPtr.Size == 8) { - Interop.Comdlg32.PRINTDLG data = CreatePRINTDLG(); + CreatePRINTDLG(out Interop.Comdlg32.PRINTDLG data); data.Flags = SafeNativeMethods.PD_RETURNDEFAULT; - bool status = Interop.Comdlg32.PrintDlg(data); + bool status = Interop.Comdlg32.PrintDlg(ref data); if (!status) return SR.NoDefaultPrinter; IntPtr handle = data.hDevNames; - IntPtr names = Interop.Kernel32.GlobalLock(new HandleRef(data, handle)); + IntPtr names = Interop.Kernel32.GlobalLock(handle); if (names == IntPtr.Zero) throw new Win32Exception(); string name = ReadOneDEVNAME(names, 2); - Interop.Kernel32.GlobalUnlock(new HandleRef(data, handle)); + Interop.Kernel32.GlobalUnlock(handle); names = IntPtr.Zero; // Windows allocates them, but we have to free them - Interop.Kernel32.GlobalFree(new HandleRef(data, data.hDevNames)); - Interop.Kernel32.GlobalFree(new HandleRef(data, data.hDevMode)); + Interop.Kernel32.GlobalFree(data.hDevNames); + Interop.Kernel32.GlobalFree(data.hDevMode); return name; } else { - Interop.Comdlg32.PRINTDLGX86 data = CreatePRINTDLGX86(); + CreatePRINTDLGX86(out Interop.Comdlg32.PRINTDLGX86 data); data.Flags = SafeNativeMethods.PD_RETURNDEFAULT; - bool status = Interop.Comdlg32.PrintDlg(data); + bool status = Interop.Comdlg32.PrintDlg(ref data); if (!status) return SR.NoDefaultPrinter; IntPtr handle = data.hDevNames; - IntPtr names = Interop.Kernel32.GlobalLock(new HandleRef(data, handle)); + IntPtr names = Interop.Kernel32.GlobalLock(handle); if (names == IntPtr.Zero) throw new Win32Exception(); string name = ReadOneDEVNAME(names, 2); - Interop.Kernel32.GlobalUnlock(new HandleRef(data, handle)); + Interop.Kernel32.GlobalUnlock(handle); names = IntPtr.Zero; // Windows allocates them, but we have to free them - Interop.Kernel32.GlobalFree(new HandleRef(data, data.hDevNames)); - Interop.Kernel32.GlobalFree(new HandleRef(data, data.hDevMode)); + Interop.Kernel32.GlobalFree(data.hDevNames); + Interop.Kernel32.GlobalFree(data.hDevMode); return name; } @@ -881,8 +849,7 @@ private IntPtr GetHdevmodeInternal(string printer) } } - Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(pointer, typeof(Interop.Gdi32.DEVMODE))!; - + Interop.Gdi32.DEVMODE mode = Marshal.PtrToStructure(pointer)!; if (_extrainfo != null) { @@ -1001,7 +968,7 @@ internal short GetModeField(ModeField field, short defaultValue, IntPtr modeHand } IntPtr modePointer = Interop.Kernel32.GlobalLock(new HandleRef(this, modeHandle)); - Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(Interop.Gdi32.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = Marshal.PtrToStructure(modePointer)!; switch (field) { case ModeField.Orientation: @@ -1194,7 +1161,7 @@ public void SetHdevmode(IntPtr hdevmode) throw new ArgumentException(SR.Format(SR.InvalidPrinterHandle, hdevmode)); IntPtr pointer = Interop.Kernel32.GlobalLock(hdevmode); - Interop.Gdi32.DEVMODE mode = (Interop.Gdi32.DEVMODE)Marshal.PtrToStructure(pointer, typeof(Interop.Gdi32.DEVMODE))!; + Interop.Gdi32.DEVMODE mode = Marshal.PtrToStructure(pointer)!; //Copy entire public devmode as a byte array... _devmodebytes = mode.dmSize; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrintingServices.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrintingServices.Unix.cs index 3324fa97cd1ef..07c60aa8f943f 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrintingServices.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrintingServices.Unix.cs @@ -215,7 +215,7 @@ internal static void LoadPrinterSettings(string printer, PrinterSettings setting if (ret == 0) return; - cups_dests_size = Marshal.SizeOf(typeof(CUPS_DESTS)); + cups_dests_size = Marshal.SizeOf(); ptr = dests; for (int i = 0; i < ret; i++) { @@ -237,7 +237,7 @@ internal static void LoadPrinterSettings(string printer, PrinterSettings setting if (ppd_handle == IntPtr.Zero) return; - printer_dest = (CUPS_DESTS)Marshal.PtrToStructure(ptr, typeof(CUPS_DESTS))!; + printer_dest = Marshal.PtrToStructure(ptr)!; options = new NameValueCollection(); paper_names = new NameValueCollection(); paper_sources = new NameValueCollection(); @@ -261,7 +261,7 @@ internal static void LoadPrinterSettings(string printer, PrinterSettings setting settings.DefaultPageSettings.PaperSize = LoadPrinterPaperSizes(ppd_handle, settings, defsize, paper_names); LoadPrinterResolutionsAndDefault(printer, settings, ppd_handle); - ppd = (PPD_FILE)Marshal.PtrToStructure(ppd_handle, typeof(PPD_FILE))!; + ppd = Marshal.PtrToStructure(ppd_handle)!; settings.landscape_angle = ppd.landscape; settings.supports_color = (ppd.color_device == 0) ? false : true; settings.can_duplex = options["Duplex"] != null; @@ -295,14 +295,14 @@ private static void LoadPrinterOptions(IntPtr options, int numOptions, IntPtr pp { CUPS_OPTIONS cups_options; string? option_name, option_value; - int cups_size = Marshal.SizeOf(typeof(CUPS_OPTIONS)); + int cups_size = Marshal.SizeOf(); LoadOptionList(ppd, "PageSize", paper_names, out defsize); LoadOptionList(ppd, "InputSlot", paper_sources, out defsource); for (int j = 0; j < numOptions; j++) { - cups_options = (CUPS_OPTIONS)Marshal.PtrToStructure(options, typeof(CUPS_OPTIONS))!; + cups_options = Marshal.PtrToStructure(options)!; option_name = Marshal.PtrToStringAnsi(cups_options.name); option_value = Marshal.PtrToStringAnsi(cups_options.val); @@ -329,11 +329,11 @@ private static NameValueCollection LoadPrinterOptions(IntPtr options, int numOpt { CUPS_OPTIONS cups_options; string? option_name, option_value; - int cups_size = Marshal.SizeOf(typeof(CUPS_OPTIONS)); + int cups_size = Marshal.SizeOf(); NameValueCollection list = new NameValueCollection(); for (int j = 0; j < numOptions; j++) { - cups_options = (CUPS_OPTIONS)Marshal.PtrToStructure(options, typeof(CUPS_OPTIONS))!; + cups_options = Marshal.PtrToStructure(options)!; option_name = Marshal.PtrToStringAnsi(cups_options.name); option_value = Marshal.PtrToStringAnsi(cups_options.val); @@ -362,13 +362,13 @@ private static void LoadOptionList(IntPtr ppd, string option_name, NameValueColl IntPtr ptr = IntPtr.Zero; PPD_OPTION ppd_option; PPD_CHOICE choice; - int choice_size = Marshal.SizeOf(typeof(PPD_CHOICE)); + int choice_size = Marshal.SizeOf(); defoption = null; ptr = LibcupsNative.ppdFindOption(ppd, option_name); if (ptr != IntPtr.Zero) { - ppd_option = (PPD_OPTION)Marshal.PtrToStructure(ptr, typeof(PPD_OPTION))!; + ppd_option = Marshal.PtrToStructure(ptr)!; #if PrintDebug Console.WriteLine (" OPTION key:{0} def:{1} text: {2}", ppd_option.keyword, ppd_option.defchoice, ppd_option.text); #endif @@ -376,7 +376,7 @@ private static void LoadOptionList(IntPtr ppd, string option_name, NameValueColl ptr = ppd_option.choices; for (int c = 0; c < ppd_option.num_choices; c++) { - choice = (PPD_CHOICE)Marshal.PtrToStructure(ptr, typeof(PPD_CHOICE))!; + choice = Marshal.PtrToStructure(ptr)!; list.Add(choice.choice, choice.text); #if PrintDebug Console.WriteLine (" choice:{0} - text: {1}", choice.choice, choice.text); @@ -460,12 +460,12 @@ private static PaperSize LoadPrinterPaperSizes(IntPtr ppd_handle, PrinterSetting PaperSize ps; PaperSize defsize = new PaperSize(GetPaperKind(827, 1169), "A4", 827, 1169); - ppd = (PPD_FILE)Marshal.PtrToStructure(ppd_handle, typeof(PPD_FILE))!; + ppd = Marshal.PtrToStructure(ppd_handle)!; ptr = ppd.sizes; float w, h; for (int i = 0; i < ppd.num_sizes; i++) { - size = (PPD_SIZE)Marshal.PtrToStructure(ptr, typeof(PPD_SIZE))!; + size = Marshal.PtrToStructure(ptr)!; real_name = paper_names[size.name]!; w = size.width * 100 / 72; h = size.length * 100 / 72; @@ -475,7 +475,7 @@ private static PaperSize LoadPrinterPaperSizes(IntPtr ppd_handle, PrinterSetting if (def_size == ps.Kind.ToString()) defsize = ps; settings.paper_sizes!.Add(ps); - ptr = (IntPtr)((long)ptr + Marshal.SizeOf(size)); + ptr = (IntPtr)((long)ptr + Marshal.SizeOf()); } return defsize; @@ -577,7 +577,7 @@ private static void LoadPrinterResolutionsAndDefault(string printer, IntPtr ptr_printers = dests; for (int i = 0; i < n_printers; i++) { - var printer = (CUPS_DESTS)Marshal.PtrToStructure(ptr_printers, typeof(CUPS_DESTS))!; + var printer = Marshal.PtrToStructure(ptr_printers)!; string name = Marshal.PtrToStringAnsi(printer.name)!; if (printer.is_default == 1 || @@ -606,7 +606,7 @@ private static void LoadPrinterResolutionsAndDefault(string printer, string comment = options["printer-comment"] as string ?? string.Empty; printers.Add(name, new SysPrn.Printer(string.Empty, string.Empty, status, comment)); - ptr_printers = (IntPtr)((long)ptr_printers + Marshal.SizeOf(typeof(CUPS_DESTS))); + ptr_printers = (IntPtr)((long)ptr_printers + Marshal.SizeOf()); } } finally @@ -632,7 +632,7 @@ internal static void GetPrintDialogInfo(string printer, ref string port, ref str bool found = false; CUPS_DESTS cups_dests; IntPtr dests = IntPtr.Zero, ptr_printers, ptr_printer; - int cups_dests_size = Marshal.SizeOf(typeof(CUPS_DESTS)); + int cups_dests_size = Marshal.SizeOf(); if (!s_cupsInitialized) return; @@ -660,7 +660,7 @@ internal static void GetPrintDialogInfo(string printer, ref string port, ref str if (!found) return; - cups_dests = (CUPS_DESTS)Marshal.PtrToStructure(ptr_printers, typeof(CUPS_DESTS))!; + cups_dests = Marshal.PtrToStructure(ptr_printers)!; NameValueCollection options = LoadPrinterOptions(cups_dests.options, cups_dests.num_options);