IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
NativeLibrary.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Runtime.InteropServices;
4
5namespace IKVM.Runtime
6{
7
8#if FIRST_PASS == false && IMPORTER == FALSE && EXPORTER == false
9
15 static partial class NativeLibrary
16 {
17
18#if NETFRAMEWORK
19
20 readonly static LibIkvm libikvm = LibIkvm.Instance;
21
22#endif
23
29 public static string MapLibraryName(string name)
30 {
31 if (name == null)
32 return null;
33 if (Path.HasExtension(name))
34 return name;
35
37 return name + ".dll";
38 else if (RuntimeUtil.IsOSX)
39 return "lib" + name + ".dylib";
40 else
41 return "lib" + name + ".so";
42 }
43
50 static NativeLibraryHandle LoadImpl(string path)
51 {
52#if NETFRAMEWORK
53 var h = libikvm.dl_open(path);
54#else
55 System.Runtime.InteropServices.NativeLibrary.TryLoad(path, typeof(NativeLibrary).Assembly, DllImportSearchPath.SafeDirectories | DllImportSearchPath.UseDllDirectoryForDependencies, out nint h);
56#endif
57
58 return h != IntPtr.Zero ? new NativeLibraryHandle(h) : null;
59 }
60
66 public static NativeLibraryHandle Load(string nameOrPath)
67 {
68 if (Path.IsPathRooted(nameOrPath))
69 return LoadImpl(nameOrPath);
70
71 // give native loader a chance to load the library
72 if (LoadImpl(nameOrPath) is NativeLibraryHandle h1)
73 return h1;
74
75 // start looking at assembly path, or path given by environmental variable
76 var asml = typeof(NativeLibrary).Assembly.Location is string s && !string.IsNullOrEmpty(s) ? Path.GetDirectoryName(s) : null;
77 var root = Environment.GetEnvironmentVariable("IKVM_LIBRARY_PATH") ?? asml;
78
79 // assembly possible loaded in memory: we have no available search path
80 if (string.IsNullOrEmpty(root))
81 return null;
82
83 // check in root directory
84 if (Path.Combine(root, MapLibraryName(nameOrPath)) is string lib1)
85 if (File.Exists(lib1) && LoadImpl(lib1) is NativeLibraryHandle h2)
86 return h2;
87
88 // scan supported RIDs for current platform
89 foreach (var rid in RuntimeUtil.SupportedRuntimeIdentifiers)
90 if (Path.Combine(root, "runtimes", rid, "native", MapLibraryName(nameOrPath)) is string lib2)
91 if (File.Exists(lib2) && LoadImpl(lib2) is NativeLibraryHandle h3)
92 return h3;
93
94 return null;
95 }
96
102 internal static void Free(nint handle)
103 {
104#if NETFRAMEWORK
105 libikvm.dl_close(handle);
106#else
107 System.Runtime.InteropServices.NativeLibrary.Free(handle);
108#endif
109 }
110
117 static string MangleWin32ExportName(string name, int argl)
118 {
119 return argl == -1 ? name : $"_{name}@{argl}";
120 }
121
128 internal static string MangleExportName(string name, int argl)
129 {
130 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && RuntimeInformation.ProcessArchitecture == Architecture.X86)
131 return MangleWin32ExportName(name, argl);
132 else
133 return name;
134 }
135
143 internal static nint GetExport(nint handle, string name, int argl = -1)
144 {
145 name = MangleExportName(name, argl);
146
147#if NETFRAMEWORK
148 return libikvm.dl_sym(handle, name);
149#else
150 return System.Runtime.InteropServices.NativeLibrary.TryGetExport(handle, name, out var h) ? h : 0;
151#endif
152 }
153
154 }
155
156#endif
157
158}
IKVM.Reflection.Assembly Assembly
Provides methods to load libraries from standard .NET search locations. This class is not used to loa...
static NativeLibraryHandle Load(string nameOrPath)
Loads the given library in a platform dependent manner.
static string MapLibraryName(string name)
Returns a version of the library name with the OS specific prefix and suffix.
Maintains various information about the current runtime environment.
static IEnumerable< string > SupportedRuntimeIdentifiers
Gets the supported RIDs, returned in most specific order.
static bool IsOSX
Returns true if the current platform is Mac OS X.
static bool IsWindows
Returns true if the current platform is Windows.