@@ -1770,12 +1770,14 @@ public static void ZeroFreeGlobalAllocUnicode(IntPtr s)
17701770 /// <param name="libraryName">The name of the native library to be loaded</param>
17711771 /// <returns>The handle for the loaded native library</returns>
17721772 /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception>
1773- /// <exception cref="System.DllNotFoundException ">Thrown if the library can't be found.</exception>
1774- /// <exception cref="System.BadImageFormatException">Thrown if the library is not valid.</exception>
1773+ /// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception>
1774+ /// <exception cref="System.BadImageFormatException">If the library is not valid.</exception>
17751775 public static IntPtr LoadLibrary ( string libraryPath )
17761776 {
1777- bool throwOnError = true ;
1778- return LoadLibraryFromPath ( libraryPath , throwOnError ) ;
1777+ if ( libraryPath == null )
1778+ throw new ArgumentNullException ( nameof ( libraryPath ) ) ;
1779+
1780+ return LoadLibraryFromPath ( libraryPath , throwOnError : true ) ;
17791781 }
17801782
17811783 /// <summary>
@@ -1787,32 +1789,45 @@ public static IntPtr LoadLibrary(string libraryPath)
17871789 /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception>
17881790 public static bool TryLoadLibrary ( string libraryPath , out IntPtr handle )
17891791 {
1790- bool throwOnError = false ;
1791- handle = LoadLibraryFromPath ( libraryPath , throwOnError ) ;
1792+ if ( libraryPath == null )
1793+ throw new ArgumentNullException ( nameof ( libraryPath ) ) ;
1794+
1795+ handle = LoadLibraryFromPath ( libraryPath , throwOnError : false ) ;
17921796 return handle != IntPtr . Zero ;
17931797 }
17941798
17951799 /// <summary>
17961800 /// NativeLibrary Loader: High-level API
17971801 /// Given a library name, this function searches specific paths based on the
1798- /// runtime configuration and attributes of the calling module.
1802+ /// runtime configuration, input parameters, and attributes of the calling assembly.
1803+ /// If DllImportSearchPath parameter is non-null, the flags in this enumeration are used.
1804+ /// Otherwise, the flags specified by the DefaultDllImportSearchPaths attribute on the
1805+ /// calling assembly (if any) are used.
17991806 /// This LoadLibrary() method does not invoke the managed call-backs for native library resolution:
18001807 /// * AssemblyLoadContext.LoadUnmanagedDll()
18011808 /// </summary>
18021809 /// <param name="libraryName">The name of the native library to be loaded</param>
18031810 /// <param name="dllImportSearchPath">The search path</param>
18041811 /// <param name="assembly">The assembly loading the native library</param>
18051812 /// <returns>The handle for the loaded library</returns>
1806- /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception>
1807- /// <exception cref="System.ArgumentNullException ">If assembly is null </exception>
1808- /// <exception cref="System.DllNotFoundException ">Thrown if the library can't be found.</exception>
1809- /// <exception cref="System.BadImageFormatException">Thrown if the library is not valid.</exception>
1813+ /// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception>
1814+ /// <exception cref="System.ArgumentException ">If assembly is not a RuntimeAssembly </exception>
1815+ /// <exception cref="System.DllNotFoundException ">If the library can't be found.</exception>
1816+ /// <exception cref="System.BadImageFormatException">If the library is not valid.</exception>
18101817 public static IntPtr LoadLibrary ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath )
18111818 {
1812- RuntimeAssembly runtimeAssembly = ( assembly != null ) ? ( ( RuntimeAssembly ) assembly ) . GetNativeHandle ( ) : null ;
1813- bool throwOnError = true ;
1814- uint searchPathFlag = searchPath . HasValue ? ( uint ) searchPath . Value : 0 ;
1815- return LoadLibraryByName ( libraryName , runtimeAssembly , searchPath . HasValue , searchPathFlag , throwOnError ) ;
1819+ if ( libraryName == null )
1820+ throw new ArgumentNullException ( nameof ( libraryName ) ) ;
1821+ if ( assembly == null )
1822+ throw new ArgumentNullException ( nameof ( assembly ) ) ;
1823+ if ( ! ( assembly is RuntimeAssembly ) )
1824+ throw new ArgumentException ( SR . Argument_MustBeRuntimeAssembly ) ;
1825+
1826+ return LoadLibraryByName ( libraryName ,
1827+ ( ( RuntimeAssembly ) assembly ) . GetNativeHandle ( ) ,
1828+ searchPath . HasValue ,
1829+ ( uint ) searchPath . GetValueOrDefault ( ) ,
1830+ throwOnError : true ) ;
18161831 }
18171832
18181833 /// <summary>
@@ -1823,14 +1838,22 @@ public static IntPtr LoadLibrary(string libraryName, Assembly assembly, DllImpor
18231838 /// <param name="assembly">The assembly loading the native library</param>
18241839 /// <param name="handle">The out-parameter for the loaded native library handle</param>
18251840 /// <returns>True on successful load, false otherwise</returns>
1826- /// <exception cref="System.ArgumentNullException">If libraryPath is null</exception>
1827- /// <exception cref="System.ArgumentNullException ">If assembly is null and dllImportSearchPath includes AssemblyDirectory </exception>
1841+ /// <exception cref="System.ArgumentNullException">If libraryPath or assembly is null</exception>
1842+ /// <exception cref="System.ArgumentException ">If assembly is not a RuntimeAssembly </exception>
18281843 public static bool TryLoadLibrary ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath , out IntPtr handle )
18291844 {
1830- RuntimeAssembly runtimeAssembly = ( assembly != null ) ? ( ( RuntimeAssembly ) assembly ) . GetNativeHandle ( ) : null ;
1831- bool throwOnError = false ;
1832- uint searchPathFlag = searchPath . HasValue ? ( uint ) searchPath . Value : 0 ;
1833- handle = LoadLibraryByName ( libraryName , runtimeAssembly , searchPath . HasValue , searchPathFlag , throwOnError ) ;
1845+ if ( libraryName == null )
1846+ throw new ArgumentNullException ( nameof ( libraryName ) ) ;
1847+ if ( assembly == null )
1848+ throw new ArgumentNullException ( nameof ( assembly ) ) ;
1849+ if ( ! ( assembly is RuntimeAssembly ) )
1850+ throw new ArgumentException ( SR . Argument_MustBeRuntimeAssembly ) ;
1851+
1852+ handle = LoadLibraryByName ( libraryName ,
1853+ ( ( RuntimeAssembly ) assembly ) . GetNativeHandle ( ) ,
1854+ searchPath . HasValue ,
1855+ ( uint ) searchPath . GetValueOrDefault ( ) ,
1856+ throwOnError : false ) ;
18341857 return handle != IntPtr . Zero ;
18351858 }
18361859
@@ -1854,12 +1877,15 @@ public static void FreeLibrary(IntPtr handle)
18541877 /// <param name="name">The name of the exported symbol</param>
18551878 /// <returns>The address of the symbol</returns>
18561879 /// <exception cref="System.ArgumentNullException">If handle or name is null</exception>
1857- /// <exception cref="System.ArgumentException">If name cannot be converted to UTF8</exception>
18581880 /// <exception cref="System.EntryPointNotFoundException">If the symbol is not found</exception>
18591881 public static IntPtr GetLibraryExport ( IntPtr handle , string name )
18601882 {
1861- bool throwOnError = true ;
1862- return GetNativeLibraryExport ( handle , name , throwOnError ) ;
1883+ if ( handle == IntPtr . Zero )
1884+ throw new ArgumentNullException ( nameof ( handle ) ) ;
1885+ if ( name == null )
1886+ throw new ArgumentNullException ( nameof ( name ) ) ;
1887+
1888+ return GetNativeLibraryExport ( handle , name , throwOnError : true ) ;
18631889 }
18641890
18651891 /// <summary>
@@ -1869,10 +1895,15 @@ public static IntPtr GetLibraryExport(IntPtr handle, string name)
18691895 /// <param name="name">The name of the exported symbol</param>
18701896 /// <param name="address"> The out-parameter for the symbol address, if it exists</param>
18711897 /// <returns>True on success, false otherwise</returns>
1898+ /// <exception cref="System.ArgumentNullException">If handle or name is null</exception>
18721899 public static bool TryGetLibraryExport ( IntPtr handle , string name , out IntPtr address )
18731900 {
1874- bool throwOnError = false ;
1875- address = GetNativeLibraryExport ( handle , name , throwOnError ) ;
1901+ if ( handle == IntPtr . Zero )
1902+ throw new ArgumentNullException ( nameof ( handle ) ) ;
1903+ if ( name == null )
1904+ throw new ArgumentNullException ( nameof ( name ) ) ;
1905+
1906+ address = GetNativeLibraryExport ( handle , name , throwOnError : false ) ;
18761907 return address != IntPtr . Zero ;
18771908 }
18781909
0 commit comments