2019-02-02 12:55:56 +05:30
|
|
|
|
/*
|
|
|
|
|
Technitium DNS Server
|
2022-11-12 15:33:53 +05:30
|
|
|
|
Copyright (C) 2022 Shreyas Zare (shreyas@technitium.com)
|
2019-02-02 12:55:56 +05:30
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2019-02-16 18:30:37 +05:30
|
|
|
|
using System.IO;
|
2019-02-02 12:55:56 +05:30
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace DnsServerSystemTrayApp
|
|
|
|
|
{
|
|
|
|
|
static class Program
|
|
|
|
|
{
|
|
|
|
|
#region variables
|
|
|
|
|
|
2019-12-29 13:32:06 +05:30
|
|
|
|
const string MUTEX_NAME = "TechnitiumDnsServerSystemTrayApp";
|
2019-02-02 12:55:56 +05:30
|
|
|
|
|
2021-03-21 17:34:28 +05:30
|
|
|
|
public static readonly string APP_PATH = Assembly.GetEntryAssembly().Location;
|
|
|
|
|
|
|
|
|
|
static readonly bool _isAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
|
2019-02-02 12:55:56 +05:30
|
|
|
|
static Mutex _app;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2021-03-21 17:34:28 +05:30
|
|
|
|
#region constructor
|
|
|
|
|
|
|
|
|
|
static Program()
|
|
|
|
|
{
|
|
|
|
|
if (APP_PATH.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
APP_PATH = APP_PATH.Substring(0, APP_PATH.Length - 4) + ".exe";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2019-02-02 12:55:56 +05:30
|
|
|
|
#region public
|
|
|
|
|
|
|
|
|
|
[STAThread]
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
|
|
2019-12-29 13:32:06 +05:30
|
|
|
|
#region check for multiple instances
|
2019-02-16 18:30:37 +05:30
|
|
|
|
|
2019-12-29 13:32:06 +05:30
|
|
|
|
_app = new Mutex(true, MUTEX_NAME, out bool createdNewMutex);
|
2019-02-02 12:55:56 +05:30
|
|
|
|
|
2022-11-12 15:33:53 +05:30
|
|
|
|
bool exitApp = false;
|
|
|
|
|
|
2019-12-29 13:32:06 +05:30
|
|
|
|
if (!createdNewMutex)
|
2019-02-02 12:55:56 +05:30
|
|
|
|
{
|
2022-11-12 15:33:53 +05:30
|
|
|
|
if (args.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Technitium DNS Server system tray app is already running.", "Already Running!", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
exitApp = true;
|
|
|
|
|
}
|
2019-02-02 12:55:56 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2021-03-21 17:34:28 +05:30
|
|
|
|
string configFile = Path.Combine(Path.GetDirectoryName(APP_PATH), "SystemTrayApp.config");
|
2019-02-02 12:55:56 +05:30
|
|
|
|
|
2022-11-12 15:33:53 +05:30
|
|
|
|
MainApplicationContext mainApp = new MainApplicationContext(configFile, args, ref exitApp);
|
|
|
|
|
|
|
|
|
|
if (exitApp)
|
|
|
|
|
mainApp.Dispose();
|
|
|
|
|
else
|
|
|
|
|
Application.Run(mainApp);
|
2019-12-29 13:32:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RunAsAdmin(string args)
|
|
|
|
|
{
|
|
|
|
|
if (_isAdmin)
|
|
|
|
|
throw new Exception("App is already running as admin.");
|
2019-02-02 12:55:56 +05:30
|
|
|
|
|
2021-03-21 17:34:28 +05:30
|
|
|
|
ProcessStartInfo processInfo = new ProcessStartInfo(APP_PATH, args);
|
2019-02-02 12:55:56 +05:30
|
|
|
|
|
2019-12-29 13:32:06 +05:30
|
|
|
|
processInfo.UseShellExecute = true;
|
|
|
|
|
processInfo.Verb = "runas";
|
|
|
|
|
|
|
|
|
|
try
|
2019-02-02 12:55:56 +05:30
|
|
|
|
{
|
2019-12-29 13:32:06 +05:30
|
|
|
|
_app.Dispose();
|
|
|
|
|
Process.Start(processInfo);
|
|
|
|
|
Application.Exit();
|
2019-02-02 12:55:56 +05:30
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-29 13:32:06 +05:30
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Error! " + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
2019-02-02 12:55:56 +05:30
|
|
|
|
|
2019-12-29 13:32:06 +05:30
|
|
|
|
//user cancels UAC or exception occurred
|
2021-03-07 15:49:38 +05:30
|
|
|
|
_app = new Mutex(true, MUTEX_NAME, out _);
|
2019-12-29 13:32:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2019-02-02 12:55:56 +05:30
|
|
|
|
|
2019-12-29 13:32:06 +05:30
|
|
|
|
#region properties
|
2019-02-16 18:30:37 +05:30
|
|
|
|
|
2019-12-29 13:32:06 +05:30
|
|
|
|
public static bool IsAdmin
|
|
|
|
|
{ get { return _isAdmin; } }
|
2019-02-02 12:55:56 +05:30
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|