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


このクラスは System.Runtime.InteropServices.CriticalHandle クラスから派生したクラスです。このクラスは無効なハンドルの形式を記述します。たとえば、無効なハンドル値として -1 を使用するハンドルもあれば、0 を使用するハンドルもあります。このクラスからさらにファイル ハンドルやレジストリ ハンドルなどへ派生して、さらなる特化が可能です。
既存のマネージ ラッパーを持たないアンマネージ リソースをラップする必要があるときは、CriticalHandleZeroOrMinusOneIsInvalid クラスを使用します。
![]() |
---|
クリティカル ハンドルのセキュリティおよびスレッド セーフに関する重要な情報については、CriticalHandle クラスのトピックを参照してください。 |

CriticalHandleZeroOrMinusOneIsInvalid クラスから派生したクラスを作成する方法のコード例を次に示します。この例では、アンマネージ メモリへのポインタをラップするクラスを作成します。
Imports System Imports System.Security.Permissions Imports System.Runtime.InteropServices Imports Microsoft.Win32.SafeHandles Class Example Public Shared Sub Main() Dim ptr As IntPtr = Marshal.AllocHGlobal(10) Console.WriteLine("Ten bytes of unmanaged memory allocated.") Dim memHabdle As New CriticalUnmanagedMemoryHandle(ptr) If memHabdle.IsInvalid Then Console.WriteLine("CriticalUnmanagedMemoryHandle is invalid!.") Else Console.WriteLine("CriticalUnmanagedMemoryHandle class initialized to unmanaged memory.") End If Console.ReadLine() End Sub End Class ' Demand unmanaged code permission to use this class. <SecurityPermission(SecurityAction.Demand, UnmanagedCode:=True)> _ NotInheritable Class CriticalUnmanagedMemoryHandle Inherits CriticalHandleZeroOrMinusOneIsInvalid ' Set the handle. Friend Sub New(ByVal preexistingHandle As IntPtr) SetHandle(preexistingHandle) End Sub ' 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 CriticalHandleExamples { class Example { public static void Main() { IntPtr ptr = Marshal.AllocHGlobal(10); Console.WriteLine("Ten bytes of unmanaged memory allocated."); CriticalUnmanagedMemoryHandle memHabdle = new CriticalUnmanagedMemoryHandle(ptr); if (memHabdle.IsInvalid) { Console.WriteLine("CriticalUnmanagedMemoryHandle is invalid!."); } else { Console.WriteLine("CriticalUnmanagedMemoryHandle class initialized to unmanaged memory."); } Console.ReadLine(); } } // Demand unmanaged code permission to use this class. [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)] sealed class CriticalUnmanagedMemoryHandle : CriticalHandleZeroOrMinusOneIsInvalid { // Set the handle. internal CriticalUnmanagedMemoryHandle(IntPtr preexistingHandle) { 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.CriticalHandle
Microsoft.Win32.SafeHandles.CriticalHandleZeroOrMinusOneIsInvalid


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


CriticalHandleZeroOrMinusOneIsInvalid コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)


CriticalHandleZeroOrMinusOneIsInvalid クラスから派生したクラスを作成する方法のコード例を次に示します。この例では、アンマネージ メモリへのポインタをラップするクラスを作成します。
Imports System Imports System.Security.Permissions Imports System.Runtime.InteropServices Imports Microsoft.Win32.SafeHandles Class Example Public Shared Sub Main() Dim ptr As IntPtr = Marshal.AllocHGlobal(10) Console.WriteLine("Ten bytes of unmanaged memory allocated.") Dim memHabdle As New CriticalUnmanagedMemoryHandle(ptr) If memHabdle.IsInvalid Then Console.WriteLine("CriticalUnmanagedMemoryHandle is invalid!.") Else Console.WriteLine("CriticalUnmanagedMemoryHandle class initialized to unmanaged memory.") End If Console.ReadLine() End Sub End Class ' Demand unmanaged code permission to use this class. <SecurityPermission(SecurityAction.Demand, UnmanagedCode:=True)> _ NotInheritable Class CriticalUnmanagedMemoryHandle Inherits CriticalHandleZeroOrMinusOneIsInvalid ' Set the handle. Friend Sub New(ByVal preexistingHandle As IntPtr) SetHandle(preexistingHandle) End Sub ' 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 CriticalHandleExamples { class Example { public static void Main() { IntPtr ptr = Marshal.AllocHGlobal(10); Console.WriteLine("Ten bytes of unmanaged memory allocated."); CriticalUnmanagedMemoryHandle memHabdle = new CriticalUnmanagedMemoryHandle(ptr); if (memHabdle.IsInvalid) { Console.WriteLine("CriticalUnmanagedMemoryHandle is invalid!."); } else { Console.WriteLine("CriticalUnmanagedMemoryHandle class initialized to unmanaged memory."); } Console.ReadLine(); } } // Demand unmanaged code permission to use this class. [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)] sealed class CriticalUnmanagedMemoryHandle : CriticalHandleZeroOrMinusOneIsInvalid { // Set the handle. internal CriticalUnmanagedMemoryHandle(IntPtr preexistingHandle) { 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; } } }

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

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


