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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// 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.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading;
/// <summary>
/// [MSI 4.5] Handle to a multi-session install transaction.
/// </summary>
/// <remarks><p>
/// Win32 MSI APIs:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msibegintransaction.asp">MsiBeginTransaction</a>
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msijointransaction.asp">MsiJoinTransaction</a>
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiendtransaction.asp">MsiEndTransaction</a>
/// </p></remarks>
public class Transaction : InstallerHandle
{
private string name;
private IntPtr ownerChangeEvent;
private IList<EventHandler<EventArgs>> ownerChangeListeners;
/// <summary>
/// [MSI 4.5] Begins transaction processing of a multi-package installation.
/// </summary>
/// <param name="name">Name of the multi-package installation.</param>
/// <param name="attributes">Select optional behavior when beginning the transaction.</param>
/// <exception cref="InstallerException">The transaction could not be initialized.</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msibegintransaction.asp">MsiBeginTransaction</a>
/// </p></remarks>
public Transaction(string name, TransactionAttributes attributes)
: this(name, Transaction.Begin(name, attributes), true)
{
}
/// <summary>
/// Internal constructor.
/// </summary>
/// <remarks>
/// The second parameter is an array in order to receive multiple values from the initialization method.
/// </remarks>
private Transaction(string name, IntPtr[] handles, bool ownsHandle)
: base(handles[0], ownsHandle)
{
this.name = name;
this.ownerChangeEvent = handles[1];
this.ownerChangeListeners = new List<EventHandler<EventArgs>>();
}
/// <summary>
/// Creates a new Transaction object from an integer handle.
/// </summary>
/// <param name="handle">Integer transaction handle</param>
/// <param name="ownsHandle">true to close the handle when this object is disposed</param>
public static Transaction FromHandle(IntPtr handle, bool ownsHandle)
{
return new Transaction(handle.ToString(), new IntPtr[] { handle, IntPtr.Zero }, ownsHandle);
}
/// <summary>
/// Gets the name of the transaction.
/// </summary>
public string Name
{
get
{
return name;
}
}
/// <summary>
/// Notifies listeners when the process that owns the transaction changed.
/// </summary>
public event EventHandler<EventArgs> OwnerChanged
{
add
{
this.ownerChangeListeners.Add(value);
if (this.ownerChangeEvent != IntPtr.Zero && this.ownerChangeListeners.Count == 1)
{
new Thread(this.WaitForOwnerChange).Start();
}
}
remove
{
this.ownerChangeListeners.Remove(value);
}
}
private void OnOwnerChanged()
{
EventArgs e = new EventArgs();
foreach (EventHandler<EventArgs> handler in this.ownerChangeListeners)
{
handler(this, e);
}
}
private void WaitForOwnerChange()
{
int ret = NativeMethods.WaitForSingleObject(this.ownerChangeEvent, -1);
if (ret == 0)
{
this.OnOwnerChanged();
}
else
{
throw new InstallerException();
}
}
/// <summary>
/// Makes the current process the owner of the multi-package installation transaction.
/// </summary>
/// <param name="attributes">Select optional behavior when joining the transaction.</param>
/// <exception cref="InvalidHandleException">The transaction handle is not valid.</exception>
/// <exception cref="InstallerException">The transaction could not be joined.</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msijointransaction.asp">MsiJoinTransaction</a>
/// </p></remarks>
public void Join(TransactionAttributes attributes)
{
IntPtr hChangeOfOwnerEvent;
uint ret = NativeMethods.MsiJoinTransaction((int) this.Handle, (int) attributes, out hChangeOfOwnerEvent);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
this.ownerChangeEvent = hChangeOfOwnerEvent;
if (this.ownerChangeEvent != IntPtr.Zero && this.ownerChangeListeners.Count >= 1)
{
new Thread(this.WaitForOwnerChange).Start();
}
}
/// <summary>
/// Ends the install transaction and commits all changes to the system belonging to the transaction.
/// </summary>
/// <exception cref="InstallerException">The transaction could not be committed.</exception>
/// <remarks><p>
/// Runs any Commit Custom Actions and commits to the system any changes to Win32 or common language
/// runtime assemblies. Deletes the rollback script, and after using this option, the transaction's
/// changes can no longer be undone with a Rollback Installation.
/// </p><p>
/// This method can only be called by the current owner of the transaction.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiendtransaction.asp">MsiEndTransaction</a>
/// </p></remarks>
public void Commit()
{
this.End(true);
}
/// <summary>
/// Ends the install transaction and undoes changes to the system belonging to the transaction.
/// </summary>
/// <exception cref="InstallerException">The transaction could not be rolled back.</exception>
/// <remarks><p>
/// This method can only be called by the current owner of the transaction.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiendtransaction.asp">MsiEndTransaction</a>
/// </p></remarks>
public void Rollback()
{
this.End(false);
}
private static IntPtr[] Begin(string transactionName, TransactionAttributes attributes)
{
int hTransaction;
IntPtr hChangeOfOwnerEvent;
uint ret = NativeMethods.MsiBeginTransaction(transactionName, (int) attributes, out hTransaction, out hChangeOfOwnerEvent);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
return new IntPtr[] { (IntPtr) hTransaction, hChangeOfOwnerEvent };
}
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
private void End(bool commit)
{
uint ret = NativeMethods.MsiEndTransaction(commit ? 1 : 0);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
}
}
|