-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathProgram.cs
More file actions
112 lines (101 loc) · 4.55 KB
/
Program.cs
File metadata and controls
112 lines (101 loc) · 4.55 KB
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
using System;
using System.Runtime.InteropServices;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.Windows.ApplicationModel.DynamicDependency;
using Velopack;
namespace FluentTaskScheduler
{
public static class Program
{
[DllImport("Microsoft.ui.xaml.dll")]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
private static extern void XamlCheckProcessRequirements();
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr AddDllDirectory(string lpPathName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool SetDefaultDllDirectories(uint directoryFlags);
[STAThread]
static void Main(string[] args)
{
// Set the DLL search path to include the application directory.
// This is critical for ARM64 and self-contained builds where native DLLs
// might not be found by the default search logic.
string appDir = AppDomain.CurrentDomain.BaseDirectory;
AddDllDirectory(appDir);
SetDefaultDllDirectories(0x00001000); // LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
// Initialize ComWrappers as early as possible for WinRT support.
// This MUST be done before any WinRT types are accessed or the bootstrapper runs.
WinRT.ComWrappersSupport.InitializeComWrappers();
// VeloPack: Handle install/uninstall/update hooks before anything else.
// In a machine-wide install (C:\Program Files), non-admin users don't have write access,
// which causes Velopack to crash with UnauthorizedAccessException when it tries to
// manage the 'packages' directory. We skip Velopack for non-admins in protected folders.
if (HasWriteAccessToAppDir())
{
try
{
VelopackApp.Build().Run();
}
catch (Exception)
{
// Catch-all for any other Velopack initialization issues
}
}
// Initialize the Windows App SDK bootstrapper for unpackaged apps
try
{
// In a self-contained environment, we check if we should even call the bootstrapper.
// If WindowsAppSDKSelfContained is true, the runtime is next to the EXE.
// However, for custom Main methods, calling Bootstrap.Initialize with the right version
// helps the WinRT subsystem find the metadata even if the manifest merging is tricky.
// We use Windows App SDK 1.5 (0x00010005). We use an empty tag to avoid issues with
// specific servicing versions that might not be present on the target machine.
Bootstrap.Initialize(0x00010005, "");
}
catch (Exception ex)
{
// If this fails, it's often because the Framework Package isn't installed.
// In self-contained scenarios, this is expected to fail on machines without the SDK,
// but we carry on and hope the local DLLs and manifest are enough.
System.Diagnostics.Debug.WriteLine($"Bootstrap initialization failed: {ex.Message}");
}
try
{
XamlCheckProcessRequirements();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"XamlCheckProcessRequirements failed: {ex.Message}");
}
try
{
Application.Start((p) =>
{
var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
System.Threading.SynchronizationContext.SetSynchronizationContext(context);
new App();
});
}
finally
{
Bootstrap.Shutdown();
}
}
private static bool HasWriteAccessToAppDir()
{
try
{
string appDir = AppDomain.CurrentDomain.BaseDirectory;
string testPath = System.IO.Path.Combine(appDir, ".velopack_write_test");
System.IO.File.WriteAllText(testPath, "test");
System.IO.File.Delete(testPath);
return true;
}
catch
{
return false;
}
}
}
}