SafeHandleZeroOrMinusOneIsInvalid コンストラクタとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > SafeHandleZeroOrMinusOneIsInvalid コンストラクタの意味・解説 

SafeHandleZeroOrMinusOneIsInvalid コンストラクタ

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

SafeHandleZeroOrMinusOneIsInvalid クラス新しインスタンス初期化しハンドル安全に解放するかどうか指定します

名前空間: Microsoft.Win32.SafeHandles
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Protected Sub New ( _
    ownsHandle As Boolean _
)
Dim ownsHandle As Boolean

Dim instance As New SafeHandleZeroOrMinusOneIsInvalid(ownsHandle)
protected SafeHandleZeroOrMinusOneIsInvalid (
    bool ownsHandle
)
protected:
SafeHandleZeroOrMinusOneIsInvalid (
    bool ownsHandle
)
protected SafeHandleZeroOrMinusOneIsInvalid (
    boolean ownsHandle
)
protected function SafeHandleZeroOrMinusOneIsInvalid
 (
    ownsHandle : boolean
)

パラメータ

ownsHandle

終了処理中ハンドル安全に解放する場合true安全な解放行わない場合false (お勧めしません)。

使用例使用例

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;

        }
    }
}

.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SafeHandleZeroOrMinusOneIsInvalid クラス
SafeHandleZeroOrMinusOneIsInvalid メンバ
Microsoft.Win32.SafeHandles 名前空間
SafeHandle


このページでは「.NET Framework クラス ライブラリ リファレンス」からSafeHandleZeroOrMinusOneIsInvalid コンストラクタを検索した結果を表示しています。
Weblioに収録されているすべての辞書からSafeHandleZeroOrMinusOneIsInvalid コンストラクタを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からSafeHandleZeroOrMinusOneIsInvalid コンストラクタ を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「SafeHandleZeroOrMinusOneIsInvalid コンストラクタ」の関連用語

SafeHandleZeroOrMinusOneIsInvalid コンストラクタのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SafeHandleZeroOrMinusOneIsInvalid コンストラクタのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS