unit RunOnce; interface uses Windows, SysUtils, Classes, Forms, Dialogs; type TRunOnce = class(TComponent) private FApplicationName : string; procedure IsExist; public constructor Create(Aowner : TComponent); override; published property ApplicationName : string read FApplicationName write FApplicationName; end; procedure Register; implementation constructor TRunOnce.Create(Aowner : TComponent); begin inherited Create(Aowner); if not (csDesigning in ComponentState) then begin if ApplicationName = '' then ApplicationName := ExtractFileName(Application.ExeName); IsExist; end; end; procedure TRunOnce.IsExist; var FHandle : THandle; Hndl : THandle; FFileView : Pointer; begin // Test si l'application n'a pas déjà été exécuté. try FHandle := CreateFileMapping( $FFFFFFFF, nil, PAGE_READWRITE, 0, 4, PChar(ApplicationName)); if FHandle = 0 then abort; FFileView := MapViewOfFile( FHandle, FILE_MAP_WRITE, 0, 0, 4 ); if FFileView = nil then abort; Hndl := PHandle(FFileView)^; if Hndl <> 0 then begin if IsIconic(Hndl) then begin ShowWindow(Hndl, SW_RESTORE); end; SetForegroundWindow(Hndl); Halt(0); end else integer(FFileView^) := Application.Handle; except end; end; procedure Register; begin {Register standard components} RegisterComponents('Exemples',[TRunOnce]); end; end.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionprogram Project1; uses Forms, windows, Unit1 in 'Unit1.pas' {Form1}; {$R *.RES} begin SetLastError(NO_ERROR); CreateMutex (nil, False, 'MonMutexProject1'); if GetLastError<>ERROR_ALREADY_EXISTS then begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end; end.