3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** Class: SafeLibraryHandle
10 ** <EMAIL>Author: David Gutierrez (Microsoft) </EMAIL>
12 ** A wrapper for a library handles
16 ===========================================================*/
19 using System.Security;
20 using System.Security.Permissions;
21 using System.Runtime.InteropServices;
22 using System.Runtime.CompilerServices;
23 using Microsoft.Win32;
24 using Microsoft.Win32.SafeHandles;
25 using System.Runtime.ConstrainedExecution;
26 using System.Runtime.Versioning;
28 namespace Microsoft.Win32.SafeHandles {
29 [HostProtectionAttribute(MayLeakOnAbort = true)]
30 [SuppressUnmanagedCodeSecurityAttribute]
31 internal sealed class SafeLibraryHandle : SafeHandleZeroOrMinusOneIsInvalid
33 // Note that LoadLibraryEx returns 0 on failure
35 internal SafeLibraryHandle() : base(true) {}
37 [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Unicode, SetLastError=true)]
38 [ResourceExposure(ResourceScope.Machine)]
39 internal static extern SafeLibraryHandle LoadLibraryEx(string libFilename, IntPtr reserved, int flags);
41 [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Unicode)]
42 [ResourceExposure(ResourceScope.None)]
43 #if !FEATURE_WINDOWSPHONE
44 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
45 #endif // !FEATURE_WINDOWSPHONE
46 private static extern bool FreeLibrary(IntPtr hModule);
48 override protected bool ReleaseHandle()
50 return FreeLibrary(handle);