Advertisement
elektrohacker

ComObjectDisposer class

Mar 30th, 2022
1,638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.02 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : ElektroStudios
  3. ' Modified : 30-March-2022
  4. ' ***********************************************************************
  5.  
  6. #Region " Option Statements "
  7.  
  8. Option Strict On
  9. Option Explicit On
  10. Option Infer Off
  11.  
  12. #End Region
  13.  
  14. #Region " Usage Examples "
  15.  
  16. ' Dim shellItem As IShellItem = Nothing
  17. ' NativeMethods.SHCreateItemFromParsingName("C:\", IntPtr.Zero, GetType(IShellItem).GUID, shellItem)
  18. '
  19. ' Using comObjectDisposer As New ComObjectDisposer(Of IShellItem)(shellItem)
  20. '     Console.WriteLine(comObjectDisposer.Value.GetDisplayName(ShellItemGetDisplayName.NormalDisplay).ToString())
  21. ' End Using
  22.  
  23. #End Region
  24.  
  25. #Region " Imports "
  26.  
  27. Imports System.Runtime.InteropServices
  28.  
  29. #End Region
  30.  
  31. #Region " ComObjectDisposer "
  32.  
  33. ' ReSharper disable once CheckNamespace
  34.  
  35. Namespace DevCase.Interop.Unmanaged
  36.  
  37.     ''' ----------------------------------------------------------------------------------------------------
  38.     ''' <summary>
  39.     ''' A wrapper for COM objects, which implements <see cref="IDisposable"/>.
  40.     ''' </summary>
  41.     ''' ----------------------------------------------------------------------------------------------------
  42.     ''' <typeparam name="T">
  43.     ''' The type of COM object.
  44.     ''' </typeparam>
  45.     ''' ----------------------------------------------------------------------------------------------------
  46.     ''' <remarks>
  47.     ''' Original implementation: <see href="https://github.com/misterhaan/au.Shared/blob/master/IO/Files.FileOperation/ComDisposer.cs"/>
  48.     ''' </remarks>
  49.     ''' ----------------------------------------------------------------------------------------------------
  50.     ''' <example> This is a code example that demonstrates how to adapt a C# code that uses pointers to VB.NET language.
  51.     ''' <code language="VB.NET">
  52.     ''' Dim shellItem As IShellItem = Nothing
  53.     ''' NativeMethods.SHCreateItemFromParsingName("C:\", IntPtr.Zero, GetType(IShellItem).GUID, shellItem)
  54.     '''
  55.     ''' Using comObjectDisposer As New ComObjectDisposer(Of IShellItem)(shellItem)
  56.     '''     Console.WriteLine(comObjectDisposer.Value.GetDisplayName(ShellItemGetDisplayName.NormalDisplay).ToString())
  57.     ''' End Using
  58.     ''' </code>
  59.     ''' </example>
  60.     ''' ----------------------------------------------------------------------------------------------------
  61.     Public Class ComObjectDisposer(Of T As Class) : Implements IDisposable
  62.  
  63. #Region " Properties "
  64.  
  65.         ''' ----------------------------------------------------------------------------------------------------
  66.         ''' <summary>
  67.         ''' Gets the wrapped COM object.
  68.         ''' </summary>
  69.         ''' ----------------------------------------------------------------------------------------------------
  70.         ''' <value>
  71.         ''' The wrapped COM object.
  72.         ''' </value>
  73.         ''' ----------------------------------------------------------------------------------------------------
  74.         Public Property Value As T
  75.             Get
  76.                 Return Me.privateValue
  77.             End Get
  78.             Private Set(value As T)
  79.                 Me.privateValue = value
  80.             End Set
  81.         End Property
  82.         ''' <summary>
  83.         ''' The wrapped COM object.
  84.         ''' </summary>
  85.         Private privateValue As T
  86.  
  87. #End Region
  88.  
  89. #Region " Constructors "
  90.  
  91.         ''' ----------------------------------------------------------------------------------------------------
  92.         ''' <summary>
  93.         ''' Prevents a default instance of the <see cref="ComObjectDisposer(Of T)"/> class from being created.
  94.         ''' </summary>
  95.         ''' ----------------------------------------------------------------------------------------------------
  96.         Private Sub New()
  97.         End Sub
  98.  
  99.         ''' ----------------------------------------------------------------------------------------------------
  100.         ''' <summary>
  101.         ''' Initializes a new instance of the <see cref="ComObjectDisposer(Of T)"/> class.
  102.         ''' </summary>
  103.         ''' ----------------------------------------------------------------------------------------------------
  104.         ''' <param name="obj">
  105.         ''' The COM object to wrap.
  106.         ''' </param>
  107.         ''' ----------------------------------------------------------------------------------------------------
  108.         ''' <exception cref="ArgumentException">
  109.         ''' obj - Object must be a COM object.
  110.         ''' </exception>
  111.         ''' ----------------------------------------------------------------------------------------------------
  112.         Public Sub New(obj As T)
  113.  
  114.             If obj IsNot Nothing AndAlso Not obj.GetType().IsCOMObject Then
  115.                 Throw New ArgumentException(paramName:=NameOf(obj), message:="Object must be a COM object.")
  116.             End If
  117.  
  118.             Me.privateValue = obj
  119.  
  120.         End Sub
  121.  
  122. #End Region
  123.  
  124. #Region " IDisposable Implementation "
  125.  
  126.         ''' ----------------------------------------------------------------------------------------------------
  127.         ''' <summary>
  128.         ''' Flag to detect redundant calls when disposing.
  129.         ''' </summary>
  130.         ''' ----------------------------------------------------------------------------------------------------
  131.         Private isDisposed As Boolean = False
  132.  
  133.         ''' ----------------------------------------------------------------------------------------------------
  134.         ''' <summary>
  135.         ''' Releases all the resources used by this instance.
  136.         ''' </summary>
  137.         ''' ----------------------------------------------------------------------------------------------------
  138.         <DebuggerStepThrough>
  139.         Public Sub Dispose() Implements IDisposable.Dispose
  140.             Me.Dispose(isDisposing:=True)
  141.         End Sub
  142.  
  143.         ''' ----------------------------------------------------------------------------------------------------
  144.         ''' <summary>
  145.         ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  146.         ''' Releases unmanaged and, optionally, managed resources.
  147.         ''' </summary>
  148.         ''' ----------------------------------------------------------------------------------------------------
  149.         ''' <param name="isDisposing">
  150.         ''' <see langword="True"/>  to release both managed and unmanaged resources;
  151.         ''' <see langword="False"/> to release only unmanaged resources.
  152.         ''' </param>
  153.         ''' ----------------------------------------------------------------------------------------------------
  154.         <DebuggerStepThrough>
  155.         Protected Overridable Sub Dispose(isDisposing As Boolean)
  156.  
  157.             If (Not Me.isDisposed) AndAlso isDisposing Then
  158.  
  159.                 If Me.Value IsNot Nothing Then
  160.                     Marshal.FinalReleaseComObject(Value)
  161.                     Me.Value = Nothing
  162.                 End If
  163.  
  164.             End If
  165.  
  166.             Me.isDisposed = True
  167.  
  168.         End Sub
  169.  
  170. #End Region
  171.  
  172.     End Class
  173.  
  174. End Namespace
  175.  
  176. #End Region
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement