| Код | program Project1;
uses Forms, Windows, SysUtils, ShellApi, Unit1 in 'Unit1.pas' {Form1};
{$R *.res} const DestDir = 'c:\program files\test';
var hMutexOneInstance: THandle; AlreadyRunning: Boolean; dwWaitResult: Dword; DestName: AnsiString; begin hMutexOneInstance := CreateMutex(nil, True, 'some_exclusive_tag'); { если лень придумывать, можно Ctrl-Shift-G нажать и оттуда строчку взять } AlreadyRunning := ((GetLastError() = ERROR_ALREADY_EXISTS) or (GetLastError() = ERROR_ACCESS_DENIED)); if (AlreadyRunning) then begin dwWaitResult := WaitForSingleObject(hMutexOneInstance, 5000); if ((dwWaitResult = WAIT_TIMEOUT) or (dwWaitResult = WAIT_ABANDONED)) then begin MessageBox(0, 'Error', 'Timeout', MB_OK); CloseHandle(hMutexOneInstance); Halt(1); end; end; DestName := DestDir + '\' + ExtractFileName(Application.ExeName); if (LowerCase(Application.ExeName) <> LowerCase(DestName)) then begin if (not FileExists(DestName) or (FileExists(DestName) and (FileAge(Application.ExeName) > FileAge(DestName)))) then begin if not DirectoryExists(DestDir) then CreateDir(DestDir); CopyFile(PChar(Application.ExeName), PChar(DestName), false); end; ShellExecute(0, nil, PChar(DestName), nil, DestDir, SW_SHOWNORMAL); if (hMutexOneInstance <> 0) then ReleaseMutex(hMutexOneInstance); CloseHandle(hMutexOneInstance); Halt(0); end; Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; if (hMutexOneInstance <> 0) then ReleaseMutex(hMutexOneInstance); CloseHandle(hMutexOneInstance); end.
|
ps. непривычно дельфей писать... 
|