Skip to content

Commit 1b03f2e

Browse files
committed
Put all unmanaged functions into their own class.
1 parent c27ce65 commit 1b03f2e

3 files changed

Lines changed: 26 additions & 21 deletions

File tree

src/MainWindow.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,11 @@
66
using System.Windows.Media;
77
using System.Threading.Tasks;
88
using System.Windows.Controls;
9-
using System.Runtime.InteropServices;
10-
using System.Windows.Forms.Integration;
119
using Windows.Management.Deployment;
10+
using System.Windows.Forms.Integration;
1211

1312
class MainWindow : Window
1413
{
15-
[DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
16-
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
17-
internal static extern bool DeleteFile(string lpFileName);
18-
19-
[DllImport("Shell32", CharSet = CharSet.Auto, SetLastError = true)]
20-
internal static extern IntPtr ShellExecute(IntPtr hwnd = default, string lpOperation = null, string lpFile = null, string lpParameters = null, string lpDirectory = null, int nShowCmd = 0);
21-
2214
enum Units { B, KB, MB, GB }
2315

2416
static string Format(float bytes)
@@ -127,7 +119,7 @@ internal MainWindow(bool preview)
127119
client.CancelAsync();
128120
while (client.IsBusy) ;
129121
operation?.Cancel();
130-
DeleteFile(packageUri?.AbsolutePath);
122+
NativeMethods.DeleteFile(packageUri?.AbsolutePath);
131123
foreach (var package in Store.PackageManager.FindPackagesForUserWithPackageTypes(string.Empty, PackageTypes.Framework))
132124
_ = Store.PackageManager.RemovePackageAsync(package.Id.FullName);
133125
};
@@ -161,11 +153,11 @@ internal MainWindow(bool preview)
161153
operation.Progress += (sender, e) => Dispatcher.Invoke(() => { if (progressBar.Value != e.percentage) textBlock1.Text = $"Installing {progressBar.Value = e.percentage}%"; });
162154
operation.AsTask().Wait();
163155
}
164-
finally { DeleteFile(packageUri.AbsolutePath); }
156+
finally { NativeMethods.DeleteFile(packageUri.AbsolutePath); }
165157
}
166158
}
167159

168-
ShellExecute(lpFile: @$"shell:AppsFolder\Microsoft.Minecraft{(preview ? "WindowsBeta" : "UWP")}_8wekyb3d8bbwe!App");
160+
NativeMethods.ShellExecute(lpFile: @$"shell:AppsFolder\Microsoft.Minecraft{(preview ? "WindowsBeta" : "UWP")}_8wekyb3d8bbwe!App");
169161
Dispatcher.Invoke(Close);
170162
});
171163
}

src/NativeMethods.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
static class NativeMethods
5+
{
6+
[DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
7+
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
8+
internal static extern bool DeleteFile(string lpFileName);
9+
10+
[DllImport("Shell32", CharSet = CharSet.Auto, SetLastError = true)]
11+
internal static extern IntPtr ShellExecute(IntPtr hwnd = default, string lpOperation = null, string lpFile = null, string lpParameters = null, string lpDirectory = null, int nShowCmd = 0);
12+
13+
[DllImport("Shlwapi")]
14+
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
15+
internal static extern bool IsOS(int dwOS);
16+
17+
[DllImport("Shell32", CharSet = CharSet.Auto, SetLastError = true)]
18+
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
19+
internal static extern int ShellMessageBox(IntPtr hAppInst = default, IntPtr hWnd = default, string lpcText = default, string lpcTitle = "Bedrock Updater", int fuStyle = 0x00000010);
20+
}

src/Program.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
using System;
22
using System.Linq;
33
using System.Threading;
4-
using System.Runtime.InteropServices;
54

65
static class Program
76
{
8-
[DllImport("Shlwapi")]
9-
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
10-
internal static extern bool IsOS(int dwOS);
117

12-
[DllImport("Shell32", CharSet = CharSet.Auto, SetLastError = true)]
13-
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
14-
internal static extern int ShellMessageBox(IntPtr hAppInst = default, IntPtr hWnd = default, string lpcText = default, string lpcTitle = "Bedrock Updater", int fuStyle = 0x00000010);
158

169
[STAThread]
1710
static void Main(string[] args)
1811
{
1912
using Mutex mutex = new(true, "C7B58EAD-356C-40A1-A145-7262C3C04D00", out bool createdNew);
20-
if (Environment.OSVersion.Version < new Version(10, 0, 19041, 0) || IsOS(29) || !createdNew) return;
13+
if (Environment.OSVersion.Version < new Version(10, 0, 19041, 0) || NativeMethods.IsOS(29) || !createdNew) return;
2114
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
2215
{
2316
var exception = (Exception)e.ExceptionObject;
2417
while (exception.InnerException != null) exception = exception.InnerException;
25-
ShellMessageBox(lpcText: exception.Message);
18+
NativeMethods.ShellMessageBox(lpcText: exception.Message);
2619
Environment.Exit(0);
2720
};
2821
new MainWindow(args.FirstOrDefault()?.Equals("/preview", StringComparison.OrdinalIgnoreCase) ?? false).ShowDialog();

0 commit comments

Comments
 (0)