SafeHandleZeroOrMinusOneIsInvalid クラス
アセンブリ: mscorlib (mscorlib.dll 内)


このクラスは System.Runtime.InteropServices.SafeHandle クラスから派生したクラスです。このクラスは無効なハンドルの形式を記述します。たとえば、無効なハンドル値として -1 を使用するハンドルもあれば、0 を使用するハンドルもあります。このクラスからさらにファイル ハンドルやレジストリ ハンドルなどへ派生して、さらなる特化が可能です。SafeHandleZeroOrMinusOneIsInvalid から派生したクラスの例については、SafeFileHandle クラスのトピックを参照してください。
既存のマネージ ラッパーを持たないアンマネージ リソースを安全にラップする必要があるときは、SafeHandleZeroOrMinusOneIsInvalid クラスを使用します。

SafeHandleZeroOrMinusOneIsInvalid クラスから派生したクラスを作成する方法のコード例を次に示します。この例では、アンマネージ メモリへのポインタをラップするクラスを作成します。
Imports System Imports System.Security.Permissions Imports System.Runtime.InteropServices Imports Microsoft.Win32.SafeHandles Module Example Sub Main() Dim ptr As IntPtr = Marshal.AllocHGlobal(10) Console.WriteLine("Ten bytes of unmanaged memory allocated.") Dim memHabdle As New SafeUnmanagedMemoryHandle(ptr, True) If memHabdle.IsInvalid Then Console.WriteLine("SafeUnmanagedMemoryHandle is invalid!.") Else Console.WriteLine("SafeUnmanagedMemoryHandle class initialized to unmanaged memory.") End If Console.ReadLine() End Sub End Module ' Demand unmanaged code permission to use this class. <SecurityPermission(SecurityAction.Demand, UnmanagedCode:=True)> _ NotInheritable Class SafeUnmanagedMemoryHandle Inherits SafeHandleZeroOrMinusOneIsInvalid ' Set ownsHandle to true for the default constructor. Friend Sub New() MyBase.New(True) End Sub 'New ' Set the handle and set ownsHandle to true. Friend Sub New(ByVal preexistingHandle As IntPtr, ByVal ownsHandle As Boolean) MyBase.New(ownsHandle) SetHandle(preexistingHandle) End Sub 'New ' Perform any specific actions to release the ' handle in the ReleaseHandle method. ' Often, you need to use Pinvoke to make ' a call into the Win32 API to release the ' handle. In this case, however, we can use ' the Marshal class to release the unmanaged ' memory. Protected Overrides Function ReleaseHandle() As Boolean ' "handle" is the internal ' value for the IntPtr handle. ' If the handle was set, ' free it. Return success. If handle <> IntPtr.Zero Then ' Free the handle. Marshal.FreeHGlobal(handle) ' Set the handle to zero. handle = IntPtr.Zero ' Return success. Return True End If ' Return false. Return False End Function End Class
using System; using System.Security.Permissions; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; namespace SafeHandleExamples { class Example { public static void Main() { IntPtr ptr = Marshal.AllocHGlobal(10); Console.WriteLine("Ten bytes of unmanaged memory allocated."); SafeUnmanagedMemoryHandle memHabdle = new SafeUnmanagedMemoryHandle(ptr, true); if (memHabdle.IsInvalid) { Console.WriteLine("SafeUnmanagedMemoryHandle is invalid!."); } else { Console.WriteLine("SafeUnmanagedMemoryHandle class initialized to unmanaged memory."); } Console.ReadLine(); } } // Demand unmanaged code permission to use this class. [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)] sealed class SafeUnmanagedMemoryHandle : SafeHandleZeroOrMinusOneIsInvalid { // Set ownsHandle to true for the default constructor. internal SafeUnmanagedMemoryHandle() : base(true) { } // Set the handle and set ownsHandle to true. internal SafeUnmanagedMemoryHandle(IntPtr preexistingHandle, bool ownsHandle) : base(ownsHandle) { SetHandle(preexistingHandle); } // Perform any specific actions to release the // handle in the ReleaseHandle method. // Often, you need to use Pinvoke to make // a call into the Win32 API to release the // handle. In this case, however, we can use // the Marshal class to release the unmanaged // memory. override protected bool ReleaseHandle() { // "handle" is the internal // value for the IntPtr handle. // If the handle was set, // free it. Return success. if (handle != IntPtr.Zero) { // Free the handle. Marshal.FreeHGlobal(handle); // Set the handle to zero. handle = IntPtr.Zero; // Return success. return true; } // Return false. return false; } } }

System.Runtime.ConstrainedExecution.CriticalFinalizerObject
System.Runtime.InteropServices.SafeHandle
Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
Microsoft.Win32.SafeHandles.SafeFileHandle
Microsoft.Win32.SafeHandles.SafeWaitHandle


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


- SafeHandleZeroOrMinusOneIsInvalid クラスのページへのリンク