From 22c9627b84623a8c7bedef808ada4d8d4106f6a3 Mon Sep 17 00:00:00 2001 From: William JCM Date: Wed, 28 Jul 2021 15:17:46 +0200 Subject: [PATCH] Main: add single-instance checking. --- src/main.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 944e581..ca38b71 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,25 @@ #include +#include +#include +#include + int main(int argc, char** argv) { + void* mutex_handle = CreateMutexW(nullptr, 0, L"MassBuilderSaveTool"); + + if(!mutex_handle) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error initialising the app", + "There was an error initialising the mutex.",nullptr); + return EXIT_FAILURE; + } + + if(GetLastError() == ERROR_ALREADY_EXISTS) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error initialising the app", + "There can be only one running instance of the application.",nullptr); + return EXIT_FAILURE; + } + #ifndef SAVETOOL_DEBUG_BUILD std::ofstream output{"SaveToolLog.txt", std::ios::trunc|std::ios::out}; @@ -28,5 +46,9 @@ int main(int argc, char** argv) { #endif SaveTool app({argc, argv}); - return app.exec(); + Int result = app.exec(); + + ReleaseMutex(mutex_handle); + + return result; }