Windows: Win32 migration tools code review
authorMichael Schuster <michael@schuster.ms>
Wed, 9 Sep 2020 19:01:38 +0000 (21:01 +0200)
committerMichael Schuster <michael@schuster.ms>
Mon, 21 Sep 2020 14:40:19 +0000 (16:40 +0200)
Signed-off-by: Michael Schuster <michael@schuster.ms>
20 files changed:
admin/win/tools/CMakeLists.txt
admin/win/tools/NCMsiHelper/CMakeLists.txt
admin/win/tools/NCMsiHelper/CustomAction.cpp
admin/win/tools/NCMsiHelper/LogResult.cpp
admin/win/tools/NCMsiHelper/NCMsiHelper.cpp
admin/win/tools/NCMsiHelper/NCMsiHelper.h
admin/win/tools/NCNavRemove/ConfigIni.cpp
admin/win/tools/NCNavRemove/ConfigIni.h
admin/win/tools/NCNavRemove/dll/NavRemove.cpp
admin/win/tools/NCNavRemove/dll/NavRemove.h
admin/win/tools/NCNavRemove/dll/dllmain.cpp
admin/win/tools/NCNavRemove/exe/main.cpp
admin/win/tools/NCToolsShared/CMakeLists.txt
admin/win/tools/NCToolsShared/NCTools.h [deleted file]
admin/win/tools/NCToolsShared/SimpleMutex.cpp [deleted file]
admin/win/tools/NCToolsShared/SimpleMutex.h [deleted file]
admin/win/tools/NCToolsShared/SimpleNamedMutex.cpp [new file with mode: 0644]
admin/win/tools/NCToolsShared/SimpleNamedMutex.h [new file with mode: 0644]
admin/win/tools/NCToolsShared/utility.h
admin/win/tools/NCToolsShared/utility_win.cpp

index 7f1058af40175253916f7e6f27221db7e8196e79..934e6b15cf44ec1e9cb89c1f64bc98f6e2aaa74d 100644 (file)
@@ -22,37 +22,35 @@ add_definitions(-D_WINDOWS)
 add_definitions(-D_WIN32_WINNT=0x0601)
 add_definitions(-DWINVER=0x0601)
 
-if(MSVC)
-    # Use automatic overload for suitable CRT safe-functions
-    # See https://docs.microsoft.com/de-de/cpp/c-runtime-library/security-features-in-the-crt?view=vs-2019
-    add_definitions(-D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1)
-    # Also: Disable compiler warnings because we don't use Windows CRT safe-functions explicitly and don't intend to
-    # as this is a pure cross-platform source the only alternative would be a ton of ifdefs with calls to the _s version
-    add_definitions(-D_CRT_SECURE_NO_WARNINGS)
+# Use automatic overload for suitable CRT safe-functions
+# See https://docs.microsoft.com/de-de/cpp/c-runtime-library/security-features-in-the-crt?view=vs-2019
+add_definitions(-D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1)
+# Also: Disable compiler warnings because we don't use Windows CRT safe-functions explicitly and don't intend to
+# as this is a pure cross-platform source the only alternative would be a ton of ifdefs with calls to the _s version
+add_definitions(-D_CRT_SECURE_NO_WARNINGS)
 
-    # Optimize for size
-    set(COMPILER_FLAGS "/GL /O1 /sdl /Zc:inline /Oi /EHsc /nologo")
-    set(LINKER_FLAGS "/LTCG /OPT:REF /SUBSYSTEM:WINDOWS /NOLOGO")
+# Optimize for size
+set(COMPILER_FLAGS "/GL /O1 /sdl /Zc:inline /Oi /EHsc /nologo")
+set(LINKER_FLAGS "/LTCG /OPT:REF /SUBSYSTEM:WINDOWS /NOLOGO")
 
