-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
2,715 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProductVersion>8.0.30703</ProductVersion> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>{CE119D2D-F9B6-4FCF-B527-40CAEAEF029C}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>MutexManager</RootNamespace> | ||
<AssemblyName>MutexManager</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<TargetFrameworkProfile>Client</TargetFrameworkProfile> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="ProgramInfo.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="SingleInstance.cs" /> | ||
<Compile Include="WinAPI.cs" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace MutexManager | ||
{ | ||
|
||
/// <summary> | ||
/// Gets information about the application. | ||
/// </summary> | ||
/// <remarks> | ||
/// This class is just for getting information about the application. | ||
/// Each assembly has a GUID, and that GUID is useful to us in this application, | ||
/// so the most important thing in this class is the AssemblyGuid property. | ||
/// GetEntryAssembly() is used instead of GetExecutingAssembly(), so that you | ||
/// can put this code into a class library and still get the results you expect. | ||
/// (Otherwise it would return info on the DLL assembly instead of your application.) | ||
/// | ||
/// From http://www.codeproject.com/KB/cs/SingleInstanceAppMutex.aspx | ||
/// </remarks> | ||
static internal class ProgramInfo | ||
{ | ||
static internal string AssemblyGuid | ||
{ | ||
get | ||
{ | ||
object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false); | ||
if (attributes.Length == 0) | ||
{ | ||
return String.Empty; | ||
} | ||
return ((System.Runtime.InteropServices.GuidAttribute)attributes[0]).Value; | ||
} | ||
} | ||
static internal string AssemblyTitle | ||
{ | ||
get | ||
{ | ||
object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false); | ||
if (attributes.Length > 0) | ||
{ | ||
AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0]; | ||
if (titleAttribute.Title != "") | ||
{ | ||
return titleAttribute.Title; | ||
} | ||
} | ||
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("MutexManager")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("MutexManager")] | ||
[assembly: AssemblyCopyright("Copyright © 2010")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("241786b1-4dc3-4c88-97e0-7db750d1ecf1")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace MutexManager | ||
{ | ||
|
||
/// <summary> | ||
/// Enforces a single instance. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is where the magic happens. | ||
/// Start() tries to create a mutex. | ||
/// If it detects that another instance is already using the mutex, then it returns FALSE. | ||
/// Otherwise it returns TRUE. | ||
/// (Notice that a GUID is used for the mutex name, which is a little better than using the application name.) | ||
/// If another instance is detected, then you can use ShowFirstInstance() to show it | ||
/// (which will work as long as you override WndProc as shown above). | ||
/// ShowFirstInstance() broadcasts a message to all windows. | ||
/// The message is WM_SHOWFIRSTINSTANCE. | ||
/// (Notice that a GUID is used for WM_SHOWFIRSTINSTANCE. | ||
/// That allows you to reuse this code in multiple applications without getting | ||
/// strange results when you run them all at the same time.) | ||
/// | ||
/// From http://www.codeproject.com/KB/cs/SingleInstanceAppMutex.aspx | ||
/// </remarks> | ||
static public class SingleInstance | ||
{ | ||
public static readonly int WM_SHOWFIRSTINSTANCE = | ||
WinApi.RegisterWindowMessage("WM_SHOWFIRSTINSTANCE|{0}", ProgramInfo.AssemblyGuid); | ||
private 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() | ||
{ | ||
mutex.ReleaseMutex(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace MutexManager | ||
{ | ||
|
||
/// <summary> | ||
/// A wrapper for various WinAPI functions. | ||
/// </summary> | ||
/// <remarks> | ||
/// This class is just a wrapper for your various WinApi functions. | ||
/// In this sample only the bare essentials are included. | ||
/// In my own WinApi class, I have all the WinApi functions that any | ||
/// of my applications would ever need. | ||
/// | ||
/// From http://www.codeproject.com/KB/cs/SingleInstanceAppMutex.aspx | ||
/// </remarks> | ||
static public class WinApi | ||
{ | ||
[DllImport("user32")] | ||
public static extern int RegisterWindowMessage(string message); | ||
|
||
internal static int RegisterWindowMessage(string format, params object[] args) | ||
{ | ||
string message = String.Format(format, args); | ||
return RegisterWindowMessage(message); | ||
} | ||
|
||
internal const int HWND_BROADCAST = 0xffff; | ||
internal const int SW_SHOWNORMAL = 1; | ||
|
||
[DllImport("user32")] | ||
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam); | ||
|
||
[DllImportAttribute("user32.dll")] | ||
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); | ||
|
||
[DllImportAttribute("user32.dll")] | ||
public static extern bool SetForegroundWindow(IntPtr hWnd); | ||
|
||
internal static void ShowToFront(IntPtr window) | ||
{ | ||
ShowWindow(window, SW_SHOWNORMAL); | ||
SetForegroundWindow(window); | ||
} | ||
} | ||
} |
Oops, something went wrong.