-
Notifications
You must be signed in to change notification settings - Fork 331
/
Bootstrap.cs
290 lines (262 loc) · 14.5 KB
/
Bootstrap.cs
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
// <auto-generated>
// Exclude this file from StyleCop analysis. This file isn't generated but is added to projects.
// DO NOT MODIFY. Changes to this file may cause incorrect behavior and will be lost on updates.
// </auto-generated>
using System.Runtime.InteropServices;
namespace Microsoft.Windows.ApplicationModel.DynamicDependency
{
// The version of an MSIX package. This is logically `Major.Minor.Build.Revision` and can be expressed as...
// * individual `ushort` values (uint16)
// * an unsigned `ulong` value (uint64)
// * a dot-string notation ("major.minor.build.revision")
[StructLayout(LayoutKind.Sequential)]
public struct PackageVersion
{
// NOTE: MUST match memory layout of PACKAGE_VERSION in appmodel.h
public ushort Revision;
public ushort Build;
public ushort Minor;
public ushort Major;
// Create an instance with the value `major.0.0.0`.
public PackageVersion(ushort major) :
this(major, 0, 0, 0)
{
}
// Create an instance with the value `major.minor.0.0`.
public PackageVersion(ushort major, ushort minor) :
this(major, minor, 0, 0)
{
}
// Create an instance with the value `major.minor.build.0`.
public PackageVersion(ushort major, ushort minor, ushort build) :
this(major, minor, build, 0)
{
}
// Create an instance with the value `major.minor.build.revision`.
public PackageVersion(ushort major, ushort minor, ushort build, ushort revision)
{
Major = major;
Minor = minor;
Build = build;
Revision = revision;
}
// Create an instance from a version as a uint64.
public PackageVersion(ulong version) :
this((ushort)(version >> 48), (ushort)(version >> 32), (ushort)(version >> 16), (ushort)version)
{
}
// Return the version as a uint64.
public ulong ToVersion()
{
return (((ulong)Major) << 48) | (((ulong)Minor) << 32) | (((ulong)Build) << 16) | ((ulong)Revision);
}
// Return the string as a formatted value "major.minor.build.revision".
public override string ToString()
{
return $"{Major}.{Minor}.{Build}.{Revision}";
}
};
internal static class NativeMethods
{
[DllImport("Microsoft.WindowsAppRuntime.Bootstrap.dll", EntryPoint = "MddBootstrapInitialize2", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = false)]
internal static extern void MddBootstrapInitialize2_Throw(uint majorMinorVersion, string versionTag, PackageVersion packageVersion, Bootstrap.InitializeOptions options);
[DllImport("Microsoft.WindowsAppRuntime.Bootstrap.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int MddBootstrapInitialize2(uint majorMinorVersion, string versionTag, PackageVersion packageVersion, Bootstrap.InitializeOptions options);
[DllImport("Microsoft.WindowsAppRuntime.Bootstrap.dll", ExactSpelling = true)]
internal static extern void MddBootstrapShutdown();
}
// The Windows App SDK bootstrap initialization API.
public class Bootstrap
{
/// Options for Bootstrap initialization APIs.
public enum InitializeOptions : int
{
/// Default behavior
None = 0,
/// If not successful call DebugBreak()
OnError_DebugBreak = 0x0001,
/// If not successful call DebugBreak() if a debugger is attached to the process
OnError_DebugBreak_IfDebuggerAttached = 0x0002,
/// If not successful perform a fail-fast
OnError_FailFast = 0x0004,
/// If a compatible Windows App Runtime framework package is not found show UI
OnNoMatch_ShowUI = 0x0008,
/// Do nothing (do not error) if the process has package identity
OnPackageIdentity_NOOP = 0x0010,
}
/// Initialize the calling process to use Windows App SDK's framework package.
///
/// Find a Windows App SDK framework package meeting the criteria and make it available
/// for use by the current process. If multiple packages meet the criteria the best
/// candidate is selected.
///
/// This is equivalent to `Initialize(majorMinorVersion, null, new PackageVersion(), InitializeOptions.None)`.
///
/// @param majorMinorVersion major and minor version of Windows App SDK's framework package, encoded as `0xMMMMNNNN` where M=Major, N=Minor (e.g. 1.2 == 0x00010002).
/// @see Initialize(uint, string)
/// @see Initialize(uint, string, PackageVersion)
/// @see Initialize(uint, string, PackageVersion, InitializeOptions)
/// @see Shutdown()
public static void Initialize(uint majorMinorVersion)
{
Initialize(majorMinorVersion, null);
}
/// Initialize the calling process to use Windows App SDK's framework package.
///
/// Find a Windows App SDK framework package meeting the criteria and make it available
/// for use by the current process. If multiple packages meet the criteria the best
/// candidate is selected.
///
/// This is equivalent to `Initialize(majorMinorVersion, versionTag, new PackageVersion(), InitializeOptions.None)`.
///
/// @param majorMinorVersion major and minor version of Windows App SDK's framework package, encoded as `0xMMMMNNNN` where M=Major, N=Minor (e.g. 1.2 == 0x00010002).
/// @param versionTag version tag (if any), e.g. "preview1".
/// @see Initialize(uint)
/// @see Initialize(uint, string, PackageVersion)
/// @see Initialize(uint, string, PackageVersion, InitializeOptions)
/// @see Shutdown()
public static void Initialize(uint majorMinorVersion, string versionTag)
{
Initialize(majorMinorVersion, versionTag, new PackageVersion());
}
/// Initialize the calling process to use Windows App SDK's framework package.
///
/// Find a Windows App SDK framework package meeting the criteria and make it available
/// for use by the current process. If multiple packages meet the criteria the best
/// candidate is selected.
///
/// This is equivalent to `Initialize(majorMinorVersion, versionTag, minVersion, InitializeOptions.None)`.
///
/// @param majorMinorVersion major and minor version of Windows App SDK's framework package, encoded as `0xMMMMNNNN` where M=Major, N=Minor (e.g. 1.2 == 0x00010002).
/// @param versionTag version tag (if any), e.g. "preview1".
/// @param minVersion the minimum version to use.
/// @see Initialize(uint)
/// @see Initialize(uint, string)
/// @see Initialize(uint, string, PackageVersion, InitializeOptions)
/// @see Shutdown()
public static void Initialize(uint majorMinorVersion, string versionTag, PackageVersion minVersion)
{
NativeMethods.MddBootstrapInitialize2_Throw(majorMinorVersion, versionTag, minVersion, InitializeOptions.None);
}
/// Initialize the calling process to use Windows App SDK's framework package.
///
/// Find a Windows App SDK framework package meeting the criteria and make it available
/// for use by the current process. If multiple packages meet the criteria the best
/// candidate is selected.
///
/// @param majorMinorVersion major and minor version of Windows App SDK's framework package, encoded as `0xMMMMNNNN` where M=Major, N=Minor (e.g. 1.2 == 0x00010002).
/// @param versionTag version tag (if any), e.g. "preview1".
/// @param minVersion the minimum version to use.
/// @param options optional behavior.
/// @see Initialize(uint)
/// @see Initialize(uint, string)
/// @see Initialize(uint, string, PackageVersion)
/// @see Shutdown()
public static void Initialize(uint majorMinorVersion, string versionTag, PackageVersion minVersion, InitializeOptions options)
{
NativeMethods.MddBootstrapInitialize2_Throw(majorMinorVersion, versionTag, minVersion, options);
}
/// Initialize the calling process to use Windows App SDK's framework package.
/// Failure returns false with the failure HRESULT in the hresult parameter.
///
/// Find a Windows App SDK framework package meeting the criteria and make it available
/// for use by the current process. If multiple packages meet the criteria the best
/// candidate is selected.
///
/// This is equivalent to `TryInitialize(majorMinorVersion, null, new PackageVersion(), InitializeOptions.None, hresult)`.
///
/// @param majorMinorVersion major and minor version of Windows App SDK's framework package, encoded as `0xMMMMNNNN` where M=Major, N=Minor (e.g. 1.2 == 0x00010002).
/// @retval true if successful, otherwise false is returned.
/// @see TryInitialize(uint, string, out int)
/// @see TryInitialize(uint, string, PackageVersion, InitializeOptions, out int)
/// @see Shutdown()
public static bool TryInitialize(uint majorMinorVersion, out int hresult)
{
return TryInitialize(majorMinorVersion, null, out hresult);
}
/// Initialize the calling process to use Windows App SDK's framework package.
/// Failure returns false with the failure HRESULT in the hresult parameter.
///
/// Find a Windows App SDK framework package meeting the criteria and make it available
/// for use by the current process. If multiple packages meet the criteria the best
/// candidate is selected.
///
/// This is equivalent to `TryInitialize(majorMinorVersion, versionTag, new PackageVersion(), InitializeOptions.None, hresult)`.
///
/// @param majorMinorVersion major and minor version of Windows App SDK's framework package, encoded as `0xMMMMNNNN` where M=Major, N=Minor (e.g. 1.2 == 0x00010002).
/// @param versionTag version tag (if any), e.g. "preview1".
/// @retval true if successful, otherwise false is returned.
/// @see TryInitialize(uint, out int)
/// @see TryInitialize(uint, string, PackageVersion, out int)
/// @see TryInitialize(uint, string, PackageVersion, InitializeOptions, out int)
/// @see Shutdown()
public static bool TryInitialize(uint majorMinorVersion, string versionTag, out int hresult)
{
var minVersion = new PackageVersion();
return TryInitialize(majorMinorVersion, versionTag, minVersion, out hresult);
}
/// Initialize the calling process to use Windows App SDK's framework package.
/// Failure returns false with the failure HRESULT in the hresult parameter.
///
/// Find a Windows App SDK framework package meeting the criteria and make it available
/// for use by the current process. If multiple packages meet the criteria the best
/// candidate is selected.
///
/// This is equivalent to `TryInitialize(majorMinorVersion, versionTag, minVersion, InitializeOptions.None, hresult)`.
///
/// @param majorMinorVersion major and minor version of Windows App SDK's framework package, encoded as `0xMMMMNNNN` where M=Major, N=Minor (e.g. 1.2 == 0x00010002).
/// @param versionTag version tag (if any), e.g. "preview1".
/// @param minVersion the minimum version to use.
/// @param options optional behavior.
/// @param hresult the error code if an error occurred.
/// @retval true if successful, otherwise false is returned.
/// @see TryInitialize(uint, out int)
/// @see TryInitialize(uint, string, out int)
/// @see TryInitialize(uint, string, PackageVersion, out int)
/// @see Shutdown()
public static bool TryInitialize(uint majorMinorVersion, string versionTag, PackageVersion minVersion, out int hresult)
{
return TryInitialize(majorMinorVersion, versionTag, minVersion, InitializeOptions.None, out hresult);
}
/// Initialize the calling process to use Windows App SDK's framework package.
/// Failure returns false with the failure HRESULT in the hresult parameter.
///
/// Find a Windows App SDK framework package meeting the criteria and make it available
/// for use by the current process. If multiple packages meet the criteria the best
/// candidate is selected.
///
/// @param majorMinorVersion major and minor version of Windows App SDK's framework package, encoded as `0xMMMMNNNN` where M=Major, N=Minor (e.g. 1.2 == 0x00010002).
/// @param versionTag version tag (if any), e.g. "preview1".
/// @param minVersion the minimum version to use.
/// @param options optional behavior.
/// @param hresult the error code if an error occurred.
/// @retval true if successful, otherwise false is returned.
/// @see TryInitialize(uint, out int)
/// @see TryInitialize(uint, string, out int)
/// @see TryInitialize(uint, string, PackageVersion, out int)
/// @see Shutdown()
public static bool TryInitialize(uint majorMinorVersion, string versionTag, PackageVersion minVersion, InitializeOptions options, out int hresult)
{
hresult = NativeMethods.MddBootstrapInitialize2(majorMinorVersion, versionTag, minVersion, options);
return hresult >= 0;
}
/// Undo the changes made by Initialize().
///
/// @warning Packages made available via `Initialize()` and
/// the Dynamic Dependencies API should not be used after this call.
/// @see Initialize(uint)
/// @see Initialize(uint, string)
/// @see Initialize(uint, string, PackageVersion)
/// @see Initialize(uint, string, PackageVersion, InitializeOptions options)
/// @see TryInitialize(uint, out int)
/// @see TryInitialize(uint, string, out int)
/// @see TryInitialize(uint, string, PackageVersion, out int)
/// @see TryInitialize(uint, string, PackageVersion, InitializeOptions options, out int)
public static void Shutdown()
{
NativeMethods.MddBootstrapShutdown();
}
}
}