Repository navigation
Expand file tree
/
Copy pathLightCore.MutexSingleInstance.pas
More file actions
127 lines (96 loc) · 3.83 KB
/
Copy pathLightCore.MutexSingleInstance.pas
File metadata and controls
127 lines (96 loc) · 3.83 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
UNIT LightCore.MutexSingleInstance;
{=============================================================================================================
2026.09.30
www.GabrielMoraru.com
==============================================================================================================
Only allow one instance of the program to run at a time.
FreeMutex is called automatically during finalization.
Usage:
Project Test;
begin
AppData.AppName:= 'MyApp';
if NOT IsSingleInstance(AppData.AppName) then EXIT;
Application.Initialize;
AppData.CreateMainForm(TfrmStarter, frmStarter);
Application.Run;
end.
This unit is not deprecated, but it was replaced by AppData.InstanceRunning which is easier to use.
IsSingleInstance has a Windows body only, so far. It is declared only on Windows, so a call written in an Android, macOS or iOS build fails to compile instead of getting a silent wrong answer. FreeMutex is declared on every platform and does nothing off Windows.
=============================================================================================================}
INTERFACE
USES
{$IFDEF MSWINDOWS}
WinApi.Windows,
{$ENDIF}
System.SysUtils;
procedure FreeMutex;
{$IFDEF MSWINDOWS}
function IsSingleInstance(CONST MutexName: string): Boolean;
{$ENDIF}
IMPLEMENTATION
{$IFDEF MSWINDOWS}
VAR
SingleMutex: THandle = 0;
{ Creates a mutex to see if the program is already running.
By default (lpMutexAttributes=nil) created mutexes are accessible only by the user running the process.
We need our mutexes to be accessible to all users, so that the mutex detection can work across user sessions.
I.e. both the current user account and the System (Service) account.
To do this we use a security descriptor with a null DACL. }
function IsSingleInstance(CONST MutexName: string): Boolean;
CONST
MUTEX_GLOBAL = 'Global\'; { Prefix to create object in global namespace - accessible across user sessions }
VAR
SecurityDesc: TSecurityDescriptor;
SecurityAttr: TSecurityAttributes;
ErrCode: Integer;
begin
if MutexName = ''
then raise Exception.Create('IsSingleInstance: MutexName parameter cannot be empty');
{ Release any previously created mutex to prevent handle leak if called multiple times }
if SingleMutex <> 0 then
begin
CloseHandle(SingleMutex);
SingleMutex:= 0;
end;
InitializeSecurityDescriptor(@SecurityDesc, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@SecurityDesc, TRUE, NIL, FALSE);
SecurityAttr.nLength:= SizeOf(SecurityAttr);
SecurityAttr.lpSecurityDescriptor:= @SecurityDesc;
SecurityAttr.bInheritHandle:= FALSE;
{ The mutex is created in the global namespace which makes it possible to access across user sessions }
SingleMutex:= CreateMutex(@SecurityAttr, TRUE, PChar(MUTEX_GLOBAL + MutexName));
ErrCode:= GetLastError;
{ If the mutex already exists, another instance is running.
Note: CreateMutex returns a valid handle even if mutex exists, but GetLastError returns ERROR_ALREADY_EXISTS }
if ErrCode = ERROR_ALREADY_EXISTS then
begin
Result:= FALSE;
CloseHandle(SingleMutex);
SingleMutex:= 0;
end
else
Result:= TRUE; { This is the first instance }
{ The mutex handle is kept open for the lifetime of the application.
The system closes it automatically when the process terminates. }
end;
procedure FreeMutex;
begin
if SingleMutex <> 0 then
begin
CloseHandle(SingleMutex);
SingleMutex:= 0;
end;
end;
{$ELSE}
procedure FreeMutex;
begin
end;
{$ENDIF}
INITIALIZATION
{ No initialization needed }
FINALIZATION
{ Note: Finalization is used here to ensure the mutex is released even if
the application crashes or terminates unexpectedly. The OS will also
release it when the process ends, but explicit cleanup is preferred. }
FreeMutex;
end.