SafeHandle.DangerousRelease メソッド
アセンブリ: mscorlib (mscorlib.dll 内)


DangerousRelease メソッドは、DangerousAddRef に対応するメソッドです。DangerousRelease メソッドの呼び出しを DangerousAddRef の正常な呼び出しと常に一致させる必要があります。
![]() |
---|
このメソッドは上級ユーザーを対象としているため、使用する場合には常に注意が必要です。ハンドル リソースのリークを防ぐために、このメソッドは必ず、スレッドの中止によって処理を中断することのできない制約された実行領域 (CER: Constrained Execution Region) 内で呼び出してください。一致しない DangerousAddRef の呼び出しがリソース リークの原因となる場合があるのと同様に、一致しない DangerousRelease の呼び出しによって、無効なハンドルの状態が他のスレッドから参照可能になる場合があります。信頼関係のないコードに対して、DangerousAddRef または DangerousRelease の呼び出しを公開しないでください。 |

SafeHandle クラスを実装する MySafeHandle クラスというカスタム クラスで、DangerousAddRef、DangerousGetHandle、および DangerousRelease を使用する方法を次のコード例に示します。
Imports System Imports System.Runtime.InteropServices Imports System.Runtime.CompilerServices Imports System.Runtime.ConstrainedExecution Imports System.Security.Permissions NotInheritable Class MySafeHandle Inherits SafeHandle ' Called by P/Invoke when returning SafeHandles Public Sub New() MyBase.New(IntPtr.Zero, True) End Sub ' If & only if you need to support user-supplied handles Friend Sub New(ByVal preexistingHandle As IntPtr, ByVal ownsHandle As Boolean) MyBase.New(IntPtr.Zero, ownsHandle) SetHandle(preexistingHandle) End Sub ' Do not provide a finalizer - SafeHandle's critical finalizer will ' call ReleaseHandle for you. Public Overrides ReadOnly Property IsInvalid() As Boolean Get Return IsClosed OrElse handle = IntPtr.Zero End Get End Property <DllImport("kernel32"), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)> _ Private Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean End Function Protected Overrides Function ReleaseHandle() As Boolean Return CloseHandle(handle) End Function <DllImport("kernel32")> _ Public Shared Function CreateHandle(ByVal someState As Integer) As MySafeHandle End Function End Class Public Class Example Shared Sub Main() Run() End Sub <SecurityPermission(SecurityAction.Demand, UnmanagedCode:=true)> _ shared Sub Run() 'Create an instance of MySafeHandle. Dim myHandle As New MySafeHandle() ' ...Set the handle... Dim mustRelease As Boolean = False RuntimeHelpers.PrepareConstrainedRegions() Try myHandle.DangerousAddRef(mustRelease) Dim handleCopy As IntPtr = myHandle.DangerousGetHandle() ' ... perform operations with handleCopy ... Finally If mustRelease Then myHandle.DangerousRelease() End If End Try End Sub End Class
using System; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Security.Permissions; namespace SafeHandleExample { sealed class MySafeHandle : SafeHandle { // Called by P/Invoke when returning SafeHandles public MySafeHandle() : base(IntPtr.Zero, true) { } // If & only if you need to support user-supplied handles internal MySafeHandle(IntPtr preexistingHandle, bool ownsHandle) : base(IntPtr.Zero, ownsHandle) { SetHandle(preexistingHandle); } // Do not provide a finalizer - SafeHandle's critical finalizer will // call ReleaseHandle for you. public override bool IsInvalid { get { return IsClosed || handle == IntPtr.Zero; } } [DllImport("kernel32")] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] private static extern bool CloseHandle(IntPtr handle); protected override bool ReleaseHandle() { return CloseHandle(handle); } [DllImport("kernel32")] public static extern MySafeHandle CreateHandle(int someState); } public class Example { static void Main() { Run(); } [SecurityPermission(SecurityAction.Demand, UnmanagedCode=true)] static void Run() { //Create an instance of MySafeHandle. MySafeHandle myHandle = new MySafeHandle(); // ...Set the handle... bool mustRelease = false; RuntimeHelpers.PrepareConstrainedRegions(); try { myHandle.DangerousAddRef(ref mustRelease); IntPtr handleCopy = myHandle.DangerousGetHandle(); // ... perform operations with handleCopy ... } finally { if (mustRelease) myHandle.DangerousRelease(); } } } }

- SecurityPermission (アンマネージ コードを呼び出すために必要なアクセス許可)。 UnmanagedCode (関連する列挙体)。LinkDemand (セキュリティ アクション)。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SafeHandle.DangerousRelease メソッドのページへのリンク