; 多重起動禁止テストその2 ; ; 二重起動の防止(1) - Advanced HSP ; http://chokuto.ifdef.jp/advanced/singleton1.html ; ; 2023/11/30, Windows10 x64 22H2 + HSP 3.7 beta 7 #packopt name "test_createmutex_b" ; filename #packopt type 0 ; generate ".exe" ; 多重起動をチェックするためのミューテックスオブジェクト名を定義。 ; アプリ固有の名前であること。 #define MUTEX_NAME "HSP_WinAPI_Test_Mutex" ; ---------------------------------------- ; アプリケーションの起動チェックを行うモジュール #module #uselib "kernel32.dll" #cfunc CreateMutex "CreateMutexA" int, int, sptr #cfunc GetLastError "GetLastError" #func CloseHandle "CloseHandle" int #define ERROR_ALREADY_EXISTS 183 ; アプリがすでに起動されているか取得する関数 #defcfunc AlreadyAppRunning str name if (hMutex == 0) { ; 名前付きミューテックスオブジェクトの作成 hMutex = CreateMutex(0, 0, name) ; オブジェクトがすでに作成されていたかどうかの判別 if (GetLastError() == ERROR_ALREADY_EXISTS) { ; すでに同じ名前のオブジェクトが存在する alreadyRunning = 1 } else { ; オブジェクトが新しく作成された alreadyRunning = 0 } } return alreadyRunning ; クリーンアップ処理(終了時に自動実行) #deffunc CleanupAppRunChecker onexit if (hMutex != 0) { ; ミューテックスオブジェクトハンドルのクローズ CloseHandle hMutex hMutex = 0 } return ; ---------------------------------------- #global *start if (AlreadyAppRunning(MUTEX_NAME)) { dialog "すでに起動されています。" end } mes "二重起動してしません。" stop