diff --git a/Steam Desktop Authenticator/MainForm.cs b/Steam Desktop Authenticator/MainForm.cs index 7eae5f63..6596d289 100644 --- a/Steam Desktop Authenticator/MainForm.cs +++ b/Steam Desktop Authenticator/MainForm.cs @@ -27,6 +27,16 @@ public partial class MainForm : Form // Forms private TradePopupForm popupFrm = new TradePopupForm(); + protected override void WndProc(ref System.Windows.Forms.Message message) + { + if (message.Msg == SingleInstance.WM_SHOWFIRSTINSTANCE) + { + this.Show(); + this.WindowState = FormWindowState.Normal; + } + base.WndProc(ref message); + } + public MainForm() { InitializeComponent(); @@ -724,5 +734,10 @@ private void MainForm_KeyDown(object sender, KeyEventArgs e) CopyLoginToken(); } } + + private void listAccounts_SelectedIndexChanged(object sender, EventArgs e) + { + + } } } diff --git a/Steam Desktop Authenticator/Program.cs b/Steam Desktop Authenticator/Program.cs index a9c655d0..f29f3e91 100644 --- a/Steam Desktop Authenticator/Program.cs +++ b/Steam Desktop Authenticator/Program.cs @@ -1,13 +1,12 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Security; -using System.Threading.Tasks; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Threading; using System.Windows.Forms; -using System.Diagnostics; using CommandLine; using CommandLine.Text; + namespace Steam_Desktop_Authenticator { @@ -32,32 +31,72 @@ public string GetUsage() } } - static class Program + static public class WinApi + { + [DllImport("user32")] + public static extern int RegisterWindowMessage(string message); + + public static int RegisterWindowMessage(string format, params object[] args) + { + string message = String.Format(format, args); + return RegisterWindowMessage(message); + } + + public const int HWND_BROADCAST = 0xffff; + public const int SW_SHOWNORMAL = 1; + + [DllImport("user32")] + public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam); + } + + static public class SingleInstance { - public static Process PriorProcess() - // Returns a System.Diagnostics.Process pointing to - // a pre-existing process with the same name as the - // current one, if any; or null if the current process - // is unique. + public static readonly int WM_SHOWFIRSTINSTANCE = WinApi.RegisterWindowMessage("WM_SHOWFIRSTINSTANCE|{0}", ProgramInfo.AssemblyGuid); + static Mutex mutex; + static public bool Start() + { + bool onlyInstance = false; + string mutexName = String.Format("Local\\{0}", ProgramInfo.AssemblyGuid); + + // if you want your app to be limited to a single instance + // across ALL SESSIONS (multiple users & terminal services), then use the following line instead: + // string mutexName = String.Format("Global\\{0}", ProgramInfo.AssemblyGuid); + + mutex = new Mutex(true, mutexName, out onlyInstance); + return onlyInstance; + } + static public void ShowFirstInstance() + { + WinApi.PostMessage( + (IntPtr)WinApi.HWND_BROADCAST, + WM_SHOWFIRSTINSTANCE, + IntPtr.Zero, + IntPtr.Zero); + } + static public void Stop() { - try + mutex.ReleaseMutex(); + } + } + + static public class ProgramInfo + { + static public string AssemblyGuid + { + get { - Process curr = Process.GetCurrentProcess(); - Process[] procs = Process.GetProcessesByName(curr.ProcessName); - foreach (Process p in procs) + object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false); + if (attributes.Length == 0) { - if ((p.Id != curr.Id) && - (p.MainModule.FileName == curr.MainModule.FileName)) - return p; + return String.Empty; } - return null; - } - catch (Exception) - { - return null; + return ((System.Runtime.InteropServices.GuidAttribute)attributes[0]).Value; } } + } + static class Program + { /// /// The main entry point for the application. /// @@ -65,14 +104,19 @@ public static Process PriorProcess() static void Main(string[] args) { // run the program only once - if (PriorProcess() != null) + if (!SingleInstance.Start()) { - MessageBox.Show("Another instance of the app is already running."); + SingleInstance.ShowFirstInstance(); return; } - // Parse command line arguments - var options = new Options(); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + + + + // Parse command line arguments + var options = new Options(); Parser.Default.ParseArguments(args, options); Application.EnableVisualStyles();