Skip to content

Instantly share code, notes, and snippets.

@luojunyuan
Created February 15, 2022 08:50
Show Gist options
  • Save luojunyuan/ce94026dc21e56cce0c2b764b3ccb8a4 to your computer and use it in GitHub Desktop.
Save luojunyuan/ce94026dc21e56cce0c2b764b3ccb8a4 to your computer and use it in GitHub Desktop.
启动器备份
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace Installer
{
public class Installer
{
public static void Main()
{
var directories = Directory.GetDirectories(Directory.GetCurrentDirectory());
var path = directories.Length == 1 ? directories.First() : null;
var wpfPath = "ErogeHelper.Installer.exe";
var winUIPath = "ErogeHelper.Installer.WinUI.exe";
if (path != null)
{
wpfPath = Path.Combine(path, wpfPath);
winUIPath = Path.Combine(path, winUIPath);
}
var isWin7 = Environment.OSVersion.Version < new Version(6, 2);
if (isWin7)
{
var support = CheckOSIsNetCoreSupported();
if (!support)
{
MessageBox((IntPtr)0, "Please install KB2533623 patch!", "Eroge Helper", 0);
return;
}
}
var releaseIdStr = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ReleaseId", "").ToString();
var IsOrAfter1809 = false;
if (int.TryParse(releaseIdStr, out var releaseId))
{
IsOrAfter1809 = releaseId >= 1809;
}
if (IsOrAfter1809 && File.Exists(winUIPath))
{
Process.Start(winUIPath);
}
else
{
Process.Start(wpfPath);
}
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetModuleHandleW", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetModuleHandle([In][MarshalAs(UnmanagedType.LPWStr)] string lpModuleName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true, ThrowOnUnmappableChar = true)]
public static extern IntPtr GetProcAddress([In] IntPtr hModule, [In][MarshalAs(UnmanagedType.LPStr)] string lpProcName);
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr h, string m, string c, int type);
public static bool CheckOSIsNetCoreSupported()
{
var kernel32ModuleHandle = GetModuleHandle("kernel32");
if (kernel32ModuleHandle != IntPtr.Zero)
{
// Three functions required by .Net Core, it's enough to check one
// - SetDefaultDllDirectories
// - AddDllDirectory
// - RemoveDllDirectory
var setDefaultDllDirectoriesProcAddress = GetProcAddress(kernel32ModuleHandle, "SetDefaultDllDirectories");
return setDefaultDllDirectoriesProcAddress != IntPtr.Zero;
}
else
{
throw new InvalidOperationException("Unable to get the module name of Kernel32");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment