-
Notifications
You must be signed in to change notification settings - Fork 625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
If another instance of the app is allready running, it just opens it #389
Open
0xAozora
wants to merge
3
commits into
Jessecar96:master
Choose a base branch
from
0xAozora:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,47 +31,92 @@ 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 | ||
{ | ||
/// <summary> | ||
/// The main entry point for the application. | ||
/// </summary> | ||
[STAThread] | ||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete this |
||
Application.SetCompatibleTextRenderingDefault(false); | ||
|
||
|
||
|
||
// Parse command line arguments | ||
var options = new Options(); | ||
Parser.Default.ParseArguments(args, options); | ||
|
||
Application.EnableVisualStyles(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am not sure adding this