Skip to content

Commit

Permalink
Merge pull request dotnet#24123 from mellinoe/system.drawing-fix-osx-…
Browse files Browse the repository at this point in the history
…loading

Fix libgdiplus function loading on OSX.
  • Loading branch information
mellinoe authored Sep 18, 2017
2 parents c017ae3 + bc9a9d7 commit bafbafd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/System.Drawing.Common/src/System.Drawing.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
<Reference Include="System.ComponentModel" />
<Reference Include="System.ComponentModel.Primitives" />
<Reference Include="System.Threading" />
Expand Down Expand Up @@ -365,4 +366,4 @@
</EmbeddedResource>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>
22 changes: 14 additions & 8 deletions src/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@ internal unsafe partial class Gdip
{
private static IntPtr LoadNativeLibrary()
{
// Various Unix package managers have chosen different names for the "libgdiplus" shared library.
// The mono project, where libgdiplus originated, allowed both of the names below to be used, via
// a global configuration setting. We prefer the "unversioned" shared object name, and fallback to
// the name suffixed with ".0".
IntPtr lib = Interop.Libdl.dlopen("libgdiplus.so", Interop.Libdl.RTLD_NOW);
if (lib == IntPtr.Zero)
IntPtr lib = IntPtr.Zero;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
lib = Interop.Libdl.dlopen("libgdiplus.so.0", Interop.Libdl.RTLD_NOW);
lib = Interop.Libdl.dlopen("libgdiplus.dylib", Interop.Libdl.RTLD_NOW);
}
else
{
// Various Unix package managers have chosen different names for the "libgdiplus" shared library.
// The mono project, where libgdiplus originated, allowed both of the names below to be used, via
// a global configuration setting. We prefer the "unversioned" shared object name, and fallback to
// the name suffixed with ".0".
lib = Interop.Libdl.dlopen("libgdiplus.so", Interop.Libdl.RTLD_NOW);
if (lib == IntPtr.Zero)
{
throw new DllNotFoundException(SR.LibgdiplusNotFound);
lib = Interop.Libdl.dlopen("libgdiplus.so.0", Interop.Libdl.RTLD_NOW);
}
}

// This function may return a null handle. If it does, individual functions loaded from it will throw a DllNotFoundException,
// but not until an attempt is made to actually use the function (rather than load it). This matches how PInvokes behave.
return lib;
}

Expand Down
14 changes: 11 additions & 3 deletions src/System.Drawing.Common/tests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ public static bool GetGdiplusIsAvailable()
}
else
{
IntPtr nativeLib = dlopen("libgdiplus.so", RTLD_NOW);
if (nativeLib == IntPtr.Zero)
IntPtr nativeLib;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
nativeLib = dlopen("libgdiplus.so.0", RTLD_NOW);
nativeLib = dlopen("libgdiplus.dylib", RTLD_NOW);
}
else
{
nativeLib = dlopen("libgdiplus.so", RTLD_NOW);
if (nativeLib == IntPtr.Zero)
{
nativeLib = dlopen("libgdiplus.so.0", RTLD_NOW);
}
}

return nativeLib != IntPtr.Zero;
Expand Down

0 comments on commit bafbafd

Please sign in to comment.