From e7e576ad29a14a157521ca0fab71baffcba9d0c1 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sun, 15 Apr 2018 11:19:31 +0500 Subject: [PATCH] UI: Improve startup checks. --- src/ui/main.cpp | 17 ++++++++++++++--- src/ui/util/osutil.cpp | 3 ++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ui/main.cpp b/src/ui/main.cpp index eeda0dfc..c07ae81a 100644 --- a/src/ui/main.cpp +++ b/src/ui/main.cpp @@ -1,4 +1,5 @@ #include +#include #include "../common/version.h" #include "driver/drivermanager.h" @@ -7,6 +8,9 @@ #include "fortsettings.h" #include "util/osutil.h" +#define FORT_ERROR_INSTANCE 1 +#define FORT_ERROR_DEVICE 2 + int main(int argc, char *argv[]) { QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); @@ -28,13 +32,20 @@ int main(int argc, char *argv[]) } // To check running instance - OsUtil::createGlobalMutex(APP_NAME); + if (!OsUtil::createGlobalMutex(APP_NAME)) { + QMessageBox::critical(nullptr, QString(), + "Application is already running!"); + return FORT_ERROR_INSTANCE; + } FortManager fortManager(&fortSettings); // Error: Cannot open the driver device - if (!fortManager.driverManager()->isDeviceOpened()) - return 1; + if (!fortManager.driverManager()->isDeviceOpened()) { + QMessageBox::critical(nullptr, QString(), + "Cannot open the driver device!"); + return FORT_ERROR_DEVICE; + } fortManager.showTrayIcon(); diff --git a/src/ui/util/osutil.cpp b/src/ui/util/osutil.cpp index 32e88e73..e317384f 100644 --- a/src/ui/util/osutil.cpp +++ b/src/ui/util/osutil.cpp @@ -18,7 +18,8 @@ QString OsUtil::pidToPath(quint32 pid, bool isKernelPath) bool OsUtil::createGlobalMutex(const char *name) { - return !CreateMutexA(nullptr, FALSE, name); + return CreateMutexA(nullptr, FALSE, name) + && GetLastError() != ERROR_ALREADY_EXISTS; } quint32 OsUtil::lastErrorCode()