-    # Enable DEP, ASLR and CFG
-    set(LINKER_FLAGS "${LINKER_FLAGS} /nxcompat /dynamicbase /guard:cf")
+# Enable DEP, ASLR and CFG
+set(LINKER_FLAGS "${LINKER_FLAGS} /nxcompat /dynamicbase /guard:cf")
 
-    # x86 only: Enable SafeSEH
-    if(CMAKE_SIZEOF_VOID_P MATCHES 4)
-        set(LINKER_FLAGS "${LINKER_FLAGS} /safeseh")
-    endif()
+# x86 only: Enable SafeSEH
+if(CMAKE_SIZEOF_VOID_P MATCHES 4)
+    set(LINKER_FLAGS "${LINKER_FLAGS} /safeseh")
+endif()
 
-    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILER_FLAGS}")
-    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILER_FLAGS}")
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILER_FLAGS}")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILER_FLAGS}")
 
-    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
-    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
+set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
+set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
 
-    # Use static runtime for all subdirectories
-    foreach(buildType "" "_DEBUG" "_MINSIZEREL" "_RELEASE" "_RELWITHDEBINFO")
-        string(REPLACE "/MD" "/MT" "CMAKE_CXX_FLAGS${buildType}" "${CMAKE_CXX_FLAGS${buildType}}")
-    endforeach()
-endif()
+# Use static runtime for all subdirectories
+foreach(buildType "" "_DEBUG" "_MINSIZEREL" "_RELEASE" "_RELWITHDEBINFO")
+    string(REPLACE "/MD" "/MT" "CMAKE_CXX_FLAGS${buildType}" "${CMAKE_CXX_FLAGS${buildType}}")
+endforeach()
 
 add_subdirectory(NCToolsShared)
 
index d28d595815f3a7452553dcd6e962f40127c22820..484b454222da9ce33c35c751b8a8574988f73ff7 100644 (file)
@@ -7,18 +7,12 @@ else()
     message(STATUS "WiX Toolset SDK path: ${WIX_SDK_PATH}")
 endif()
 
-include_directories(
-    ${WIX_SDK_PATH}/inc
-)
+include_directories(${WIX_SDK_PATH}/inc)
 
 if(CMAKE_SIZEOF_VOID_P MATCHES 4)
-    link_directories(
-        ${WIX_SDK_PATH}/lib/x86
-    )
+    link_directories(${WIX_SDK_PATH}/lib/x86)
 else()
-    link_directories(
-        ${WIX_SDK_PATH}/lib/x64
-    )
+    link_directories(${WIX_SDK_PATH}/lib/x64)
 endif()
 
 add_definitions(-D_NCMSIHELPER_EXPORTS)
index f222a264f52676710acabc729c055f0b2b0d474d..e3d63dec18bfdd2bc50f4c6bc24f0e0924899081 100644 (file)
@@ -19,7 +19,6 @@
  * 
  */
 
