-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckMutex.cs
59 lines (52 loc) · 1.56 KB
/
CheckMutex.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace MUTEX
{
public class Win32Calls
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes,
bool bInitialOwner, string lpName);
[DllImport("kernel32.dll")]
public static extern bool ReleaseMutex(IntPtr hMutex);
public const int ERROR_ALREADY_EXISTS = 183;
}
class Program
{
static int Main(string[] args)
{
if (args.Length == 0)
{
System.Console.WriteLine("Please enter a Mutex name to check");
return -1;
}
string sMutex = Convert.ToString(args[0]);
IntPtr ipMutexAttr = new IntPtr(0);
IntPtr ipHMutex = new IntPtr(0);
ipHMutex = Win32Calls.CreateMutex(ipMutexAttr,
true, sMutex);
if (ipHMutex != (IntPtr)0)
{
int iGLE = Marshal.GetLastWin32Error();
if (iGLE == Win32Calls.ERROR_ALREADY_EXISTS)
{
Console.WriteLine("Mutex found!");
return 1;
}
else
{
Console.Write("Not Found");
return 0;
}
}
else
{
Console.WriteLine("Error during operation...");
return -1;
}
}
}
}