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
|
// 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.Harvesters
{
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using WixToolset.Data;
using WixToolset.Harvesters.Data;
using WixToolset.Harvesters.Extensibility;
using Wix = WixToolset.Harvesters.Serialize;
/// <summary>
/// The WiX Toolset harvester mutator.
/// </summary>
public sealed class UtilHarvesterMutator : BaseMutatorExtension
{
// Flags for SetErrorMode() native method.
private const UInt32 SEM_FAILCRITICALERRORS = 0x0001;
private const UInt32 SEM_NOGPFAULTERRORBOX = 0x0002;
private const UInt32 SEM_NOALIGNMENTFAULTEXCEPT = 0x0004;
private const UInt32 SEM_NOOPENFILEERRORBOX = 0x8000;
// Remember whether we were able to call OaEnablePerUserTLibRegistration
private bool calledPerUserTLibReg;
/// <summary>
/// allow process to handle serious system errors.
/// </summary>
[DllImport("Kernel32.dll")]
private static extern void SetErrorMode(UInt32 uiMode);
/// <summary>
/// enable the RegisterTypeLib API to use the appropriate override mapping for non-admin users on Vista
/// </summary>
[DllImport("Oleaut32.dll")]
private static extern void OaEnablePerUserTLibRegistration();
public UtilHarvesterMutator()
{
this.calledPerUserTLibReg = false;
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
try
{
OaEnablePerUserTLibRegistration();
this.calledPerUserTLibReg = true;
}
catch (EntryPointNotFoundException)
{
}
}
/// <summary>
/// Gets the sequence of this mutator extension.
/// </summary>
/// <value>The sequence of this mutator extension.</value>
public override int Sequence
{
get { return 100; }
}
/// <summary>
/// Mutate a WiX document.
/// </summary>
/// <param name="wix">The Wix document element.</param>
public override void Mutate(Wix.Wix wix)
{
this.MutateElement(null, wix);
}
/// <summary>
/// Mutate an element.
/// </summary>
/// <param name="parentElement">The parent of the element to mutate.</param>
/// <param name="element">The element to mutate.</param>
private void MutateElement(Wix.IParentElement parentElement, Wix.ISchemaElement element)
{
if (element is Wix.File)
{
this.MutateFile(parentElement, (Wix.File)element);
}
// mutate the child elements
if (element is Wix.IParentElement)
{
ArrayList childElements = new ArrayList();
// copy the child elements to a temporary array (to allow them to be deleted/moved)
foreach (Wix.ISchemaElement childElement in ((Wix.IParentElement)element).Children)
{
childElements.Add(childElement);
}
foreach (Wix.ISchemaElement childElement in childElements)
{
this.MutateElement((Wix.IParentElement)element, childElement);
}
}
}
/// <summary>
/// Mutate a file.
/// </summary>
/// <param name="parentElement">The parent of the element to mutate.</param>
/// <param name="file">The file to mutate.</param>
private void MutateFile(Wix.IParentElement parentElement, Wix.File file)
{
if (null != file.Source)
{
string fileExtension = Path.GetExtension(file.Source);
string fileSource = this.Core.ResolveFilePath(file.Source);
if (String.Equals(".ax", fileExtension, StringComparison.OrdinalIgnoreCase) || // DirectShow filter
String.Equals(".dll", fileExtension, StringComparison.OrdinalIgnoreCase) ||
String.Equals(".exe", fileExtension, StringComparison.OrdinalIgnoreCase) ||
String.Equals(".ocx", fileExtension, StringComparison.OrdinalIgnoreCase)) // ActiveX
{
// try the assembly harvester
try
{
AssemblyHarvester assemblyHarvester = new AssemblyHarvester();
this.Core.Messaging.Write(HarvesterVerboses.HarvestingAssembly(fileSource));
Wix.RegistryValue[] registryValues = assemblyHarvester.HarvestRegistryValues(fileSource);
foreach (Wix.RegistryValue registryValue in registryValues)
{
parentElement.AddChild(registryValue);
}
// also try self-reg since we could have a mixed-mode assembly
this.HarvestSelfReg(parentElement, fileSource);
}
catch (BadImageFormatException) // not an assembly, try raw DLL.
{
this.HarvestSelfReg(parentElement, fileSource);
}
catch (Exception ex)
{
this.Core.Messaging.Write(HarvesterWarnings.AssemblyHarvestFailed(fileSource, ex.Message));
}
}
else if (String.Equals(".olb", fileExtension, StringComparison.OrdinalIgnoreCase) || // type library
String.Equals(".tlb", fileExtension, StringComparison.OrdinalIgnoreCase)) // type library
{
// try the type library harvester
try
{
TypeLibraryHarvester typeLibHarvester = new TypeLibraryHarvester();
this.Core.Messaging.Write(HarvesterVerboses.HarvestingTypeLib(fileSource));
Wix.RegistryValue[] registryValues = typeLibHarvester.HarvestRegistryValues(fileSource);
foreach (Wix.RegistryValue registryValue in registryValues)
{
parentElement.AddChild(registryValue);
}
}
catch (COMException ce)
{
// 0x8002801C (TYPE_E_REGISTRYACCESS)
// If we don't have permission to harvest typelibs, it's likely because we're on
// Vista or higher and aren't an Admin, or don't have the appropriate QFE installed.
if (!this.calledPerUserTLibReg && (0x8002801c == unchecked((uint)ce.ErrorCode)))
{
this.Core.Messaging.Write(WarningMessages.InsufficientPermissionHarvestTypeLib());
}
else if (0x80029C4A == unchecked((uint)ce.ErrorCode)) // generic can't load type library
{
this.Core.Messaging.Write(HarvesterWarnings.TypeLibLoadFailed(fileSource, ce.Message));
}
}
}
}
}
/// <summary>
/// Calls self-reg harvester.
/// </summary>
/// <param name="parentElement">The parent element.</param>
/// <param name="fileSource">The file source.</param>
private void HarvestSelfReg(Wix.IParentElement parentElement, string fileSource)
{
// try the self-reg harvester
try
{
DllHarvester dllHarvester = new DllHarvester();
this.Core.Messaging.Write(HarvesterVerboses.HarvestingSelfReg(fileSource));
Wix.RegistryValue[] registryValues = dllHarvester.HarvestRegistryValues(fileSource);
foreach (Wix.RegistryValue registryValue in registryValues)
{
parentElement.AddChild(registryValue);
}
}
catch (TargetInvocationException tie)
{
if (tie.InnerException is EntryPointNotFoundException)
{
// No DllRegisterServer(), which is fine by me.
}
else
{
this.Core.Messaging.Write(HarvesterWarnings.SelfRegHarvestFailed(fileSource, tie.Message));
}
}
catch (Exception ex)
{
this.Core.Messaging.Write(HarvesterWarnings.SelfRegHarvestFailed(fileSource, ex.Message));
}
}
}
}
|