d41dbfbb3a
Lancee, fermee par la croix (tray), puis relancee : la fenetre revenait derriere les autres, seule son icone dans la barre des taches. Mesure : visible, non minimisee, pas au premier plan. La 2e instance, qui recoit le double-clic, reveille la 1re par un message ; mais la 1re n'a recu aucune entree, et Windows lui refuse SetForegroundWindow. La 2e cede maintenant son droit (AllowSetForegroundWindow) avant d'envoyer le message. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
86 lines
2.8 KiB
ObjectPascal
86 lines
2.8 KiB
ObjectPascal
unit PM.SingleInstance;
|
|
|
|
{
|
|
Single-instance guard.
|
|
|
|
AcquireOrSignal:
|
|
- First instance: creates a named mutex and returns True. Caller proceeds.
|
|
- Subsequent instance: detects the mutex, broadcasts WM_PMSHOW so the
|
|
running instance restores from tray, returns False. Caller exits.
|
|
|
|
WM_PMSHOW is a RegisterWindowMessage('PMServer_ShowExisting') — system-
|
|
unique, all processes that register the same string get the same ID.
|
|
PM.Bridge listens for it on its message-only window.
|
|
}
|
|
|
|
interface
|
|
|
|
uses
|
|
Winapi.Windows, Winapi.Messages;
|
|
|
|
const
|
|
// Mutex name lives in the Local\ namespace → per-user-session, so a
|
|
// second user on the same machine (RDP, Switch User) can still launch
|
|
// their own instance. The Global\ namespace would block them.
|
|
PMSERVER_MUTEX_NAME = 'Local\PMServer.SingleInstance.Mutex';
|
|
|
|
// System-wide unique message ID, computed once. Bridge + .dpr both call
|
|
// this to get the same UINT.
|
|
function WM_PMShowMessage: UINT;
|
|
|
|
// Try to become the single instance. True = we are first; False = another
|
|
// instance was already running (we have signalled it and the caller must
|
|
// exit immediately).
|
|
function AcquireOrSignal: Boolean;
|
|
|
|
implementation
|
|
|
|
var
|
|
_Mutex: THandle = 0;
|
|
_WmShow: UINT = 0;
|
|
|
|
function WM_PMShowMessage: UINT;
|
|
begin
|
|
if _WmShow = 0 then
|
|
_WmShow := RegisterWindowMessage('PMServer_ShowExisting');
|
|
Result := _WmShow;
|
|
end;
|
|
|
|
function AcquireOrSignal: Boolean;
|
|
var
|
|
LErr: DWORD;
|
|
begin
|
|
_Mutex := CreateMutex(nil, True, PMSERVER_MUTEX_NAME);
|
|
LErr := GetLastError;
|
|
|
|
if (_Mutex <> 0) and (LErr <> ERROR_ALREADY_EXISTS) then
|
|
begin
|
|
// We are the first instance. Keep the mutex alive for the process
|
|
// lifetime — Windows releases it automatically on exit.
|
|
Result := True;
|
|
Exit;
|
|
end;
|
|
|
|
// Another instance is already running. Close our handle (it isn't ours)
|
|
// and broadcast the show-message to all top-level windows. The running
|
|
// bridge picks it up on its message-only window.
|
|
if _Mutex <> 0 then
|
|
begin
|
|
CloseHandle(_Mutex);
|
|
_Mutex := 0;
|
|
end;
|
|
// CEDER LE PREMIER PLAN AVANT DE REVEILLER L'AUTRE. C'est NOUS qui venons de
|
|
// recevoir le double-clic de l'utilisateur, donc nous seuls avons le droit de
|
|
// passer devant. Sans cette cession, l'instance deja lancee — qui n'a recu
|
|
// aucune entree — se voit refuser SetForegroundWindow par Windows : sa
|
|
// fenetre se montre DERRIERE les autres, avec seulement son icone dans la
|
|
// barre des taches. Mesure avant correctif : visible, non minimisee, pas au
|
|
// premier plan. Tous processus parce que le PID de l'autre instance n'est pas connu
|
|
// ici ; le droit cede expire a la prochaine entree utilisateur.
|
|
AllowSetForegroundWindow(DWORD(-1)); // ASFW_ANY, absent de Winapi.Windows
|
|
PostMessage(HWND_BROADCAST, WM_PMShowMessage, 0, 0);
|
|
Result := False;
|
|
end;
|
|
|
|
end.
|