-#include "NCTools.h"
 #include "NCMsiHelper.h"
 
 /**
  */
 UINT CustomActionArgcArgv(MSIHANDLE hInstall, CUSTOM_ACTION_ARGC_ARGV func, LPCSTR actionName)
 {
-       HRESULT hr = S_OK;
-       UINT er = ERROR_SUCCESS;
-    LPWSTR pszCustomActionData = nullptr;
-    int argc = 0;
-    LPWSTR *argv = nullptr;
+    LPWSTR pszCustomActionData = nullptr, *argv = nullptr;
 
-       hr = WcaInitialize(hInstall, actionName);
-       ExitOnFailure(hr, "Failed to initialize");
+    HRESULT hr = WcaInitialize(hInstall, actionName);
+    ExitOnFailure(hr, "Failed to initialize");
 
-       WcaLog(LOGMSG_STANDARD, "Initialized.");
+    WcaLog(LOGMSG_STANDARD, "Initialized.");
     
     // Retrieve our custom action property. This is one of
     // only three properties we can request on a Deferred
     // Custom Action.  So, we assume the caller puts all
     // parameters in this one property.
-    pszCustomActionData = nullptr;
     hr = WcaGetProperty(L"CustomActionData", &pszCustomActionData);
     ExitOnFailure(hr, "Failed to get Custom Action Data.");
     WcaLog(LOGMSG_STANDARD, "Custom Action Data = '%ls'.", pszCustomActionData);
@@ -71,9 +65,9 @@ UINT CustomActionArgcArgv(MSIHANDLE hInstall, CUSTOM_ACTION_ARGC_ARGV func, LPCS
     // Convert the string retrieved into a standard argc/arg layout
     // (ignoring the fact that the first parameter is whatever was
     // passed, not necessarily the application name/path).
+    int argc = 0;
     argv = CommandLineToArgvW(pszCustomActionData, &argc);
-    if (argv)
-    {
+    if (argv) {
         hr = HRESULT_FROM_WIN32(GetLastError());
         ExitOnFailure(hr, "Failed to convert Custom Action Data to argc/argv.");
     }
@@ -87,8 +81,7 @@ LExit:
     if (argv)
         LocalFree(argv);
 
-       er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
-       return WcaFinalize(er);
+    return WcaFinalize(SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE);
 }
 
 UINT __stdcall ExecNsisUninstaller(MSIHANDLE hInstall)
@@ -105,21 +98,21 @@ UINT __stdcall RemoveNavigationPaneEntries(MSIHANDLE hInstall)
  * DllMain - Initialize and cleanup WiX custom action utils.
  */
 extern "C" BOOL WINAPI DllMain(
-       __in HINSTANCE hInst,
-       __in ULONG ulReason,
-       __in LPVOID
-       )
+    __in HINSTANCE hInst,
+    __in ULONG ulReason,
+    __in LPVOID
+    )
 {
-       switch(ulReason)
-       {
-       case DLL_PROCESS_ATTACH:
-               WcaGlobalInitialize(hInst);
-               break;
+    switch(ulReason)
+    {
+    case DLL_PROCESS_ATTACH:
+        WcaGlobalInitialize(hInst);
+        break;
 
-       case DLL_PROCESS_DETACH:
-               WcaGlobalFinalize();
-               break;
-       }
+    case DLL_PROCESS_DETACH:
+        WcaGlobalFinalize();
+        break;
+    }
 
-       return TRUE;
+    return TRUE;
 }
index 5aff88b31d095af2f4dc102fcc44bd33e06d2d29..b9f8f03085f8a07a1faba441716868798cd5e887 100644 (file)
@@ -19,7 +19,6 @@
  * 
  */
 
-#include "NCTools.h"
 #include "NCMsiHelper.h"
 
 //
@@ -64,14 +63,14 @@ void LogError(DWORD dwErrorMsgId)
             hInst, // Handle to the DLL.
             dwMessageId, // Message identifier.
             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
-            (LPSTR)&pBuffer, // Buffer that will hold the text string.
+            reinterpret_cast<LPSTR>(&pBuffer), // Buffer that will hold the text string.
             ERRMSGBUFFERSIZE, // Allocate at least this many chars for pBuffer.
             nullptr // No insert values.
         );
     }
 
     if (0 < ret && nullptr != pBuffer) {
-        pMessage = (LPSTR)pBuffer;
+        pMessage = static_cast<LPSTR>(pBuffer);
     }
 
     // Display the string.
