From 1ffb69e1ce0e375b6dfe1198ddd3a13a112736f5 Mon Sep 17 00:00:00 2001 From: lx-s Date: Tue, 19 Dec 2023 18:57:15 +0100 Subject: [PATCH] New function to launch the browser --- WinMute/Utility.cpp | 40 ++++++++++++++++++++++++++++++ WinMute/Utility.h | 59 +++++++++++++++++++++++++++++++++++++++++++++ WinMute/common.h | 21 +--------------- 3 files changed, 100 insertions(+), 20 deletions(-) create mode 100644 WinMute/Utility.h diff --git a/WinMute/Utility.cpp b/WinMute/Utility.cpp index a99cbe1..e5a227c 100644 --- a/WinMute/Utility.cpp +++ b/WinMute/Utility.cpp @@ -33,6 +33,19 @@ POSSIBILITY OF SUCH DAMAGE. #include "Common.h" +// ========================================================================= +// Types +// ========================================================================= + +struct WINDOWPROCESSINFO { + DWORD pid; + HWND hWnd; +}; + +// ========================================================================= +// ShowWindowsError +// ========================================================================= + void ShowWindowsError(const wchar_t *functionName, DWORD lastError) { // Retrieve the system error message for the last-error code @@ -66,6 +79,10 @@ void ShowWindowsError(const wchar_t *functionName, DWORD lastError) LocalFree(lpMsgBuf); } +// ========================================================================= +// GetWinMuteversion +// ========================================================================= + bool GetWinMuteVersion(std::wstring &versNumber) { DWORD unused; @@ -92,6 +109,10 @@ bool GetWinMuteVersion(std::wstring &versNumber) return true; } +// ========================================================================= +// String Conversion +// ========================================================================= + std::wstring ConvertStringToWideString(const std::string &ansiString) { std::wstring unicodeString; @@ -117,3 +138,22 @@ std::string ConvertWideStringToString(const std::wstring &wideString) ansiStringSize = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wideString.c_str(), -1, &ansiString[0], ansiStringSize, "?", nullptr); return ansiString; } + +// ========================================================================= +// Launch Browser +// ========================================================================= + +bool LaunchBrowser(HWND hParent, const std::wstring &url) +{ + SHELLEXECUTEINFOW sxi = { 0 }; + sxi.cbSize = sizeof(sxi); + sxi.nShow = SW_SHOWNORMAL; + sxi.hwnd = hParent; + sxi.lpVerb = L"open"; + sxi.lpFile = url.c_str(); + if (!ShellExecuteExW(&sxi)) { + WMLog::GetInstance().LogWinError(L"ShellExecuteEx", GetLastError()); + return false; + } + return true; +} diff --git a/WinMute/Utility.h b/WinMute/Utility.h new file mode 100644 index 0000000..0473e21 --- /dev/null +++ b/WinMute/Utility.h @@ -0,0 +1,59 @@ +/* + WinMute + Copyright (c) 2023, Alexander Steinhoefer + +----------------------------------------------------------------------------- +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +----------------------------------------------------------------------------- +*/ + +#pragma once + +#include +#include + +// ============================================================================= +// Utility + +void ShowWindowsError(const wchar_t *functionName, DWORD lastError = -1); +bool GetWinMuteVersion(std::wstring &versNumber); + +std::wstring ConvertStringToWideString(const std::string &ansiString); +std::string ConvertWideStringToString(const std::wstring &wideString); + +bool LaunchBrowser(HWND hParent, const std::wstring &url); + +// ============================================================================= +// COM Helper +template +inline void SafeRelease(Interface **ppInterfaceToRelease) +{ + if (*ppInterfaceToRelease) { + (*ppInterfaceToRelease)->Release(); + (*ppInterfaceToRelease) = nullptr; + } +} diff --git a/WinMute/common.h b/WinMute/common.h index 824c61e..8238b7a 100644 --- a/WinMute/common.h +++ b/WinMute/common.h @@ -115,6 +115,7 @@ namespace fs = std::filesystem; #include "BluetoothDetector.h" #include "QuietHoursTimer.h" #include "WinMute.h" +#include "Utility.h" #include "VersionHelper.h" // ============================================================================= @@ -130,23 +131,3 @@ static const wchar_t *LOG_FILE_NAME = L"WinMute.log"; constexpr int WM_SAVESETTINGS = WM_USER + 300; constexpr int WM_WINMUTE_UPDATE_POPUP = WM_USER + 301; - -// ============================================================================= -// Utility - -void ShowWindowsError(const wchar_t *functionName, DWORD lastError = -1); -bool GetWinMuteVersion(std::wstring &versNumber); - -std::wstring ConvertStringToWideString(const std::string &ansiString); -std::string ConvertWideStringToString(const std::wstring &wideString); - -// ============================================================================= -// COM Helper -template -inline void SafeRelease(Interface * *ppInterfaceToRelease) -{ - if (*ppInterfaceToRelease) { - (*ppInterfaceToRelease)->Release(); - (*ppInterfaceToRelease) = nullptr; - } -} -- 2.43.0