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

Dim instance As SafeHandle Dim returnValue As IntPtr returnValue = instance.DangerousGetHandle
handle フィールドの値を表す IntPtr。ハンドルが SetHandleAsInvalid で無効としてマークされている場合にも、このメソッドは元のハンドル値を返すため、返される値が古い値である可能性があります。

このメソッドを使用すると、SafeHandle の派生クラスのインスタンスから実際のハンドル値を取得できます。.NET Framework の多くのプロパティは IntPtr ハンドル型を返すため、このメソッドは下位互換性を保つために必要となります。IntPtr ハンドル型は、ポインタまたはハンドルを表すために使用するプラットフォーム固有の型です。
![]() |
---|
DangerousGetHandle メソッドを使用する場合、セキュリティ上のリスクが生じる可能性があります。ハンドルが SetHandleAsInvalid で無効としてマークされている場合にも、DangerousGetHandle は元のハンドル値を返すため、返される値が古い値である可能性があるためです。また、返されるハンドルはいつでもリサイクルできます。つまり、最善でもハンドルが突然機能しなくなる可能性があるということです。最悪の場合、ハンドルまたはハンドルが表すリソースが信頼関係のないコードに対して公開されると、再利用されるハンドルや返されるハンドルに対するリサイクル セキュリティ攻撃につながる可能性があります。たとえば、信頼関係のない呼び出し元が返されたハンドルでデータを照会し、まったく関係のないリソースの情報を受け取る可能性があります。 DangerousGetHandle メソッドを安全に使用する方法の詳細については、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.DangerousGetHandle メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As SafeHandle Dim returnValue As IntPtr returnValue = instance.DangerousGetHandle

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


- SafeHandle.DangerousGetHandleのページへのリンク