From 392a3eb775141cb44c5b72f71a3908cf08623fab Mon Sep 17 00:00:00 2001 From: Ryandw11 <6239385+ryandw11@users.noreply.github.com> Date: Wed, 15 Dec 2021 17:03:50 -0700 Subject: [PATCH] Prevent multiple instances of the program. --- AutoClickerDL/AutoClickerDL.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/AutoClickerDL/AutoClickerDL.c b/AutoClickerDL/AutoClickerDL.c index fcabbac..3fd0623 100644 --- a/AutoClickerDL/AutoClickerDL.c +++ b/AutoClickerDL/AutoClickerDL.c @@ -53,6 +53,13 @@ LRESULT CALLBACK SettingsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam // Main method. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { + // Prevent more than 1 instance of the program from running. + HANDLE mutexHandle = CreateMutex(NULL, TRUE, L"com_ryandw11_autoclickerdl"); + if (GetLastError() == ERROR_ALREADY_EXISTS) { + MessageBox(NULL, L"AutoClickerDL is already running! Please close the exisiting instance of the program before opening it again.", L"AutoClickerDL Already Open", MB_OK | MB_ICONERROR); + return 0; + } + // Class name of the window. const wchar_t CLASS_NAME[] = L"AutoClickerDL Window Class"; @@ -210,6 +217,9 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine DispatchMessage(&msg); } + ReleaseMutex(mutexHandle); + CloseHandle(mutexHandle); + return 0; }