CriticalHandleZeroOrMinusOneIsInvalid フィールド
CriticalHandleZeroOrMinusOneIsInvalid プロパティ

名前 | 説明 | |
---|---|---|
![]() | IsClosed | ハンドルが閉じているかどうかを示す値を取得します。 ( CriticalHandle から継承されます。) |
![]() | IsInvalid | オーバーライドされます。 ハンドルが無効かどうかを示す値を取得します。 |

CriticalHandleZeroOrMinusOneIsInvalid メソッド

名前 | 説明 | |
---|---|---|
![]() | Close | リソースを解放するようにハンドルにマークを付けます。 ( CriticalHandle から継承されます。) |
![]() | Dispose | オーバーロードされます。 リソースを解放するようにハンドルにマークを付けます。 ( CriticalHandle から継承されます。) |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | SetHandleAsInvalid | ハンドルを無効としてマークします。 ( CriticalHandle から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 リソースを解放するようにハンドルにマークを付けます。 ( CriticalHandle から継承されます。) |
![]() | Finalize | ハンドルに関連付けられたすべてのリソースを解放します。 ( CriticalHandle から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
![]() | ReleaseHandle | 派生クラスでオーバーライドされると、ハンドルを解放するために必要なコードを実行します。 ( CriticalHandle から継承されます。) |
![]() | SetHandle | ハンドルを、指定した既存のハンドルに設定します。 ( CriticalHandle から継承されます。) |

CriticalHandleZeroOrMinusOneIsInvalid メンバ
値 0 または -1 が無効なハンドルである Win32 クリティカル ハンドルの実装の基本クラスを提供します。
CriticalHandleZeroOrMinusOneIsInvalid データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | CriticalHandleZeroOrMinusOneIsInvalid | CriticalHandleZeroOrMinusOneIsInvalid クラスの新しいインスタンスを初期化します。 |


名前 | 説明 | |
---|---|---|
![]() | IsClosed | ハンドルが閉じているかどうかを示す値を取得します。(CriticalHandle から継承されます。) |
![]() | IsInvalid | オーバーライドされます。 ハンドルが無効かどうかを示す値を取得します。 |

名前 | 説明 | |
---|---|---|
![]() | Close | リソースを解放するようにハンドルにマークを付けます。 (CriticalHandle から継承されます。) |
![]() | Dispose | オーバーロードされます。 リソースを解放するようにハンドルにマークを付けます。 (CriticalHandle から継承されます。) |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | SetHandleAsInvalid | ハンドルを無効としてマークします。 (CriticalHandle から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 リソースを解放するようにハンドルにマークを付けます。 (CriticalHandle から継承されます。) |
![]() | Finalize | ハンドルに関連付けられたすべてのリソースを解放します。 (CriticalHandle から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
![]() | ReleaseHandle | 派生クラスでオーバーライドされると、ハンドルを解放するために必要なコードを実行します。 (CriticalHandle から継承されます。) |
![]() | SetHandle | ハンドルを、指定した既存のハンドルに設定します。 (CriticalHandle から継承されます。) |

- CriticalHandleZeroOrMinusOneIsInvalidのページへのリンク