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)
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)
*
*/
-#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);
// 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.");
}
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)
* 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;
}
*
*/
-#include "NCTools.h"
#include "NCMsiHelper.h"
//
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.
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);
WcaLog(LOGMSG_STANDARD, buffer);
}
} else { // Log to stdout/stderr
- if (FAILED(hr))
- {
+ if (FAILED(hr)) {
fprintf_s(stderr, "%s\n", buffer);
LogError(hr);
} else {
* for more details.
*/
-#include "NCTools.h"
+#include "NCMsiHelper.h"
#include "utility.h"
#include "LogResult.h"
-#include "NCMsiHelper.h"
using namespace NCTools;
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()) {
}
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 {
}
// 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);
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);
*/
#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:
* 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);
* for more details.
*/
-#include "NCTools.h"
+#include <windows.h>
#include "3rdparty/SimpleIni.h"
#include "NavRemoveConstants.h"
#include "ConfigIni.h"
bool ConfigIni::load()
{
- std::wstring filename;
- DWORD bufferLen = GetCurrentDirectory(0, nullptr);
+ const DWORD bufferLen = GetCurrentDirectory(0, nullptr);
TCHAR *pszBuffer = nullptr;
if (bufferLen == 0) {
return false;
}
+ std::wstring filename;
if (GetCurrentDirectory(bufferLen, pszBuffer) != 0) {
filename = pszBuffer;
}
- delete [] pszBuffer;
+ delete[] pszBuffer;
if (filename.empty()) {
return false;
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();
}
return success;
}
-const std::wstring ConfigIni::getAppName() const
+std::wstring ConfigIni::getAppName() const
{
return _appName;
}
#pragma once
-#include "NCTools.h"
+#include <string>
class ConfigIni
{
bool load();
- const std::wstring getAppName() const;
+ std::wstring getAppName() const;
private:
std::wstring _appName;
* for more details.
*/
-#include "NCTools.h"
+#include <windows.h>
#include "utility.h"
#include "NavRemove.h"
#include "../ConfigIni.h"
#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
* 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;
}
* for more details.
*/
-#include "NCTools.h"
+#include <windows.h>
#include "utility.h"
-#include "SimpleMutex.h"
+#include "SimpleNamedMutex.h"
#include "NavRemoveConstants.h"
#include "../ConfigIni.h"
UNREFERENCED_PARAMETER(nCmdShow);
// Mutex
- SimpleMutex mutex;
+ SimpleNamedMutex mutex(std::wstring(MUTEX_NAME));
- if (!mutex.create(std::wstring(MUTEX_NAME))) {
+ if (!mutex.lock()) {
return 0;
}
Utility::removeNavigationPaneEntries(ini.getAppName());
// Release mutex
- mutex.release();
+ mutex.unlock();
return 0;
}
add_library(NCToolsShared STATIC
utility_win.cpp
- SimpleMutex.cpp
+ SimpleNamedMutex.cpp
)
+++ /dev/null
-// 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>
+++ /dev/null
-/*
- * 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;
- }
-}
+++ /dev/null
-/*
- * 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;
-};
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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;
+};
#pragma once
-#include "NCTools.h"
+#include <windows.h>
+#include <string>
+#include <vector>
+#include <variant>
+#include <functional>
namespace NCTools {
* 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