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


DangerousAddRef メソッドは、ハンドルで使用するメモリを共通言語ランタイムが再要求するのを防ぎます (メモリの再要求は、ランタイムが ReleaseHandle メソッドを呼び出した場合に発生します)。このメソッドを使用すると、SafeHandle インスタンスの参照カウントを手動でインクリメントできます。DangerousAddRef は、参照カウントが正常にインクリメントされたかどうかを示す ref パラメータ (success) を使用してブール値を返します。これにより、エラーが発生した場合にプログラム ロジックでバックアウトできます。DangerousAddRef を呼び出す前に、success に false を設定しておく必要があります。success が true の場合は、DangerousAddRef の呼び出しを DangerousRelease の対応する呼び出しと一致させることによって、リソース リークを防ぎます。
![]() |
---|
このメソッドは上級ユーザーを対象としているため、使用する場合には常に注意が必要です。ハンドル リソースのリークを防ぐために、このメソッドは必ず、スレッドの中止によって処理を中断することのできない制約された実行領域 (CER: Constrained Execution Region) 内で呼び出してください。 |

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からSafeHandle.DangerousAddRef メソッドを検索する場合は、下記のリンクをクリックしてください。

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