2重起動を防止する †
2重起動を防止するにはプロジェクトソースに次のコードを記述する。
プロジェクトソースは [プロジェクト] - [ソース表示] で 表示できる。
program Sample;
uses
Forms,
Windows,
Messages,
Unit1 in 'Unit1.pas' {Form1},
{$R *.RES}
const
MutexName = 'Sample Mutex'; //Mutexを識別するユニークな名称
var
hMutex: THANDLE;
hWndPrev, hWndApp: HWND;
begin
(*---- 二重起動を防止する処理 ----*)
hMutex := OpenMutex(MUTEX_ALL_ACCESS, False, MutexName);
if hMutex <> 0 then begin
(*---- すでに Mutex が登録されている ----*)
CloseHandle(hMutex);
hWndPrev := FindWindow('TForm1', nil); //メインフォームを探す
if hWndPrev <> 0 then begin
(*---- 起動済みのフォームを表示させる ----*)
SetForegroundWindow(hWndPrev);
hWndApp := GetWindowLong(hWndPrev, GWL_HWNDPARENT);
if (hWndApp <> 0) and IsIconic(hWndApp) then begin
SendMessage(hWndApp, WM_SYSCOMMAND, SC_RESTORE, -1);
end;
end;
Exit;
end;
CreateMutex(nil, False, MutexName);
(*---- ここまで ----*)
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
ReleaseMutex(hMutex); //作成したMutexを開放する。
end.
2重起動を防止するコードはアプリケーションの初期化前なので、 Application変数などは使用できないので注意が必要。
Counter: 6117,
today: 1,
yesterday: 2
Last-modified: Sun, 05 Feb 2006 16:19:37 JST (7286d)
Last-modified: Sun, 05 Feb 2006 16:19:37 JST (7286d)