@@ -102,21 +101,20 @@ void LogResult(
     va_start(args, fmt);
 #pragma warning(push)
 #pragma warning(disable : 4996)
-    auto len = _vsnprintf(nullptr, 0, fmt, args) + 1;
+    const auto len = _vsnprintf(nullptr, 0, fmt, args) + 1;
 #pragma warning(pop)
-    auto buffer = (char*)malloc(len * sizeof(char));
+    auto buffer = static_cast<char*>(malloc(len * sizeof(char)));
 
 #ifdef _DEBUG
     ::ZeroMemory(buffer, len);
 #endif // _DEBUG
-    _vsnprintf_s(buffer, len, len-1, fmt, args);
+    _vsnprintf_s(buffer, len, len - 1, fmt, args);
 
     // (MSDN code complete)
 
     // Now that the buffer holds the formatted string, send it to
     // the appropriate output.
-    if (WcaIsInitialized())
-    {
+    if (WcaIsInitialized()) {
         if (FAILED(hr)) {
             WcaLogError(hr, buffer);
             LogError(hr);
@@ -124,8 +122,7 @@ void LogResult(
             WcaLog(LOGMSG_STANDARD, buffer);
         }
     } else { // Log to stdout/stderr
-        if (FAILED(hr))
-        {
+        if (FAILED(hr)) {
             fprintf_s(stderr, "%s\n", buffer);
             LogError(hr);
         } else {
index 2cdbb59d10011c13b31cdafcdebeca2766c6fb01..68b99584bbff477205a4c67caf96a61579c55c6d 100644 (file)
  * for more details.
  */
 
-#include "NCTools.h"
+#include "NCMsiHelper.h"
 #include "utility.h"
 #include "LogResult.h"
-#include "NCMsiHelper.h"
 
 using namespace NCTools;
 
@@ -25,8 +24,8 @@ HRESULT NCMSIHELPER_API DoExecNsisUninstaller(int argc, LPWSTR *argv)
         return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
     }
 
-    auto appShortName = std::wstring(argv[0]);
-    auto uninstallExePath = std::wstring(argv[1]);
+    const auto appShortName = std::wstring(argv[0]);
+    const auto uninstallExePath = std::wstring(argv[1]);
 
     if (appShortName.empty()
          || uninstallExePath.empty()) {
@@ -34,7 +33,7 @@ HRESULT NCMSIHELPER_API DoExecNsisUninstaller(int argc, LPWSTR *argv)
     }
 
     auto appInstallDir = uninstallExePath;
-    auto posLastSlash = appInstallDir.find_last_of(PathSeparator);
+    const auto posLastSlash = appInstallDir.find_last_of(PathSeparator);
     if (posLastSlash != std::wstring::npos) {
         appInstallDir.erase(posLastSlash);
     } else {
@@ -42,7 +41,7 @@ HRESULT NCMSIHELPER_API DoExecNsisUninstaller(int argc, LPWSTR *argv)
     }
 
     // Run uninstaller
-    std::wstring cmd = L'\"' + uninstallExePath + L"\" /S _?=" + appInstallDir;
+    const std::wstring cmd = L'\"' + uninstallExePath + L"\" /S _?=" + appInstallDir;
     LogResult(S_OK, "Running '%ls'.", cmd.data());
     Utility::execCmd(cmd);
 
@@ -69,7 +68,7 @@ HRESULT NCMSIHELPER_API DoRemoveNavigationPaneEntries(int argc, LPWSTR *argv)
         return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
     }
 
-    auto appName = std::wstring(argv[0]);
+    const auto appName = std::wstring(argv[0]);
 
     if (appName.empty()) {
         return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
index 53e3d20f4d39d1950b0615ea5537f9b1b7c13023..05c9ba43f484beeb75afadcdbb1d5487d86cb529 100644 (file)
@@ -33,6 +33,8 @@
  */
 #pragma once
 
+#include <windows.h>
+
 #ifdef _NCMSIHELPER_EXPORTS
 #  pragma comment (lib, "newdev")
 #  pragma comment (lib, "setupapi")
 #  pragma comment (lib, "wcautil")
 #  pragma comment (lib, "Version")
 
+#  include <cstdlib>
+#  include <string>
+#  include <tchar.h>
 #  include <msiquery.h>
-#  include <stdlib.h>
 #  include <lmerr.h>
 
 // WiX Header Files:
@@ -92,5 +96,4 @@ HRESULT NCMSIHELPER_API DoRemoveNavigationPaneEntries(int argc, LPWSTR *argv);
  *  As a result, all functions defined in this header should
  *  conform to this function prototype.
  */
-typedef HRESULT NCMSIHELPER_API (*CUSTOM_ACTION_ARGC_ARGV)(
-    int argc, LPWSTR *argv);
+using CUSTOM_ACTION_ARGC_ARGV = NCMSIHELPER_API HRESULT(*)(int argc, LPWSTR *argv);
index 6bb56508e77c525e5798f76de7253a4ddab0920d..b1f0e9799e6fbe67b0d2407848a8ee6e8cfe74c6 100644 (file)
@@ -12,7 +12,7 @@
  * for more details.
  */
 
-#include "NCTools.h"
+#include <windows.h>
 #include "3rdparty/SimpleIni.h"
 #include "NavRemoveConstants.h"
 #include "ConfigIni.h"
@@ -23,8 +23,7 @@ ConfigIni::ConfigIni()
 
 bool ConfigIni::load()
 {
-    std::wstring filename;
-    DWORD bufferLen = GetCurrentDirectory(0, nullptr);
+    const DWORD bufferLen = GetCurrentDirectory(0, nullptr);
     TCHAR *pszBuffer = nullptr;
 
     if (bufferLen == 0) {
@@ -36,10 +35,11 @@ bool ConfigIni::load()
         return false;
     }
 
+    std::wstring filename;
     if (GetCurrentDirectory(bufferLen, pszBuffer) != 0) {
         filename = pszBuffer;
     }
-    delete [] pszBuffer;
+    delete[] pszBuffer;
 
     if (filename.empty()) {
         return false;
@@ -52,18 +52,17 @@ bool ConfigIni::load()
     const wchar_t iniSection[] = CFG_KEY;
     const wchar_t iniKey[] = CFG_VAR_APPNAME;
 
-    auto rc = ini.LoadFile(filename.data());
+    const auto rc = ini.LoadFile(filename.data());
 
     if (rc != SI_OK) {
         return false;
     }
 
-    auto pv = ini.GetValue(iniSection, iniKey);
+    const auto pv = ini.GetValue(iniSection, iniKey);
     bool success = false;
 
     if (pv) {
         _appName = pv;
-        
         success = !_appName.empty();
     }
  
@@ -72,7 +71,7 @@ bool ConfigIni::load()
     return success;
 }
 
-const std::wstring ConfigIni::getAppName() const
+std::wstring ConfigIni::getAppName() const
 {
     return _appName;
 }
index 7ee6a92bd92af9d051029b49360dfd7b12d1a76b..b78fcad42e0b97b5ddd260df44e3418ca52b0dae 100644 (file)
@@ -14,7 +14,7 @@
 
 #pragma once
 
-#include "NCTools.h"
+#include <string>
 
 class ConfigIni
 {
@@ -23,7 +23,7 @@ public:
 
     bool load();
 
-    const std::wstring getAppName() const;
+    std::wstring getAppName() const;
 
 private:
     std::wstring _appName;
index 414d191d78a82f38d4299b83200cd176657846e2..0e6945a00905db445bd94a1d7ebe98dbf3d64464 100644 (file)
@@ -12,7 +12,7 @@
  * for more details.
  */
 
-#include "NCTools.h"
+#include <windows.h>
 #include "utility.h"
 #include "NavRemove.h"
 #include "../ConfigIni.h"
index 92e55348fea7cf17e9071cd2f1c39f0194e9687f..a83fe28e59a8b4ce43dca9c601ea6529f2100272 100644 (file)
@@ -14,7 +14,7 @@
 
 #pragma once
 
-#include "NCTools.h"
+#include <windows.h>
 
 // The following ifdef block is the standard way of creating macros which make exporting
 // from a DLL simpler. All files within this DLL are compiled with the _NAVREMOVE_EXPORTS
index 303158f208bea84c268b08b3cc9f7f3ff9c14389..c57f5444bcfb138513a70c89d2202005818c83bb 100644 (file)
  * for more details.
  */
 
-#include "NCTools.h"
-#include "SimpleMutex.h"
+#include <windows.h>
+#include "SimpleNamedMutex.h"
 #include "NavRemoveConstants.h"
 
-SimpleMutex g_mutex;
+SimpleNamedMutex g_mutex(std::wstring(MUTEX_NAME));
 bool g_alreadyRunning = false;
 
 extern "C" BOOL WINAPI DllMain(
-       __in HINSTANCE hInst,
-       __in ULONG ulReason,
-       __in LPVOID
-       )
+    __in HINSTANCE hInst,
+    __in ULONG ulReason,
+    __in LPVOID
+    )
 {
-       switch(ulReason)
-       {
-       case DLL_PROCESS_ATTACH:
+    switch(ulReason)
+    {
+    case DLL_PROCESS_ATTACH:
         // Mutex
-        g_alreadyRunning = !g_mutex.create(std::wstring(MUTEX_NAME));
-               break;
+        g_alreadyRunning = !g_mutex.lock();
+        break;
 
-       case DLL_PROCESS_DETACH:
+    case DLL_PROCESS_DETACH:
         // Release mutex
-        g_mutex.release();
-               break;
-       }
+        g_mutex.unlock();
+        break;
+    }
 
-       return TRUE;
+    return TRUE;
 }
index 649e979e43221393030bc56c79a1ba5bce0019a8..0e55d9e18312c79a2f2306c3536fc2303cb4e872 100644 (file)
@@ -12,9 +12,9 @@
  * for more details.
  */
 
-#include "NCTools.h"
+#include <windows.h>
 #include "utility.h"
-#include "SimpleMutex.h"
+#include "SimpleNamedMutex.h"
 #include "NavRemoveConstants.h"
 #include "../ConfigIni.h"
 
@@ -31,9 +31,9 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
     UNREFERENCED_PARAMETER(nCmdShow);
 
     // Mutex
-    SimpleMutex mutex;
+    SimpleNamedMutex mutex(std::wstring(MUTEX_NAME));
 
-    if (!mutex.create(std::wstring(MUTEX_NAME))) {
+    if (!mutex.lock()) {
         return 0;
     }
 
@@ -47,7 +47,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
     Utility::removeNavigationPaneEntries(ini.getAppName());
 
     // Release mutex
-    mutex.release();
+    mutex.unlock();
 
     return 0;
 }
index 61429fdf6d219f843f8ea27fec300f96b7dbe2e2..f5a323330038396f37c8227b2edbad253e547e12 100644 (file)
@@ -1,4 +1,4 @@
 add_library(NCToolsShared STATIC
     utility_win.cpp
-    SimpleMutex.cpp
+    SimpleNamedMutex.cpp
 )
diff --git a/admin/win/tools/NCToolsShared/NCTools.h b/admin/win/tools/NCToolsShared/NCTools.h
deleted file mode 100644 (file)
index 87f724f..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-// NCTools.h : include file for standard system include files
-//
-
-#pragma once
-
-#include <WinSDKVer.h>
-
-// // Including SDKDDKVer.h defines the highest available Windows platform.
-// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
-// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
-#include <SDKDDKVer.h>
-
-#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
-
-// Windows Header Files
-#include <windows.h>
-#include <shellapi.h>
-#include <Shlobj.h>
-#include <psapi.h>
-#include <wincred.h>
-
-// C RunTime Header Files
-#include <cstdlib>
-#include <malloc.h>
-#include <memory.h>
-#include <tchar.h>
-#include <algorithm>
-#include <functional>
-#include <string>
-#include <vector>
-#include <variant>
diff --git a/admin/win/tools/NCToolsShared/SimpleMutex.cpp b/admin/win/tools/NCToolsShared/SimpleMutex.cpp
deleted file mode 100644 (file)
index 9f216e7..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (C) by Michael Schuster <michael.schuster@nextcloud.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * for more details.
- */
-
-#include "NCTools.h"
-#include "SimpleMutex.h"
-
-SimpleMutex::SimpleMutex()
-{
-}
-
-bool SimpleMutex::create(const std::wstring &name)
-{
-    release();
-
-    // Mutex
-    _hMutex = CreateMutex(nullptr, TRUE, name.data());
-
-    if (GetLastError() == ERROR_ALREADY_EXISTS) {
-        CloseHandle(_hMutex);
-        _hMutex = nullptr;
-        return false;
-    }
-
-    return true;
-}
-
-void SimpleMutex::release()
-{
-    // Release mutex
-    if (_hMutex) {
-        ReleaseMutex(_hMutex);
-        CloseHandle(_hMutex);
-        _hMutex = nullptr;
-    }
-}
diff --git a/admin/win/tools/NCToolsShared/SimpleMutex.h b/admin/win/tools/NCToolsShared/SimpleMutex.h
deleted file mode 100644 (file)
index 3a8bf21..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (C) by Michael Schuster <michael.schuster@nextcloud.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * for more details.
- */
-
-#pragma once
-
-#include "NCTools.h"
-
-class SimpleMutex
-{
-public:
-    SimpleMutex();
-
-    bool create(const std::wstring &name);
-    void release();
-
-private:
-    HANDLE _hMutex = nullptr;
-};
diff --git a/admin/win/tools/NCToolsShared/SimpleNamedMutex.cpp b/admin/win/tools/NCToolsShared/SimpleNamedMutex.cpp
new file mode 100644 (file)
index 0000000..d636c81
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) by Michael Schuster <michael.schuster@nextcloud.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include "SimpleNamedMutex.h"
+
+SimpleNamedMutex::SimpleNamedMutex(const std::wstring &name)
+{
+    _name = name;
+}
+
+bool SimpleNamedMutex::lock()
+{
+    if (_name.empty() || _hMutex) {
+        return false;
+    }
+
+    // Mutex
+    _hMutex = CreateMutex(nullptr, TRUE, _name.data());
+
+    if (GetLastError() == ERROR_ALREADY_EXISTS) {
+        CloseHandle(_hMutex);
+        _hMutex = nullptr;
+        return false;
+    }
+
+    return true;
+}
+
+void SimpleNamedMutex::unlock()
+{
+    // Release mutex
+    if (_hMutex) {
+        ReleaseMutex(_hMutex);
+        CloseHandle(_hMutex);
+        _hMutex = nullptr;
+    }
+}
diff --git a/admin/win/tools/NCToolsShared/SimpleNamedMutex.h b/admin/win/tools/NCToolsShared/SimpleNamedMutex.h
new file mode 100644 (file)
index 0000000..ed57f78
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) by Michael Schuster <michael.schuster@nextcloud.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#pragma once
+
+#include <windows.h>
+#include <string>
+
+class SimpleNamedMutex
+{
+public:
+    SimpleNamedMutex(const std::wstring &name);
+
+    bool lock();
+    void unlock();
+
+private:
+    std::wstring _name;
+    HANDLE _hMutex = nullptr;
+};
index 2120967364521af3062918375060a3ef01c2dc16..0ab0a561d6ae062e3de9b3d7f996a02ff29e7476 100644 (file)
 
 #pragma once
 
-#include "NCTools.h"
+#include <windows.h>
+#include <string>
+#include <vector>
+#include <variant>
+#include <functional>
 
 namespace NCTools {
 
index c6bc279e7d79b393e9db9b74b0f7de4bb9531920..eb42318d948acd340affa282f22eabc89ab79b19 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <cassert>
-#include "NCTools.h"
 #include "utility.h"
+#include <cassert>
+#include <algorithm>
+#include <Shlobj.h>
+#include <psapi.h>
 
 #define ASSERT assert
 #define Q_ASSERT assert