aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Mba.Core/Engine.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Mba.Core/Engine.cs')
-rw-r--r--src/WixToolset.Mba.Core/Engine.cs290
1 files changed, 99 insertions, 191 deletions
diff --git a/src/WixToolset.Mba.Core/Engine.cs b/src/WixToolset.Mba.Core/Engine.cs
index 2c544f29..98427cfa 100644
--- a/src/WixToolset.Mba.Core/Engine.cs
+++ b/src/WixToolset.Mba.Core/Engine.cs
@@ -18,10 +18,6 @@ namespace WixToolset.Mba.Core
18 private static readonly string normalizeVersionFormatString = "{0} must be less than or equal to " + UInt16.MaxValue; 18 private static readonly string normalizeVersionFormatString = "{0} must be less than or equal to " + UInt16.MaxValue;
19 19
20 private IBootstrapperEngine engine; 20 private IBootstrapperEngine engine;
21 private Variables<long> numericVariables;
22 private Variables<SecureString> secureStringVariables;
23 private Variables<string> stringVariables;
24 private Variables<Version> versionVariables;
25 21
26 /// <summary> 22 /// <summary>
27 /// Creates a new instance of the <see cref="Engine"/> container class. 23 /// Creates a new instance of the <see cref="Engine"/> container class.
@@ -30,135 +26,6 @@ namespace WixToolset.Mba.Core
30 internal Engine(IBootstrapperEngine engine) 26 internal Engine(IBootstrapperEngine engine)
31 { 27 {
32 this.engine = engine; 28 this.engine = engine;
33
34 // Wrap the calls to get and set numeric variables.
35 this.numericVariables = new Variables<long>(
36 delegate(string name)
37 {
38 long value;
39 int ret = this.engine.GetVariableNumeric(name, out value);
40 if (NativeMethods.S_OK != ret)
41 {
42 throw new Win32Exception(ret);
43 }
44
45 return value;
46 },
47 delegate(string name, long value)
48 {
49 this.engine.SetVariableNumeric(name, value);
50 },
51 delegate(string name)
52 {
53 long value;
54 int ret = this.engine.GetVariableNumeric(name, out value);
55
56 return NativeMethods.E_NOTFOUND != ret;
57 }
58 );
59
60 // Wrap the calls to get and set string variables using SecureStrings.
61 this.secureStringVariables = new Variables<SecureString>(
62 delegate(string name)
63 {
64 var pUniString = this.getStringVariable(name, out var length);
65 try
66 {
67 return this.convertToSecureString(pUniString, length);
68 }
69 finally
70 {
71 if (IntPtr.Zero != pUniString)
72 {
73 Marshal.FreeCoTaskMem(pUniString);
74 }
75 }
76 },
77 delegate(string name, SecureString value)
78 {
79 IntPtr pValue = Marshal.SecureStringToCoTaskMemUnicode(value);
80 try
81 {
82 this.engine.SetVariableString(name, pValue, true);
83 }
84 finally
85 {
86 Marshal.FreeCoTaskMem(pValue);
87 }
88 },
89 delegate(string name)
90 {
91 return this.containsVariable(name);
92 }
93 );
94
95 // Wrap the calls to get and set string variables.
96 this.stringVariables = new Variables<string>(
97 delegate(string name)
98 {
99 int length;
100 IntPtr pUniString = this.getStringVariable(name, out length);
101 try
102 {
103 return Marshal.PtrToStringUni(pUniString, length);
104 }
105 finally
106 {
107 if (IntPtr.Zero != pUniString)
108 {
109 Marshal.FreeCoTaskMem(pUniString);
110 }
111 }
112 },
113 delegate(string name, string value)
114 {
115 IntPtr pValue = Marshal.StringToCoTaskMemUni(value);
116 try
117 {
118 this.engine.SetVariableString(name, pValue, true);
119 }
120 finally
121 {
122 Marshal.FreeCoTaskMem(pValue);
123 }
124 },
125 delegate(string name)
126 {
127 return this.containsVariable(name);
128 }
129 );
130
131 // Wrap the calls to get and set version variables.
132 this.versionVariables = new Variables<Version>(
133 delegate(string name)
134 {
135 long value;
136 int ret = this.engine.GetVariableVersion(name, out value);
137 if (NativeMethods.S_OK != ret)
138 {
139 throw new Win32Exception(ret);
140 }
141
142 return LongToVersion(value);
143 },
144 delegate(string name, Version value)
145 {
146 long version = VersionToLong(value);
147 this.engine.SetVariableVersion(name, version);
148 },
149 delegate(string name)
150 {
151 long value;
152 int ret = this.engine.GetVariableVersion(name, out value);
153
154 return NativeMethods.E_NOTFOUND != ret;
155 }
156 );
157 }
158
159 public IVariables<long> NumericVariables
160 {
161 get { return this.numericVariables; }
162 } 29 }
163 30
164 public int PackageCount 31 public int PackageCount
@@ -172,21 +39,6 @@ namespace WixToolset.Mba.Core
172 } 39 }
173 } 40 }
174 41
175 public IVariables<SecureString> SecureStringVariables
176 {
177 get { return this.secureStringVariables; }
178 }
179
180 public IVariables<string> StringVariables
181 {
182 get { return this.stringVariables; }
183 }
184
185 public IVariables<Version> VersionVariables
186 {
187 get { return this.versionVariables; }
188 }
189
190 public void Apply(IntPtr hwndParent) 42 public void Apply(IntPtr hwndParent)
191 { 43 {
192 this.engine.Apply(hwndParent); 44 this.engine.Apply(hwndParent);
@@ -197,6 +49,13 @@ namespace WixToolset.Mba.Core
197 this.engine.CloseSplashScreen(); 49 this.engine.CloseSplashScreen();
198 } 50 }
199 51
52 public bool ContainsVariable(string name)
53 {
54 int capacity = 0;
55 int ret = this.engine.GetVariableString(name, IntPtr.Zero, ref capacity);
56 return NativeMethods.E_NOTFOUND != ret;
57 }
58
200 public void Detect() 59 public void Detect()
201 { 60 {
202 this.Detect(IntPtr.Zero); 61 this.Detect(IntPtr.Zero);
@@ -275,6 +134,61 @@ namespace WixToolset.Mba.Core
275 return sb.ToString(); 134 return sb.ToString();
276 } 135 }
277 136
137 public long GetVariableNumeric(string name)
138 {
139 int ret = this.engine.GetVariableNumeric(name, out long value);
140 if (NativeMethods.S_OK != ret)
141 {
142 throw new Win32Exception(ret);
143 }
144
145 return value;
146 }
147
148 public SecureString GetVariableSecureString(string name)
149 {
150 var pUniString = this.getStringVariable(name, out var length);
151 try
152 {
153 return this.convertToSecureString(pUniString, length);
154 }
155 finally
156 {
157 if (IntPtr.Zero != pUniString)
158 {
159 Marshal.FreeCoTaskMem(pUniString);
160 }
161 }
162 }
163
164 public string GetVariableString(string name)
165 {
166 int length;
167 IntPtr pUniString = this.getStringVariable(name, out length);
168 try
169 {
170 return Marshal.PtrToStringUni(pUniString, length);
171 }
172 finally
173 {
174 if (IntPtr.Zero != pUniString)
175 {
176 Marshal.FreeCoTaskMem(pUniString);
177 }
178 }
179 }
180
181 public Version GetVariableVersion(string name)
182 {
183 int ret = this.engine.GetVariableVersion(name, out long value);
184 if (NativeMethods.S_OK != ret)
185 {
186 throw new Win32Exception(ret);
187 }
188
189 return LongToVersion(value);
190 }
191
278 public void LaunchApprovedExe(IntPtr hwndParent, string approvedExeForElevationId, string arguments) 192 public void LaunchApprovedExe(IntPtr hwndParent, string approvedExeForElevationId, string arguments)
279 { 193 {
280 this.LaunchApprovedExe(hwndParent, approvedExeForElevationId, arguments, 0); 194 this.LaunchApprovedExe(hwndParent, approvedExeForElevationId, arguments, 0);
@@ -310,6 +224,43 @@ namespace WixToolset.Mba.Core
310 this.engine.SetDownloadSource(packageOrContainerId, payloadId, url, user, password); 224 this.engine.SetDownloadSource(packageOrContainerId, payloadId, url, user, password);
311 } 225 }
312 226
227 public void SetVariable(string name, long value)
228 {
229 this.engine.SetVariableNumeric(name, value);
230 }
231
232 public void SetVariable(string name, SecureString value, bool formatted)
233 {
234 IntPtr pValue = Marshal.SecureStringToCoTaskMemUnicode(value);
235 try
236 {
237 this.engine.SetVariableString(name, pValue, formatted);
238 }
239 finally
240 {
241 Marshal.FreeCoTaskMem(pValue);
242 }
243 }
244
245 public void SetVariable(string name, string value, bool formatted)
246 {
247 IntPtr pValue = Marshal.StringToCoTaskMemUni(value);
248 try
249 {
250 this.engine.SetVariableString(name, pValue, formatted);
251 }
252 finally
253 {
254 Marshal.FreeCoTaskMem(pValue);
255 }
256 }
257
258 public void SetVariable(string name, Version value)
259 {
260 long version = VersionToLong(value);
261 this.engine.SetVariableVersion(name, version);
262 }
263
313 public int SendEmbeddedError(int errorCode, string message, int uiHint) 264 public int SendEmbeddedError(int errorCode, string message, int uiHint)
314 { 265 {
315 int result = 0; 266 int result = 0;
@@ -329,49 +280,6 @@ namespace WixToolset.Mba.Core
329 this.engine.Quit(exitCode); 280 this.engine.Quit(exitCode);
330 } 281 }
331 282
332 internal sealed class Variables<T> : IVariables<T>
333 {
334 // .NET 2.0 does not support Func<T, TResult> or Action<T1, T2>.
335 internal delegate T Getter(string name);
336 internal delegate void Setter(string name, T value);
337
338 private Getter getter;
339 private Setter setter;
340 private Predicate<string> contains;
341
342 internal Variables(Getter getter, Setter setter, Predicate<string> contains)
343 {
344 this.getter = getter;
345 this.setter = setter;
346 this.contains = contains;
347 }
348
349 public T this[string name]
350 {
351 get { return this.getter(name); }
352 set { this.setter(name, value); }
353 }
354
355 public bool Contains(string name)
356 {
357 return this.contains(name);
358 }
359 }
360
361 /// <summary>
362 /// Gets whether the variable given by <paramref name="name"/> exists.
363 /// </summary>
364 /// <param name="name">The name of the variable to check.</param>
365 /// <returns>True if the variable given by <paramref name="name"/> exists; otherwise, false.</returns>
366 internal bool containsVariable(string name)
367 {
368 int capacity = 0;
369 IntPtr pValue = IntPtr.Zero;
370 int ret = this.engine.GetVariableString(name, pValue, ref capacity);
371
372 return NativeMethods.E_NOTFOUND != ret;
373 }
374
375 /// <summary> 283 /// <summary>
376 /// Gets the variable given by <paramref name="name"/> as a string. 284 /// Gets the variable given by <paramref name="name"/> as a string.
377 /// </summary> 285 /// </summary>