Skip to content

Commit

Permalink
Fix manual delegate interop in System.Drawing.Common to handle nulls …
Browse files Browse the repository at this point in the history
…correctly. (#71225)
  • Loading branch information
jkoritzinsky authored Jun 23, 2022
1 parent 8ec8870 commit 6c209dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private delegate Interop.BOOL EnumerateMetafileProcNative(
public EnumerateMetafileProcMarshaller(EnumerateMetafileProc? managed)
{
_managed = managed is null ? null : (recordType, flags, dataSize, data, callbackData) =>
managed(recordType, flags, dataSize, data, Marshal.GetDelegateForFunctionPointer<PlayRecordCallback>(callbackData)) ? Interop.BOOL.TRUE : Interop.BOOL.FALSE;
managed(recordType, flags, dataSize, data, callbackData == IntPtr.Zero ? null! : Marshal.GetDelegateForFunctionPointer<PlayRecordCallback>(callbackData)) ? Interop.BOOL.TRUE : Interop.BOOL.FALSE;
_nativeFunction = _managed is null ? null : (delegate* unmanaged<IntPtr, Interop.BOOL>)Marshal.GetFunctionPointerForDelegate(_managed);
}

Expand Down
14 changes: 11 additions & 3 deletions src/libraries/System.Drawing.Common/src/System/Drawing/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ public abstract class Image : MarshalByRefObject, IDisposable, ICloneable, ISeri
internal unsafe struct GetThumbnailImageAbortMarshaller
{
private delegate Interop.BOOL GetThumbnailImageAbortNative(IntPtr callbackdata);
private GetThumbnailImageAbortNative _managed;
private GetThumbnailImageAbortNative? _managed;
private delegate* unmanaged<IntPtr, Interop.BOOL> _nativeFunction;
public GetThumbnailImageAbortMarshaller(GetThumbnailImageAbort managed)
{
_managed = data => managed() ? Interop.BOOL.TRUE : Interop.BOOL.FALSE;
_nativeFunction = (delegate* unmanaged<IntPtr, Interop.BOOL>)Marshal.GetFunctionPointerForDelegate(_managed);
if (managed is null)
{
_managed = null;
_nativeFunction = null;
}
else
{
_managed = data => managed() ? Interop.BOOL.TRUE : Interop.BOOL.FALSE;
_nativeFunction = (delegate* unmanaged<IntPtr, Interop.BOOL>)Marshal.GetFunctionPointerForDelegate(_managed);
}
}

public delegate* unmanaged<IntPtr, Interop.BOOL> ToNativeValue()
Expand Down

0 comments on commit 6c209dc

Please sign in to comment.