|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Reflection; |
| 7 | +using System.Reflection.Emit; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using System.Runtime.ConstrainedExecution; |
| 10 | +using Win32Native = Microsoft.Win32.Win32Native; |
| 11 | +using System.Diagnostics; |
| 12 | + |
| 13 | +namespace System.Runtime.InteropServices |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// APIs for managing Native Libraries |
| 17 | + /// </summary> |
| 18 | + public static partial class NativeLibrary |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// NativeLibrary Loader: Simple API |
| 22 | + /// This method is a wrapper around OS loader, using "default" flags. |
| 23 | + /// </summary> |
| 24 | + /// <param name="libraryPath">The name of the native library to be loaded</param> |
| 25 | + /// <returns>The handle for the loaded native library</returns> |
| 26 | + /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception> |
| 27 | + /// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception> |
| 28 | + /// <exception cref="System.BadImageFormatException">If the library is not valid.</exception> |
| 29 | + public static IntPtr Load(string libraryPath) |
| 30 | + { |
| 31 | + if (libraryPath == null) |
| 32 | + throw new ArgumentNullException(nameof(libraryPath)); |
| 33 | + |
| 34 | + return LoadFromPath(libraryPath, throwOnError: true); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// NativeLibrary Loader: Simple API that doesn't throw |
| 39 | + /// </summary> |
| 40 | + /// <param name="libraryPath">The name of the native library to be loaded</param> |
| 41 | + /// <param name="handle">The out-parameter for the loaded native library handle</param> |
| 42 | + /// <returns>True on successful load, false otherwise</returns> |
| 43 | + /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception> |
| 44 | + public static bool TryLoad(string libraryPath, out IntPtr handle) |
| 45 | + { |
| 46 | + if (libraryPath == null) |
| 47 | + throw new ArgumentNullException(nameof(libraryPath)); |
| 48 | + |
| 49 | + handle = LoadFromPath(libraryPath, throwOnError: false); |
| 50 | + return handle != IntPtr.Zero; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// NativeLibrary Loader: High-level API |
| 55 | + /// Given a library name, this function searches specific paths based on the |
| 56 | + /// runtime configuration, input parameters, and attributes of the calling assembly. |
| 57 | + /// If DllImportSearchPath parameter is non-null, the flags in this enumeration are used. |
| 58 | + /// Otherwise, the flags specified by the DefaultDllImportSearchPaths attribute on the |
| 59 | + /// calling assembly (if any) are used. |
| 60 | + /// This LoadLibrary() method does not invoke the managed call-backs for native library resolution: |
| 61 | + /// * AssemblyLoadContext.LoadUnmanagedDll() |
| 62 | + /// </summary> |
| 63 | + /// <param name="libraryName">The name of the native library to be loaded</param> |
| 64 | + /// <param name="assembly">The assembly loading the native library</param> |
| 65 | + /// <param name="searchPath">The search path</param> |
| 66 | + /// <returns>The handle for the loaded library</returns> |
| 67 | + /// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception> |
| 68 | + /// <exception cref="System.ArgumentException">If assembly is not a RuntimeAssembly</exception> |
| 69 | + /// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception> |
| 70 | + /// <exception cref="System.BadImageFormatException">If the library is not valid.</exception> |
| 71 | + public static IntPtr Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) |
| 72 | + { |
| 73 | + if (libraryName == null) |
| 74 | + throw new ArgumentNullException(nameof(libraryName)); |
| 75 | + if (assembly == null) |
| 76 | + throw new ArgumentNullException(nameof(assembly)); |
| 77 | + if (!(assembly is RuntimeAssembly)) |
| 78 | + throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); |
| 79 | + |
| 80 | + return LoadByName(libraryName, |
| 81 | + ((RuntimeAssembly)assembly).GetNativeHandle(), |
| 82 | + searchPath.HasValue, |
| 83 | + (uint) searchPath.GetValueOrDefault(), |
| 84 | + throwOnError: true); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// NativeLibrary Loader: High-level API that doesn't throw. |
| 89 | + /// </summary> |
| 90 | + /// <param name="libraryName">The name of the native library to be loaded</param> |
| 91 | + /// <param name="searchPath">The search path</param> |
| 92 | + /// <param name="assembly">The assembly loading the native library</param> |
| 93 | + /// <param name="handle">The out-parameter for the loaded native library handle</param> |
| 94 | + /// <returns>True on successful load, false otherwise</returns> |
| 95 | + /// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception> |
| 96 | + /// <exception cref="System.ArgumentException">If assembly is not a RuntimeAssembly</exception> |
| 97 | + public static bool TryLoad(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle) |
| 98 | + { |
| 99 | + if (libraryName == null) |
| 100 | + throw new ArgumentNullException(nameof(libraryName)); |
| 101 | + if (assembly == null) |
| 102 | + throw new ArgumentNullException(nameof(assembly)); |
| 103 | + if (!(assembly is RuntimeAssembly)) |
| 104 | + throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); |
| 105 | + |
| 106 | + handle = LoadByName(libraryName, |
| 107 | + ((RuntimeAssembly)assembly).GetNativeHandle(), |
| 108 | + searchPath.HasValue, |
| 109 | + (uint) searchPath.GetValueOrDefault(), |
| 110 | + throwOnError: false); |
| 111 | + return handle != IntPtr.Zero; |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Free a loaded library |
| 116 | + /// Given a library handle, free it. |
| 117 | + /// No action if the input handle is null. |
| 118 | + /// </summary> |
| 119 | + /// <param name="handle">The native library handle to be freed</param> |
| 120 | + /// <exception cref="System.InvalidOperationException">If the operation fails</exception> |
| 121 | + public static void Free(IntPtr handle) |
| 122 | + { |
| 123 | + FreeLib(handle); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Get the address of an exported Symbol |
| 128 | + /// This is a simple wrapper around OS calls, and does not perform any name mangling. |
| 129 | + /// </summary> |
| 130 | + /// <param name="handle">The native library handle</param> |
| 131 | + /// <param name="name">The name of the exported symbol</param> |
| 132 | + /// <returns>The address of the symbol</returns> |
| 133 | + /// <exception cref="System.ArgumentNullException">If handle or name is null</exception> |
| 134 | + /// <exception cref="System.EntryPointNotFoundException">If the symbol is not found</exception> |
| 135 | + public static IntPtr GetExport(IntPtr handle, string name) |
| 136 | + { |
| 137 | + if (handle == IntPtr.Zero) |
| 138 | + throw new ArgumentNullException(nameof(handle)); |
| 139 | + if (name == null) |
| 140 | + throw new ArgumentNullException(nameof(name)); |
| 141 | + |
| 142 | + return GetSymbol(handle, name, throwOnError: true); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Get the address of an exported Symbol, but do not throw |
| 147 | + /// </summary> |
| 148 | + /// <param name="handle">The native library handle</param> |
| 149 | + /// <param name="name">The name of the exported symbol</param> |
| 150 | + /// <param name="address"> The out-parameter for the symbol address, if it exists</param> |
| 151 | + /// <returns>True on success, false otherwise</returns> |
| 152 | + /// <exception cref="System.ArgumentNullException">If handle or name is null</exception> |
| 153 | + public static bool TryGetExport(IntPtr handle, string name, out IntPtr address) |
| 154 | + { |
| 155 | + if (handle == IntPtr.Zero) |
| 156 | + throw new ArgumentNullException(nameof(handle)); |
| 157 | + if (name == null) |
| 158 | + throw new ArgumentNullException(nameof(name)); |
| 159 | + |
| 160 | + address = GetSymbol(handle, name, throwOnError: false); |
| 161 | + return address != IntPtr.Zero; |
| 162 | + } |
| 163 | + |
| 164 | + /// External functions that implement the NativeLibrary interface |
| 165 | + |
| 166 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 167 | + internal static extern IntPtr LoadFromPath(string libraryName, bool throwOnError); |
| 168 | + |
| 169 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 170 | + internal static extern IntPtr LoadByName(string libraryName, RuntimeAssembly callingAssembly, |
| 171 | + bool hasDllImportSearchPathFlag, uint dllImportSearchPathFlag, |
| 172 | + bool throwOnError); |
| 173 | + |
| 174 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 175 | + internal static extern void FreeLib(IntPtr handle); |
| 176 | + |
| 177 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 178 | + internal static extern IntPtr GetSymbol(IntPtr handle, string symbolName, bool throwOnError); |
| 179 | + } |
| 180 | +} |
0 commit comments