Bug 164181. Prevent multiple instances of CASAManager on windows.

This commit is contained in:
Jim Norman 2006-07-03 19:04:31 +00:00
parent feb8a323af
commit 1b5e320313
5 changed files with 170 additions and 20 deletions

View File

@ -1,3 +1,7 @@
--------------------------------------------------------------------
Mon Jul 03 13:01:53 MST 2006 - jnorman@novell.com
- Bug 164181. Prevent multiple instances of CASAManager on windows.
--------------------------------------------------------------------
Tue Jun 27 14:22:53 MST 2006 - jnorman@novell.com
- Added miCASARemoveKey API to dll.

View File

@ -104,40 +104,40 @@
Project = "{E21DD887-22F4-4935-9851-409715F663B0}"
Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
/>
<Reference
Name = "Novell.CASA.CASAPolicy"
Project = "{636A9D7E-BFB5-4EB9-96F8-51FF85A98826}"
Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
/>
<Reference
Name = "glib-sharp"
AssemblyName = "glib-sharp"
HintPath = "..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp-2.0\glib-sharp.dll"
/>
<Reference
Name = "gtk-sharp"
AssemblyName = "gtk-sharp"
HintPath = "..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp-2.0\gtk-sharp.dll"
/>
<Reference
Name = "pango-sharp"
AssemblyName = "pango-sharp"
HintPath = "..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp-2.0\pango-sharp.dll"
HintPath = "..\..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp\pango-sharp.dll"
/>
<Reference
Name = "atk-sharp"
AssemblyName = "atk-sharp"
HintPath = "..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp-2.0\atk-sharp.dll"
HintPath = "..\..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp\atk-sharp.dll"
/>
<Reference
Name = "gdk-sharp"
AssemblyName = "gdk-sharp"
HintPath = "..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp-2.0\gdk-sharp.dll"
HintPath = "..\..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp\gdk-sharp.dll"
/>
<Reference
Name = "glade-sharp"
AssemblyName = "glade-sharp"
HintPath = "..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp-2.0\glade-sharp.dll"
HintPath = "..\..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp\glade-sharp.dll"
/>
<Reference
Name = "glib-sharp"
AssemblyName = "glib-sharp"
HintPath = "..\..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp\glib-sharp.dll"
/>
<Reference
Name = "gtk-sharp"
AssemblyName = "gtk-sharp"
HintPath = "..\..\..\..\..\..\GtkRun\2.8\lib\gtk-sharp\gtk-sharp.dll"
/>
<Reference
Name = "Novell.CASA.CASAPolicy"
Project = "{636A9D7E-BFB5-4EB9-96F8-51FF85A98826}"
Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
/>
</References>
</Build>
@ -198,6 +198,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "SingleApplication.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Store.cs"
SubType = "Code"

View File

@ -1,7 +1,7 @@
<VisualStudioProject>
<CSHARP LastOpenVersion = "7.10.3077" >
<Build>
<Settings ReferencePath = "C:\GtkRun\2.8\lib\gtk-sharp-2.0\" >
<Settings ReferencePath = "D:\casa\extern\w32\gtk-sharp-2.0\;C:\GtkRun\2.8\lib\gtk-sharp\" >
<Config
Name = "Debug"
EnableASPDebugging = "false"

View File

@ -281,6 +281,10 @@ public class Common
}
else if( true == IS_WINDOWS )
{
#if W32
bRet = SingleInstance.SingleApplication.Run();
#endif
#if DOTNET2
Process current = Process.GetCurrentProcess();
Process[] p = System.Diagnostics.Process.GetProcessesByName("CASAManager");

View File

@ -0,0 +1,137 @@
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.IO;
namespace SingleInstance
{
/// <summary>
/// Summary description for SingleApp.
/// </summary>
public class SingleApplication
{
public SingleApplication()
{
}
/// <summary>
/// Imports
/// </summary>
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int IsIconic(IntPtr hWnd);
/// <summary>
/// GetCurrentInstanceWindowHandle
/// </summary>
/// <returns></returns>
private static IntPtr GetCurrentInstanceWindowHandle()
{
IntPtr hWnd = IntPtr.Zero;
Process process = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(process.ProcessName);
foreach(Process _process in processes)
{
// Get the first instance that is not this instance, has the
// same process name and was started from the same file name
// and location. Also check that the process has a valid
// window handle in this session to filter out other user's
// processes.
if (_process.Id != process.Id &&
_process.MainModule.FileName == process.MainModule.FileName &&
_process.MainWindowHandle != IntPtr.Zero)
{
hWnd = _process.MainWindowHandle;
break;
}
}
Console.WriteLine("Window Handle: " + hWnd.ToString());
return hWnd;
}
/// <summary>
/// SwitchToCurrentInstance
/// </summary>
public static bool SwitchToCurrentInstance()
{
IntPtr hWnd = GetCurrentInstanceWindowHandle();
if (hWnd != IntPtr.Zero)
{
// Restore window if minimised. Do not restore if already in
// normal or maximised window state, since we don't want to
// change the current state of the window.
if (IsIconic(hWnd) != 0)
{
ShowWindow(hWnd, SW_RESTORE);
}
// Set foreground window.
SetForegroundWindow(hWnd);
return true;
}
else
return false;
}
/// <summary>
/// Execute a form base application if another instance already running on
/// the system activate previous one
/// </summary>
/// <param name="frmMain">main form</param>
/// <returns>true if no previous instance is running</returns>
public static bool Run(System.Windows.Forms.Form frmMain)
{
if(IsAlreadyRunning())
{
//set focus on previously running app
SwitchToCurrentInstance();
return false;
}
Application.Run(frmMain);
return true;
}
/// <summary>
/// for GUI
/// </summary>
/// <returns></returns>
public static bool Run()
{
if(IsAlreadyRunning())
{
return true;
}
return false;
}
/// <summary>
/// check if given exe alread running or not
/// </summary>
/// <returns>returns true if already running</returns>
private static bool IsAlreadyRunning()
{
string strLoc = Assembly.GetExecutingAssembly().Location;
FileSystemInfo fileInfo = new FileInfo(strLoc);
string sExeName = fileInfo.Name;
bool bCreatedNew;
mutex = new Mutex(true, "Global\\"+sExeName, out bCreatedNew);
if (bCreatedNew)
mutex.ReleaseMutex();
return !bCreatedNew;
}
static Mutex mutex;
const int SW_RESTORE = 9;
}
}