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
|
// 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.Generic;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Represents an instance of a feature of an installed product.
/// </summary>
public class FeatureInstallation : InstallationPart
{
/// <summary>
/// Creates a new FeatureInstallation instance for a feature of a product.
/// </summary>
/// <param name="featureName">feature name</param>
/// <param name="productCode">ProductCode GUID</param>
public FeatureInstallation(string featureName, string productCode)
: base(featureName, productCode)
{
if (String.IsNullOrEmpty(featureName))
{
throw new ArgumentNullException("featureName");
}
}
/// <summary>
/// Gets the name of the feature.
/// </summary>
public string FeatureName
{
get
{
return this.Id;
}
}
/// <summary>
/// Gets the installed state of the feature.
/// </summary>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiqueryfeaturestate.asp">MsiQueryFeatureState</a>
/// </p></remarks>
public override InstallState State
{
get
{
int installState = NativeMethods.MsiQueryFeatureState(
this.ProductCode, this.FeatureName);
return (InstallState) installState;
}
}
/// <summary>
/// Gets the parent of the feature, or null if the feature has no parent (it is a root feature).
/// </summary>
/// <remarks>
/// Invocation of this property may be slightly costly for products with many features,
/// because it involves an enumeration of all the features in the product.
/// </remarks>
public FeatureInstallation Parent
{
get
{
StringBuilder featureBuf = new StringBuilder(256);
StringBuilder parentBuf = new StringBuilder(256);
for (uint i = 0; ; i++)
{
uint ret = NativeMethods.MsiEnumFeatures(this.ProductCode, i, featureBuf, parentBuf);
if (ret != 0)
{
break;
}
if (featureBuf.ToString() == this.FeatureName)
{
if (parentBuf.Length > 0)
{
return new FeatureInstallation(parentBuf.ToString(), this.ProductCode);
}
else
{
return null;
}
}
}
return null;
}
}
/// <summary>
/// Gets the usage metrics for the feature.
/// </summary>
/// <remarks><p>
/// If no usage metrics are recorded, the <see cref="UsageData.UseCount" /> value is 0.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msigetfeatureusage.asp">MsiGetFeatureUsage</a>
/// </p></remarks>
public FeatureInstallation.UsageData Usage
{
get
{
uint useCount;
ushort useDate;
uint ret = NativeMethods.MsiGetFeatureUsage(
this.ProductCode, this.FeatureName, out useCount, out useDate);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
DateTime lastUsedDate;
if (useCount == 0)
{
lastUsedDate = DateTime.MinValue;
}
else
{
lastUsedDate = new DateTime(
1980 + (useDate >> 9),
(useDate & 0x01FF) >> 5,
(useDate & 0x001F));
}
return new UsageData((int) useCount, lastUsedDate);
}
}
/// <summary>
/// Holds data about the usage of a feature.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
[SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
public struct UsageData
{
private int useCount;
private DateTime lastUsedDate;
internal UsageData(int useCount, DateTime lastUsedDate)
{
this.useCount = useCount;
this.lastUsedDate = lastUsedDate;
}
/// <summary>
/// Gets count of the number of times the feature has been used.
/// </summary>
public int UseCount
{
get
{
return this.useCount;
}
}
/// <summary>
/// Gets the date the feature was last used.
/// </summary>
public DateTime LastUsedDate
{
get
{
return this.lastUsedDate;
}
}
}
}
}
|