Menu

[r116]: / trunk / DisplayPowerOff / LaunchGuard.cpp  Maximize  Restore  History

Download this file

50 lines (38 with data), 1.0 kB

 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
#include "stdafx.h"
#include "LaunchGuard.h"
#include "Options.h"
namespace
{
HANDLE hAppMutex;
[[nodiscard]] auto GetMutexName()
{
constexpr auto cszMutexGuid = TEXT("{58E5128B-B829-4B61-B3EA-41DCF6A4B5D1}");
CharBuffer<64, ClearBuffer::First> szMutexName;
if (Options::NotificationAreaIcon())
{
VERIFY_STR(StringCchCat(szMutexName, szMutexName, TEXT("NotificationAreaIcon")));
}
VERIFY_STR(StringCchCat(szMutexName, szMutexName, cszMutexGuid));
return szMutexName;
}
}
bool LaunchGuard::TryEnter()
{
const auto cszMutexName = GetMutexName();
hAppMutex = CreateMutex(nullptr, FALSE, cszMutexName);
ASSERT(hAppMutex);
if (!hAppMutex || GetLastError() == ERROR_ALREADY_EXISTS)
{
TRACE(hAppMutex ? TEXT("Mutex already exists") : TEXT("Error creating mutex"));
return false;
}
return true;
}
void LaunchGuard::Exit()
{
if (hAppMutex)
{
TRACE(TEXT("Closing mutex..."));
VERIFY(CloseHandle(hAppMutex));
}
}