2重起動を防止する のバックアップ(No.1)
- バックアップ一覧
- ソース を表示
- 2重起動を防止する は削除されています。
- 1 (2005-10-05 21:19:54 (水))
- 2 (2005-10-06 22:50:37 (木))
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変数などは使用できないので注意が必要。