Skip to content

Commit 5c006d3

Browse files
committed
Use dynamic loading instead of DllImport to access AppKit library on macOS.
1 parent 1ff5e6f commit 5c006d3

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

MacOS/AppKit/NSApplication.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,8 @@ static NSApplication()
7373
{
7474
if (Platform.IsNotMacOS)
7575
return;
76-
var libHandle = NativeLibrary.Load(NativeLibraryNames.AppKit);
77-
if (libHandle != IntPtr.Zero)
78-
{
79-
NSAppPtr = (IntPtr*)NativeLibrary.GetExport(libHandle, "NSApp");
80-
}
76+
var libHandle = NativeLibraryHandles.AppKit;
77+
NSAppPtr = (IntPtr*)NativeLibrary.GetExport(libHandle, "NSApp");
8178
NSApplicationClass = Class.GetClass("NSApplication").AsNonNull();
8279
}
8380

MacOS/NativeLibraryHandles.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ namespace CarinaStudio.MacOS;
99
public static class NativeLibraryHandles
1010
{
1111
// Constants.
12+
static readonly string[] AppKitLibPaths =
13+
[
14+
"/System/Library/Frameworks/AppKit.framework/AppKit"
15+
];
1216
static readonly string[] CoreFoundationLibPaths =
1317
[
1418
"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation",
@@ -27,12 +31,20 @@ public static class NativeLibraryHandles
2731

2832

2933
// Fields.
34+
static IntPtr appKitLibHandle;
3035
static IntPtr coreFoundationLibHandle;
3136
static IntPtr coreGraphicsLibHandle;
37+
static bool isAppKitLibResolved;
3238
static bool isCoreFoundationLibResolved;
3339
static bool isCoreGraphicsLibResolved;
3440
static bool isImageIOLibResolved;
3541
static IntPtr imageIOLibHandle;
42+
43+
44+
/// <summary>
45+
/// Handle of AppKit library.
46+
/// </summary>
47+
public static IntPtr AppKit => GetHandle(AppKitLibPaths, ref appKitLibHandle, ref isAppKitLibResolved);
3648

3749

3850
/// <summary>

MacOS/NativeLibraryNames.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ public static class NativeLibraryNames
1010
/// <summary>
1111
/// AppKit.
1212
/// </summary>
13+
[Obsolete("The path to AppKit may be different between macOS, use NativeLibraryHandles.AppKit instead.")]
1314
public const string AppKit = "/System/Library/Frameworks/AppKit.framework/AppKit";
1415
/// <summary>
1516
/// Core Foundation.
1617
/// </summary>
17-
[Obsolete("The path to CoreFoundation may be different between macOS, use NativeLibraryHandles.CoreFoundation instead.")]
18+
[Obsolete("The path to Core Foundation may be different between macOS, use NativeLibraryHandles.CoreFoundation instead.")]
1819
public const string CoreFoundation = "/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreFoundation.framework/CoreFoundation";
1920
/// <summary>
2021
/// Core Graphics.
2122
/// </summary>
22-
[Obsolete("The path to CoreFoundation may be different between macOS, use NativeLibraryHandles.CoreGraphics instead.")]
23+
[Obsolete("The path to Core Graphics may be different between macOS, use NativeLibraryHandles.CoreGraphics instead.")]
2324
public const string CoreGraphics = "/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/CoreGraphics";
2425
/// <summary>
2526
/// Image I/O.
2627
/// </summary>
27-
[Obsolete("The path to CoreFoundation may be different between macOS, use NativeLibraryHandles.ImageIO instead.")]
28+
[Obsolete("The path to ImageIO may be different between macOS, use NativeLibraryHandles.ImageIO instead.")]
2829
public const string ImageIO = "/System/Library/Frameworks/ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO";
2930
/// <summary>
3031
/// Objective-C Runtime.

0 commit comments

Comments
 (0)