100 lines
3.5 KiB
C#
100 lines
3.5 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
using v2rayN.Forms;
|
|
using v2rayN.Properties;
|
|
using v2rayN.Tool;
|
|
|
|
namespace v2rayN
|
|
{
|
|
static class Program
|
|
{
|
|
[System.Runtime.InteropServices.DllImport("user32.dll")]
|
|
private static extern bool SetProcessDPIAware();
|
|
|
|
/// <summary>
|
|
/// 应用程序的主入口点。
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
if (Environment.OSVersion.Version.Major >= 6)
|
|
{
|
|
SetProcessDPIAware();
|
|
}
|
|
|
|
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
|
|
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
|
|
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
|
|
|
|
|
|
//AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
|
|
|
if (!IsDuplicateInstance())
|
|
{
|
|
Logging.Setup();
|
|
Utils.SaveLog($"v2rayN start up | {Utils.GetVersion()} | {Utils.GetExePath()}");
|
|
|
|
//设置语言环境
|
|
string lang = Utils.RegReadValue(Global.MyRegPath, Global.MyRegKeyLanguage, "zh-Hans");
|
|
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
|
|
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
Application.Run(new MainForm());
|
|
}
|
|
else
|
|
{
|
|
UI.ShowWarning($"v2rayN is already running(v2rayN已经运行)");
|
|
}
|
|
}
|
|
|
|
//private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
|
//{
|
|
// try
|
|
// {
|
|
// string resourceName = "v2rayN.LIB." + new AssemblyName(args.Name).Name + ".dll";
|
|
// using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
|
|
// {
|
|
// if (stream == null)
|
|
// {
|
|
// return null;
|
|
// }
|
|
// byte[] assemblyData = new byte[stream.Length];
|
|
// stream.Read(assemblyData, 0, assemblyData.Length);
|
|
// return Assembly.Load(assemblyData);
|
|
// }
|
|
// }
|
|
// catch
|
|
// {
|
|
// return null;
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 检查是否已在运行
|
|
/// </summary>
|
|
public static bool IsDuplicateInstance()
|
|
{
|
|
//string name = "v2rayN";
|
|
|
|
string name = Utils.GetExePath(); // Allow different locations to run
|
|
name = name.Replace("\\", "/"); // https://stackoverflow.com/questions/20714120/could-not-find-a-part-of-the-path-error-while-creating-mutex
|
|
|
|
Global.mutexObj = new Mutex(false, name, out bool bCreatedNew);
|
|
return !bCreatedNew;
|
|
}
|
|
|
|
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
|
|
{
|
|
Utils.SaveLog("Application_ThreadException", e.Exception);
|
|
}
|
|
|
|
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
{
|
|
Utils.SaveLog("CurrentDomain_UnhandledException", (Exception)e.ExceptionObject);
|
|
}
|
|
}
|
|
}
|