Skip to content

Commit 18794fa

Browse files
Add SetLastError to GdiPlus methods (#59151)
System.Drawing uses the last Win32 error in CheckErrorStatus to try to guess at why a GdiPlus draw method failed and ignore some failures. This was broken in .NETCore when SetLastError was removed from PINvokes. Bring back SetLastError for all PInvokes that use CheckErrorStatus. Co-authored-by: Eric StJohn <[email protected]>
1 parent b9f273a commit 18794fa

2 files changed

Lines changed: 59 additions & 59 deletions

File tree

src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Windows.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,13 @@ private static void PlatformInitialize()
269269
[DllImport(LibraryName, ExactSpelling = true)]
270270
internal static extern IntPtr GdipCreateHalftonePalette();
271271

272-
[DllImport(LibraryName, ExactSpelling = true)]
272+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
273273
internal static extern int GdipDrawBeziers(HandleRef graphics, HandleRef pen, PointF* points, int count);
274274

275-
[DllImport(LibraryName, ExactSpelling = true)]
275+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
276276
internal static extern int GdipDrawBeziersI(HandleRef graphics, HandleRef pen, Point* points, int count);
277277

278-
[DllImport(LibraryName, ExactSpelling = true)]
278+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
279279
internal static extern int GdipFillPath(HandleRef graphics, HandleRef brush, HandleRef path);
280280

281281
[DllImport(LibraryName, ExactSpelling = true)]

src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ internal static unsafe partial class Gdip
835835
[DllImport(LibraryName, ExactSpelling = true)]
836836
internal static extern int GdipDeleteRegion(HandleRef region);
837837

838-
[DllImport(LibraryName, ExactSpelling = true)]
838+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
839839
internal static extern int GdipFillRegion(HandleRef graphics, HandleRef brush, HandleRef region);
840840

841841
[DllImport(LibraryName, ExactSpelling = true)]
@@ -1183,127 +1183,127 @@ internal static unsafe partial class Gdip
11831183
[DllImport(LibraryName, ExactSpelling = true)]
11841184
internal static extern int GdipSaveGraphics(HandleRef graphics, out int state);
11851185

1186-
[DllImport(LibraryName, ExactSpelling = true)]
1186+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
11871187
internal static extern int GdipDrawArc(HandleRef graphics, HandleRef pen, float x, float y, float width, float height, float startAngle, float sweepAngle);
11881188

1189-
[DllImport(LibraryName, ExactSpelling = true)]
1189+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
11901190
internal static extern int GdipDrawArcI(HandleRef graphics, HandleRef pen, int x, int y, int width, int height, float startAngle, float sweepAngle);
11911191

1192-
[DllImport(LibraryName, ExactSpelling = true)]
1192+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
11931193
internal static extern int GdipDrawLinesI(HandleRef graphics, HandleRef pen, Point* points, int count);
11941194

1195-
[DllImport(LibraryName, ExactSpelling = true)]
1195+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
11961196
internal static extern int GdipDrawBezier(HandleRef graphics, HandleRef pen, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);
11971197

1198-
[DllImport(LibraryName, ExactSpelling = true)]
1198+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
11991199
internal static extern int GdipDrawEllipse(HandleRef graphics, HandleRef pen, float x, float y, float width, float height);
12001200

1201-
[DllImport(LibraryName, ExactSpelling = true)]
1201+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12021202
internal static extern int GdipDrawEllipseI(HandleRef graphics, HandleRef pen, int x, int y, int width, int height);
12031203

1204-
[DllImport(LibraryName, ExactSpelling = true)]
1204+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12051205
internal static extern int GdipDrawLine(HandleRef graphics, HandleRef pen, float x1, float y1, float x2, float y2);
12061206

1207-
[DllImport(LibraryName, ExactSpelling = true)]
1207+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12081208
internal static extern int GdipDrawLineI(HandleRef graphics, HandleRef pen, int x1, int y1, int x2, int y2);
12091209

1210-
[DllImport(LibraryName, ExactSpelling = true)]
1210+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12111211
internal static extern int GdipDrawLines(HandleRef graphics, HandleRef pen, PointF* points, int count);
12121212

1213-
[DllImport(LibraryName, ExactSpelling = true)]
1213+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12141214
internal static extern int GdipDrawPath(HandleRef graphics, HandleRef pen, HandleRef path);
12151215

1216-
[DllImport(LibraryName, ExactSpelling = true)]
1216+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12171217
internal static extern int GdipDrawPie(HandleRef graphics, HandleRef pen, float x, float y, float width, float height, float startAngle, float sweepAngle);
12181218

1219-
[DllImport(LibraryName, ExactSpelling = true)]
1219+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12201220
internal static extern int GdipDrawPieI(HandleRef graphics, HandleRef pen, int x, int y, int width, int height, float startAngle, float sweepAngle);
12211221

1222-
[DllImport(LibraryName, ExactSpelling = true)]
1222+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12231223
internal static extern int GdipDrawPolygon(HandleRef graphics, HandleRef pen, PointF* points, int count);
12241224

1225-
[DllImport(LibraryName, ExactSpelling = true)]
1225+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12261226
internal static extern int GdipDrawPolygonI(HandleRef graphics, HandleRef pen, Point* points, int count);
12271227

1228-
[DllImport(LibraryName, ExactSpelling = true)]
1228+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12291229
internal static extern int GdipFillEllipse(HandleRef graphics, HandleRef brush, float x, float y, float width, float height);
12301230

1231-
[DllImport(LibraryName, ExactSpelling = true)]
1231+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12321232
internal static extern int GdipFillEllipseI(HandleRef graphics, HandleRef brush, int x, int y, int width, int height);
12331233

1234-
[DllImport(LibraryName, ExactSpelling = true)]
1234+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12351235
internal static extern int GdipFillPolygon(HandleRef graphics, HandleRef brush, PointF* points, int count, FillMode brushMode);
12361236

1237-
[DllImport(LibraryName, ExactSpelling = true)]
1237+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12381238
internal static extern int GdipFillPolygonI(HandleRef graphics, HandleRef brush, Point* points, int count, FillMode brushMode);
12391239

1240-
[DllImport(LibraryName, ExactSpelling = true)]
1240+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12411241
internal static extern int GdipFillRectangle(HandleRef graphics, HandleRef brush, float x, float y, float width, float height);
12421242

1243-
[DllImport(LibraryName, ExactSpelling = true)]
1243+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12441244
internal static extern int GdipFillRectangleI(HandleRef graphics, HandleRef brush, int x, int y, int width, int height);
12451245

1246-
[DllImport(LibraryName, ExactSpelling = true)]
1246+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12471247
internal static extern int GdipFillRectangles(HandleRef graphics, HandleRef brush, RectangleF* rects, int count);
12481248

1249-
[DllImport(LibraryName, ExactSpelling = true)]
1249+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12501250
internal static extern int GdipFillRectanglesI(HandleRef graphics, HandleRef brush, Rectangle* rects, int count);
12511251

1252-
[DllImport(LibraryName, ExactSpelling = true, CharSet = CharSet.Unicode)]
1252+
[DllImport(LibraryName, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
12531253
internal static extern int GdipDrawString(HandleRef graphics, string textString, int length, HandleRef font, ref RectangleF layoutRect, HandleRef stringFormat, HandleRef brush);
12541254

1255-
[DllImport(LibraryName, ExactSpelling = true)]
1255+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12561256
internal static extern int GdipDrawImageRectI(HandleRef graphics, HandleRef image, int x, int y, int width, int height);
12571257

12581258
[DllImport(LibraryName, ExactSpelling = true)]
12591259
internal static extern int GdipGraphicsClear(HandleRef graphics, int argb);
12601260

1261-
[DllImport(LibraryName, ExactSpelling = true)]
1261+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12621262
internal static extern int GdipDrawClosedCurve(HandleRef graphics, HandleRef pen, PointF* points, int count);
12631263

1264-
[DllImport(LibraryName, ExactSpelling = true)]
1264+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12651265
internal static extern int GdipDrawClosedCurveI(HandleRef graphics, HandleRef pen, Point* points, int count);
12661266

1267-
[DllImport(LibraryName, ExactSpelling = true)]
1267+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12681268
internal static extern int GdipDrawClosedCurve2(HandleRef graphics, HandleRef pen, PointF* points, int count, float tension);
12691269

1270-
[DllImport(LibraryName, ExactSpelling = true)]
1270+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12711271
internal static extern int GdipDrawClosedCurve2I(HandleRef graphics, HandleRef pen, Point* points, int count, float tension);
12721272

1273-
[DllImport(LibraryName, ExactSpelling = true)]
1273+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12741274
internal static extern int GdipDrawCurve(HandleRef graphics, HandleRef pen, PointF* points, int count);
12751275

1276-
[DllImport(LibraryName, ExactSpelling = true)]
1276+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12771277
internal static extern int GdipDrawCurveI(HandleRef graphics, HandleRef pen, Point* points, int count);
12781278

1279-
[DllImport(LibraryName, ExactSpelling = true)]
1279+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12801280
internal static extern int GdipDrawCurve2(HandleRef graphics, HandleRef pen, PointF* points, int count, float tension);
12811281

1282-
[DllImport(LibraryName, ExactSpelling = true)]
1282+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12831283
internal static extern int GdipDrawCurve2I(HandleRef graphics, HandleRef pen, Point* points, int count, float tension);
12841284

1285-
[DllImport(LibraryName, ExactSpelling = true)]
1285+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12861286
internal static extern int GdipDrawCurve3(HandleRef graphics, HandleRef pen, PointF* points, int count, int offset, int numberOfSegments, float tension);
12871287

1288-
[DllImport(LibraryName, ExactSpelling = true)]
1288+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12891289
internal static extern int GdipDrawCurve3I(HandleRef graphics, HandleRef pen, Point* points, int count, int offset, int numberOfSegments, float tension);
12901290

1291-
[DllImport(LibraryName, ExactSpelling = true)]
1291+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12921292
internal static extern int GdipFillClosedCurve(HandleRef graphics, HandleRef brush, PointF* points, int count);
12931293

1294-
[DllImport(LibraryName, ExactSpelling = true)]
1294+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12951295
internal static extern int GdipFillClosedCurveI(HandleRef graphics, HandleRef brush, Point* points, int count);
12961296

1297-
[DllImport(LibraryName, ExactSpelling = true)]
1297+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
12981298
internal static extern int GdipFillClosedCurve2(HandleRef graphics, HandleRef brush, PointF* points, int count, float tension, FillMode mode);
12991299

1300-
[DllImport(LibraryName, ExactSpelling = true)]
1300+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13011301
internal static extern int GdipFillClosedCurve2I(HandleRef graphics, HandleRef brush, Point* points, int count, float tension, FillMode mode);
13021302

1303-
[DllImport(LibraryName, ExactSpelling = true)]
1303+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13041304
internal static extern int GdipFillPie(HandleRef graphics, HandleRef brush, float x, float y, float width, float height, float startAngle, float sweepAngle);
13051305

1306-
[DllImport(LibraryName, ExactSpelling = true)]
1306+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13071307
internal static extern int GdipFillPieI(HandleRef graphics, HandleRef brush, int x, int y, int width, int height, float startAngle, float sweepAngle);
13081308

13091309
[DllImport(LibraryName, ExactSpelling = true, CharSet = CharSet.Unicode)]
@@ -1312,49 +1312,49 @@ internal static unsafe partial class Gdip
13121312
[DllImport(LibraryName, ExactSpelling = true, CharSet = CharSet.Unicode)]
13131313
internal static extern int GdipMeasureCharacterRanges(HandleRef graphics, string textString, int length, HandleRef font, ref RectangleF layoutRect, HandleRef stringFormat, int characterCount, [In] [Out] IntPtr[] region);
13141314

1315-
[DllImport(LibraryName, ExactSpelling = true)]
1315+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13161316
internal static extern int GdipDrawImageI(HandleRef graphics, HandleRef image, int x, int y);
13171317

1318-
[DllImport(LibraryName, ExactSpelling = true)]
1318+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13191319
internal static extern int GdipDrawImage(HandleRef graphics, HandleRef image, float x, float y);
13201320

1321-
[DllImport(LibraryName, ExactSpelling = true)]
1321+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13221322
internal static extern int GdipDrawImagePoints(HandleRef graphics, HandleRef image, PointF* points, int count);
13231323

1324-
[DllImport(LibraryName, ExactSpelling = true)]
1324+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13251325
internal static extern int GdipDrawImagePointsI(HandleRef graphics, HandleRef image, Point* points, int count);
13261326

1327-
[DllImport(LibraryName, ExactSpelling = true)]
1327+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13281328
internal static extern int GdipDrawImageRectRectI(HandleRef graphics, HandleRef image, int dstx, int dsty, int dstwidth, int dstheight, int srcx, int srcy, int srcwidth, int srcheight, GraphicsUnit srcunit, HandleRef imageAttributes, Graphics.DrawImageAbort? callback, HandleRef callbackdata);
13291329

1330-
[DllImport(LibraryName, ExactSpelling = true)]
1330+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13311331
internal static extern int GdipDrawImagePointsRect(HandleRef graphics, HandleRef image, PointF* points, int count, float srcx, float srcy, float srcwidth, float srcheight, GraphicsUnit srcunit, HandleRef imageAttributes, Graphics.DrawImageAbort? callback, HandleRef callbackdata);
13321332

1333-
[DllImport(LibraryName, ExactSpelling = true)]
1333+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13341334
internal static extern int GdipDrawImageRectRect(HandleRef graphics, HandleRef image, float dstx, float dsty, float dstwidth, float dstheight, float srcx, float srcy, float srcwidth, float srcheight, GraphicsUnit srcunit, HandleRef imageAttributes, Graphics.DrawImageAbort? callback, HandleRef callbackdata);
13351335

1336-
[DllImport(LibraryName, ExactSpelling = true)]
1336+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13371337
internal static extern int GdipDrawImagePointsRectI(HandleRef graphics, HandleRef image, Point* points, int count, int srcx, int srcy, int srcwidth, int srcheight, GraphicsUnit srcunit, HandleRef imageAttributes, Graphics.DrawImageAbort? callback, HandleRef callbackdata);
13381338

1339-
[DllImport(LibraryName, ExactSpelling = true)]
1339+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13401340
internal static extern int GdipDrawImageRect(HandleRef graphics, HandleRef image, float x, float y, float width, float height);
13411341

1342-
[DllImport(LibraryName, ExactSpelling = true)]
1342+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13431343
internal static extern int GdipDrawImagePointRect(HandleRef graphics, HandleRef image, float x, float y, float srcx, float srcy, float srcwidth, float srcheight, int srcunit);
13441344

1345-
[DllImport(LibraryName, ExactSpelling = true)]
1345+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13461346
internal static extern int GdipDrawImagePointRectI(HandleRef graphics, HandleRef image, int x, int y, int srcx, int srcy, int srcwidth, int srcheight, int srcunit);
13471347

1348-
[DllImport(LibraryName, ExactSpelling = true)]
1348+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13491349
internal static extern int GdipDrawRectangle(HandleRef graphics, HandleRef pen, float x, float y, float width, float height);
13501350

1351-
[DllImport(LibraryName, ExactSpelling = true)]
1351+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13521352
internal static extern int GdipDrawRectangleI(HandleRef graphics, HandleRef pen, int x, int y, int width, int height);
13531353

1354-
[DllImport(LibraryName, ExactSpelling = true)]
1354+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13551355
internal static extern int GdipDrawRectangles(HandleRef graphics, HandleRef pen, RectangleF* rects, int count);
13561356

1357-
[DllImport(LibraryName, ExactSpelling = true)]
1357+
[DllImport(LibraryName, ExactSpelling = true, SetLastError = true)]
13581358
internal static extern int GdipDrawRectanglesI(HandleRef graphics, HandleRef pen, Rectangle* rects, int count);
13591359

13601360
[DllImport(LibraryName, ExactSpelling = true)]

0 commit comments

Comments
 (0)