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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// 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.Text;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Accessor for information about components within the context of an installation session.
/// </summary>
public sealed class ComponentInfoCollection : ICollection<ComponentInfo>
{
private Session session;
internal ComponentInfoCollection(Session session)
{
this.session = session;
}
/// <summary>
/// Gets information about a component within the context of an installation session.
/// </summary>
/// <param name="component">name of the component</param>
/// <returns>component object</returns>
public ComponentInfo this[string component]
{
get
{
return new ComponentInfo(this.session, component);
}
}
void ICollection<ComponentInfo>.Add(ComponentInfo item)
{
throw new InvalidOperationException();
}
void ICollection<ComponentInfo>.Clear()
{
throw new InvalidOperationException();
}
/// <summary>
/// Checks if the collection contains a component.
/// </summary>
/// <param name="component">name of the component</param>
/// <returns>true if the component is in the collection, else false</returns>
public bool Contains(string component)
{
return this.session.Database.CountRows(
"Component", "`Component` = '" + component + "'") == 1;
}
bool ICollection<ComponentInfo>.Contains(ComponentInfo item)
{
return item != null && this.Contains(item.Name);
}
/// <summary>
/// Copies the features into an array.
/// </summary>
/// <param name="array">array that receives the features</param>
/// <param name="arrayIndex">offset into the array</param>
public void CopyTo(ComponentInfo[] array, int arrayIndex)
{
foreach (ComponentInfo component in this)
{
array[arrayIndex++] = component;
}
}
/// <summary>
/// Gets the number of components defined for the product.
/// </summary>
public int Count
{
get
{
return this.session.Database.CountRows("Component");
}
}
bool ICollection<ComponentInfo>.IsReadOnly
{
get
{
return true;
}
}
bool ICollection<ComponentInfo>.Remove(ComponentInfo item)
{
throw new InvalidOperationException();
}
/// <summary>
/// Enumerates the components in the collection.
/// </summary>
/// <returns>an enumerator over all features in the collection</returns>
public IEnumerator<ComponentInfo> GetEnumerator()
{
using (View compView = this.session.Database.OpenView(
"SELECT `Component` FROM `Component`"))
{
compView.Execute();
foreach (Record compRec in compView) using (compRec)
{
string comp = compRec.GetString(1);
yield return new ComponentInfo(this.session, comp);
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
/// <summary>
/// Provides access to information about a component within the context of an installation session.
/// </summary>
public class ComponentInfo
{
private Session session;
private string name;
internal ComponentInfo(Session session, string name)
{
this.session = session;
this.name = name;
}
/// <summary>
/// Gets the name of the component (primary key in the Component table).
/// </summary>
public string Name
{
get
{
return this.name;
}
}
/// <summary>
/// Gets the current install state of the designated Component.
/// </summary>
/// <exception cref="InvalidHandleException">the Session handle is invalid</exception>
/// <exception cref="ArgumentException">an unknown Component was requested</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msigetcomponentstate.asp">MsiGetComponentState</a>
/// </p></remarks>
public InstallState CurrentState
{
get
{
int installedState, actionState;
uint ret = RemotableNativeMethods.MsiGetComponentState((int) this.session.Handle, this.name, out installedState, out actionState);
if (ret != 0)
{
if (ret == (uint) NativeMethods.Error.UNKNOWN_COMPONENT)
{
throw InstallerException.ExceptionFromReturnCode(ret, this.name);
}
else
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
return (InstallState) installedState;
}
}
/// <summary>
/// Gets or sets the action state of the designated Component.
/// </summary>
/// <exception cref="InvalidHandleException">the Session handle is invalid</exception>
/// <exception cref="ArgumentException">an unknown Component was requested</exception>
/// <exception cref="InstallCanceledException">the user exited the installation</exception>
/// <remarks><p>
/// Win32 MSI APIs:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msigetcomponentstate.asp">MsiGetComponentState</a>,
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msisetcomponentstate.asp">MsiSetComponentState</a>
/// </p></remarks>
public InstallState RequestState
{
get
{
int installedState, actionState;
uint ret = RemotableNativeMethods.MsiGetComponentState((int) this.session.Handle, this.name, out installedState, out actionState);
if (ret != 0)
{
if (ret == (uint) NativeMethods.Error.UNKNOWN_COMPONENT)
{
throw InstallerException.ExceptionFromReturnCode(ret, this.name);
}
else
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
return (InstallState) actionState;
}
set
{
uint ret = RemotableNativeMethods.MsiSetComponentState((int) this.session.Handle, this.name, (int) value);
if (ret != 0)
{
if (ret == (uint) NativeMethods.Error.UNKNOWN_COMPONENT)
{
throw InstallerException.ExceptionFromReturnCode(ret, this.name);
}
else
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
}
}
/// <summary>
/// Gets disk space per drive required to install a component.
/// </summary>
/// <param name="installState">Requested component state</param>
/// <returns>A list of InstallCost structures, specifying the cost for each drive for the component</returns>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msienumcomponentcosts.asp">MsiEnumComponentCosts</a>
/// </p></remarks>
public IList<InstallCost> GetCost(InstallState installState)
{
IList<InstallCost> costs = new List<InstallCost>();
StringBuilder driveBuf = new StringBuilder(20);
for (uint i = 0; true; i++)
{
int cost, tempCost;
uint driveBufSize = (uint) driveBuf.Capacity;
uint ret = RemotableNativeMethods.MsiEnumComponentCosts(
(int) this.session.Handle,
this.name,
i,
(int) installState,
driveBuf,
ref driveBufSize,
out cost,
out tempCost);
if (ret == (uint) NativeMethods.Error.NO_MORE_ITEMS) break;
if (ret == (uint) NativeMethods.Error.MORE_DATA)
{
driveBuf.Capacity = (int) ++driveBufSize;
ret = RemotableNativeMethods.MsiEnumComponentCosts(
(int) this.session.Handle,
this.name,
i,
(int) installState,
driveBuf,
ref driveBufSize,
out cost,
out tempCost);
}
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
costs.Add(new InstallCost(driveBuf.ToString(), cost * 512L, tempCost * 512L));
}
return costs;
}
}
}
|