SafeHandle.DangerousAddRef メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > SafeHandle.DangerousAddRef メソッドの意味・解説 

SafeHandle.DangerousAddRef メソッド

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

SafeHandle インスタンスの参照カウンタを手動でインクリメントします。

名前空間: System.Runtime.InteropServices
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub DangerousAddRef ( _
    ByRef success As Boolean
 _
)
Dim instance As SafeHandle
Dim success As Boolean

instance.DangerousAddRef(success)
public void DangerousAddRef (
    ref bool success
)
public:
void DangerousAddRef (
    bool% success
)
public void DangerousAddRef (
    /** @ref */ boolean success
)
JScript では、値型の引数は参照渡しされません。

パラメータ

success

参照カウンタが正常にインクリメントされた場合は true。それ以外の場合は false。

解説解説
使用例使用例

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();
            }

        }
    }

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



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

SafeHandle.DangerousAddRef メソッドのお隣キーワード
検索ランキング

   

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



SafeHandle.DangerousAddRef メソッドのページの著作権

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

©2026 GRAS Group, Inc.RSS