1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Dtf.WindowsInstaller
{
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Base class for Windows Installer handle types (Database, View, Record, SummaryInfo).
/// </summary>
/// <remarks><p>
/// These classes implement the <see cref="IDisposable"/> interface, because they
/// hold unmanaged resources (MSI handles) that should be properly disposed
/// when no longer needed.
/// </p></remarks>
public abstract class InstallerHandle : MarshalByRefObject, IDisposable
{
private NativeMethods.MsiHandle handle;
/// <summary>
/// Constructs a handle object from a native integer handle.
/// </summary>
/// <param name="handle">Native integer handle.</param>
/// <param name="ownsHandle">true to close the handle when this object is disposed or finalized</param>
protected InstallerHandle(IntPtr handle, bool ownsHandle)
{
if (handle == IntPtr.Zero)
{
throw new InvalidHandleException();
}
this.handle = new NativeMethods.MsiHandle(handle, ownsHandle);
}
/// <summary>
/// Gets the native integer handle.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
public IntPtr Handle
{
get
{
if (this.IsClosed)
{
throw new InvalidHandleException();
}
return this.handle;
}
}
/// <summary>
/// Checks if the handle is closed. When closed, method calls on the handle object may throw an <see cref="InvalidHandleException"/>.
/// </summary>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
public bool IsClosed
{
get
{
return this.handle.IsClosed;
}
}
/// <summary>
/// Closes the handle. After closing a handle, further method calls may throw an <see cref="InvalidHandleException"/>.
/// </summary>
/// <remarks><p>
/// The finalizer of this class will NOT close the handle if it is still open,
/// because finalization can run on a separate thread from the application,
/// resulting in potential problems if handles are closed from that thread.
/// It is best that the handle be closed manually as soon as it is no longer needed,
/// as leaving lots of unused handles open can degrade performance.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiclosehandle.asp">MsiCloseHandle</a>
/// </p></remarks>
/// <seealso cref="Close"/>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Closes the handle. After closing a handle, further method calls may throw an <see cref="InvalidHandleException"/>.
/// </summary>
/// <remarks><p>
/// The finalizer of this class will NOT close the handle if it is still open,
/// because finalization can run on a separate thread from the application,
/// resulting in potential problems if handles are closed from that thread.
/// It is best that the handle be closed manually as soon as it is no longer needed,
/// as leaving lots of unused handles open can degrade performance.
/// </p><p>
/// This method is merely an alias for the <see cref="Dispose()"/> method.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiclosehandle.asp">MsiCloseHandle</a>
/// </p></remarks>
public void Close()
{
this.Dispose();
}
/// <summary>
/// Tests whether this handle object is equal to another handle object. Two handle objects are equal
/// if their types are the same and their native integer handles are the same.
/// </summary>
/// <param name="obj">The handle object to compare with the current handle object.</param>
/// <returns>true if the specified handle object is equal to the current handle object; otherwise false</returns>
public override bool Equals(object obj)
{
return (obj != null && this.GetType() == obj.GetType() &&
this.Handle == ((InstallerHandle) obj).Handle);
}
/// <summary>
/// Gets a hash value for the handle object.
/// </summary>
/// <returns>A hash code for the handle object.</returns>
/// <remarks><p>
/// The hash code is derived from the native integer handle.
/// </p></remarks>
public override int GetHashCode()
{
return this.Handle.GetHashCode();
}
/// <summary>
/// Gets an object that can be used internally for safe syncronization.
/// </summary>
internal object Sync
{
get
{
return this.handle;
}
}
/// <summary>
/// Closes the handle. After closing a handle, further method calls may throw an <see cref="InvalidHandleException"/>.
/// </summary>
/// <param name="disposing">If true, the method has been called directly or indirectly by a user's code,
/// so managed and unmanaged resources will be disposed. If false, the method has been called by the
/// runtime from inside the finalizer, and only unmanaged resources will be disposed.</param>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this.handle.Dispose();
}
}
}
}
|