From 720f511be457380f4f58ebe0418653eee14c279a Mon Sep 17 00:00:00 2001 From: lx-s Date: Fri, 4 Dec 2020 10:20:18 +0100 Subject: [PATCH] Added Proxy Process for Screensaver Notification. --- ScreensaverNotify/ScreensaverNotify.cpp | 2 +- ScreensaverNotify/ScreensaverNotify.vcxproj | 10 +- .../ScreensaverNotify32.vcxproj | 172 ++++++++++++++++++ .../ScreensaverNotify32.vcxproj.filters | 19 ++ .../ScreensaverNotify32.vcxproj.user | 4 + .../ScreensaverNotifierProxy.cpp | 114 ++++++++++++ ScreensaverProxy32/ScreensaverNotifierProxy.h | 64 +++++++ ScreensaverProxy32/ScreensaverProxy.cpp | 140 ++++++++++++++ ScreensaverProxy32/ScreensaverProxy32.vcxproj | 163 +++++++++++++++++ .../ScreensaverProxy32.vcxproj.filters | 22 +++ .../ScreensaverProxy32.vcxproj.user | 19 ++ WinMute.sln | 20 ++ WinMute/ScreensaverNotifier.cpp | 78 +++++++- WinMute/ScreensaverNotifier.h | 2 + WinMute/Utility.cpp | 2 +- WinMute/WinMute.vcxproj | 2 + WinMute/WinMute.vcxproj.user | 16 ++ 17 files changed, 836 insertions(+), 13 deletions(-) create mode 100644 ScreensaverNotify32/ScreensaverNotify32.vcxproj create mode 100644 ScreensaverNotify32/ScreensaverNotify32.vcxproj.filters create mode 100644 ScreensaverNotify32/ScreensaverNotify32.vcxproj.user create mode 100644 ScreensaverProxy32/ScreensaverNotifierProxy.cpp create mode 100644 ScreensaverProxy32/ScreensaverNotifierProxy.h create mode 100644 ScreensaverProxy32/ScreensaverProxy.cpp create mode 100644 ScreensaverProxy32/ScreensaverProxy32.vcxproj create mode 100644 ScreensaverProxy32/ScreensaverProxy32.vcxproj.filters create mode 100644 ScreensaverProxy32/ScreensaverProxy32.vcxproj.user diff --git a/ScreensaverNotify/ScreensaverNotify.cpp b/ScreensaverNotify/ScreensaverNotify.cpp index 207983e..9c09475 100644 --- a/ScreensaverNotify/ScreensaverNotify.cpp +++ b/ScreensaverNotify/ScreensaverNotify.cpp @@ -1,6 +1,6 @@ /* ScreensaverNotify DLL - Copyright (c) 2017, Alexander Steinhoefer + Copyright (c) 2020, Alexander Steinhoefer ----------------------------------------------------------------------------- Redistribution and use in source and binary forms, with or without diff --git a/ScreensaverNotify/ScreensaverNotify.vcxproj b/ScreensaverNotify/ScreensaverNotify.vcxproj index fdcdc92..8a8b1f5 100644 --- a/ScreensaverNotify/ScreensaverNotify.vcxproj +++ b/ScreensaverNotify/ScreensaverNotify.vcxproj @@ -31,12 +31,14 @@ true v142 Unicode + Spectre DynamicLibrary true v142 Unicode + Spectre DynamicLibrary @@ -115,7 +117,7 @@ NotUsing Level4 Disabled - WIN32;_DEBUG;_WINDOWS;_USRDLL;WINMUTEDLL_EXPORTS;%(PreprocessorDefinitions) + WINMUTEDLL_EXPORTS;WIN32;_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) true false true @@ -132,7 +134,7 @@ NotUsing Level4 Disabled - WIN32;_DEBUG;_WINDOWS;_USRDLL;WINMUTEDLL_EXPORTS;%(PreprocessorDefinitions) + WINMUTEDLL_EXPORTS;WIN32;_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) true false true @@ -151,7 +153,7 @@ MaxSpeed true true - WIN32;NDEBUG;_WINDOWS;_USRDLL;WINMUTEDLL_EXPORTS;%(PreprocessorDefinitions) + WINMUTEDLL_EXPORTS;WIN32;NDEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) true true MultiThreadedDLL @@ -176,7 +178,7 @@ MaxSpeed true true - WIN32;NDEBUG;_WINDOWS;_USRDLL;WINMUTEDLL_EXPORTS;%(PreprocessorDefinitions) + WINMUTEDLL_EXPORTS;WIN32;NDEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) true true MultiThreadedDLL diff --git a/ScreensaverNotify32/ScreensaverNotify32.vcxproj b/ScreensaverNotify32/ScreensaverNotify32.vcxproj new file mode 100644 index 0000000..5ea3435 --- /dev/null +++ b/ScreensaverNotify32/ScreensaverNotify32.vcxproj @@ -0,0 +1,172 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {2e31ff98-3cc4-4b3a-8bb1-2efde1331c9a} + ScreensaverNotify32 + 10.0 + + + + DynamicLibrary + true + v142 + Unicode + Spectre + + + DynamicLibrary + false + v142 + true + Unicode + Spectre + + + DynamicLibrary + true + v142 + Unicode + Spectre + + + DynamicLibrary + false + v142 + true + Unicode + Spectre + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir).buildtmp\$(Configuration)\$(PlatformShortName)\$(ProjectName)\ + $(SolutionDir)Bin\x64\ + + + false + $(SolutionDir).buildtmp\$(Configuration)\$(PlatformShortName)\$(ProjectName)\ + $(SolutionDir)Bin\x64\ + + + true + $(SolutionDir).buildtmp\$(Configuration)\$(PlatformShortName)\$(ProjectName)\ + $(SolutionDir)Bin\x64\ + + + false + $(SolutionDir).buildtmp\$(Configuration)\$(PlatformShortName)\$(ProjectName)\ + $(SolutionDir)Bin\x64\ + + + + Level4 + true + WINMUTEDLL_EXPORTS;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Caret + + + Console + true + + + + + Level4 + true + true + true + WINMUTEDLL_EXPORTS;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Caret + + + Console + true + true + true + + + copy "$(TargetPath)" "$(SolutionDir)dist\bin\$(PlatformShortName)\$(TargetFileName)" + + + + + Level4 + true + WINMUTEDLL_EXPORTS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Caret + + + Console + true + + + + + Level4 + true + true + true + WINMUTEDLL_EXPORTS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Caret + + + Console + true + true + true + + + copy "$(TargetPath)" "$(SolutionDir)dist\bin\$(PlatformShortName)\$(TargetFileName)" + + + + + + + + + + + + \ No newline at end of file diff --git a/ScreensaverNotify32/ScreensaverNotify32.vcxproj.filters b/ScreensaverNotify32/ScreensaverNotify32.vcxproj.filters new file mode 100644 index 0000000..4aab079 --- /dev/null +++ b/ScreensaverNotify32/ScreensaverNotify32.vcxproj.filters @@ -0,0 +1,19 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + + + Source Files + + + + + Source Files + + + \ No newline at end of file diff --git a/ScreensaverNotify32/ScreensaverNotify32.vcxproj.user b/ScreensaverNotify32/ScreensaverNotify32.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/ScreensaverNotify32/ScreensaverNotify32.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ScreensaverProxy32/ScreensaverNotifierProxy.cpp b/ScreensaverProxy32/ScreensaverNotifierProxy.cpp new file mode 100644 index 0000000..9393717 --- /dev/null +++ b/ScreensaverProxy32/ScreensaverNotifierProxy.cpp @@ -0,0 +1,114 @@ +/* + WinMute + Copyright (c) 2020, 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. +----------------------------------------------------------------------------- +*/ + +#include "ScreensaverNotifierProxy.h" + +extern void PrintWindowsError(const char* lpszFunction, DWORD lastError = -1); + +ScreensaverNotifierProxy::ScreensaverNotifierProxy() : + isRegistered_(false), + hookDll_(nullptr), + hNotifyWnd_(nullptr), + notifyWndMsg_(0), + regHook_(nullptr), + unregHook_(nullptr) +{ } + +ScreensaverNotifierProxy::~ScreensaverNotifierProxy() +{ + Unload(); +} + +bool ScreensaverNotifierProxy::Init(int msgId) +{ + if (hookDll_ != nullptr) { + return true; + } + notifyWndMsg_ = msgId; + return InitNotifyWnd() && InitHookDll() && ActivateNotifications(); +} + +void ScreensaverNotifierProxy::Unload() +{ + if (hookDll_) { + if (isRegistered_ && unregHook_) { + unregHook_(); + } + + FreeLibrary(hookDll_); + regHook_ = nullptr; + unregHook_ = nullptr; + hookDll_ = nullptr; + } +} + +bool ScreensaverNotifierProxy::InitHookDll() +{ + if (hookDll_ == nullptr) { + hookDll_ = LoadLibrary("ScreensaverNotify32"); + if (hookDll_ == nullptr) { + PrintWindowsError("LoadLibrary"); + } else { + regHook_ = reinterpret_cast( + GetProcAddress(hookDll_, "RegisterScreensaverHook")); + unregHook_ = reinterpret_cast( + GetProcAddress(hookDll_, "UnregisterScreensaverHook")); + if (regHook_ == nullptr || unregHook_ == nullptr) { + MessageBox(0, "MISSING", "", 0); + Unload(); + } + } + } + return hookDll_ != nullptr; +} + +bool ScreensaverNotifierProxy::InitNotifyWnd() +{ + hNotifyWnd_ = FindWindowEx(0, 0, "LXS_WinMuteScreensaverNotifyClass", nullptr); + if (hNotifyWnd_ == nullptr) { + auto lastError = GetLastError(); + PrintWindowsError("FindWindowEx", lastError); + } + return hNotifyWnd_ != nullptr; +} + +bool ScreensaverNotifierProxy::ActivateNotifications() +{ + if (regHook_ != nullptr) { + if (regHook_(hNotifyWnd_, notifyWndMsg_)) { + isRegistered_ = true; + return true; + } + } + return false; +} diff --git a/ScreensaverProxy32/ScreensaverNotifierProxy.h b/ScreensaverProxy32/ScreensaverNotifierProxy.h new file mode 100644 index 0000000..aca076c --- /dev/null +++ b/ScreensaverProxy32/ScreensaverNotifierProxy.h @@ -0,0 +1,64 @@ +/* + WinMute + Copyright (c) 2020, 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 + +class ScreensaverNotifierProxy { +public: + ScreensaverNotifierProxy(); + ~ScreensaverNotifierProxy(); + + ScreensaverNotifierProxy(const ScreensaverNotifierProxy&) = delete; + ScreensaverNotifierProxy& operator= (const ScreensaverNotifierProxy&) = delete; + + bool Init(int msgId); + void Unload(); +private: + typedef int(*RegisterHook)(HWND, UINT); + typedef void(*UnregisterHook)(void); + + bool isRegistered_; + + HINSTANCE hookDll_; + HWND hNotifyWnd_; + UINT notifyWndMsg_; + + RegisterHook regHook_; + UnregisterHook unregHook_; + + bool InitHookDll(); + bool InitNotifyWnd(); + bool ActivateNotifications(); +}; diff --git a/ScreensaverProxy32/ScreensaverProxy.cpp b/ScreensaverProxy32/ScreensaverProxy.cpp new file mode 100644 index 0000000..a55206d --- /dev/null +++ b/ScreensaverProxy32/ScreensaverProxy.cpp @@ -0,0 +1,140 @@ +/* + WinMute + Copyright (c) 2020, 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. +----------------------------------------------------------------------------- +*/ + +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include +#include + +#include "ScreensaverNotifierProxy.h" + +static HINSTANCE hGlobInstance_ = nullptr; +static int notifyMsgId_ = 0; + +void PrintWindowsError(const char* lpszFunction, DWORD lastError = -1); + +void PrintWindowsError(const char* lpszFunction, DWORD lastError) +{ + // Retrieve the system error message for the last-error code + if (lastError == -1) { + lastError = GetLastError(); + } + + LPVOID lpMsgBuf; + if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, lastError, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast(&lpMsgBuf), 0, nullptr) != 0) { + size_t displayBufSize = + ((size_t)lstrlen(static_cast(lpMsgBuf)) + + (size_t)lstrlen(static_cast(lpszFunction)) + + 40) * sizeof(TCHAR); + // Display the error message and exit the process + LPVOID lpDisplayBuf = reinterpret_cast( + LocalAlloc(LMEM_ZEROINIT, displayBufSize)); + if (lpDisplayBuf) { + StringCchPrintf((LPTSTR)lpDisplayBuf, + LocalSize(lpDisplayBuf), + _T("%s failed with error %u: %s"), + lpszFunction, + lastError, + reinterpret_cast(lpMsgBuf)); + MessageBox(nullptr, static_cast(lpDisplayBuf), + "WinMute - ScreensaverProxy", MB_OK | MB_ICONERROR); + LocalFree(lpDisplayBuf); + } + LocalFree(lpMsgBuf); + } +} + +static bool ParseCommandLine(std::string cmdLine) +{ + auto pos = cmdLine.find("/msgId:"); + if (pos != std::string::npos) { + auto msgId = cmdLine.substr(pos + strlen("/msgId:")); + try { + notifyMsgId_ = std::stoi(msgId); + if (notifyMsgId_ != 0) { + return true; + } + } catch (...) { + return false; + } + } + return false; +} + +int WINAPI WinMain( + _In_ HINSTANCE hInstance, + _In_opt_ HINSTANCE, + _In_ LPSTR, + _In_ int) +{ + hGlobInstance_ = hInstance; + + auto cmdLine = GetCommandLineA(); + if (!ParseCommandLine(cmdLine)) { + MessageBox(0, "Wrong parameters", "WinMute Screensaver Proxy", MB_ICONERROR); + return FALSE; + } + HANDLE hMutex = CreateMutex(nullptr, TRUE, _T("LxSystemsWinMuteScreensaverProxy32")); + if (hMutex == nullptr) { + return FALSE; + } + if (GetLastError() == ERROR_ALREADY_EXISTS) { + ReleaseMutex(hMutex); + return FALSE; + } + + ScreensaverNotifierProxy notifyProxy; + if (!notifyProxy.Init(notifyMsgId_)) { + MessageBox(0, "Failed to init Screensaver Notify Proxy", "WinMute", MB_ICONERROR); + return FALSE; + } + + MSG msg = { 0 }; + while (GetMessage(&msg, nullptr, 0, 0)) { + if (msg.message == WM_QUIT) { + break; + } + } + ReleaseMutex(hMutex); + notifyProxy.Unload(); + + return static_cast(msg.wParam); +} diff --git a/ScreensaverProxy32/ScreensaverProxy32.vcxproj b/ScreensaverProxy32/ScreensaverProxy32.vcxproj new file mode 100644 index 0000000..1d86c28 --- /dev/null +++ b/ScreensaverProxy32/ScreensaverProxy32.vcxproj @@ -0,0 +1,163 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {b3023843-584d-4dbb-9041-19e1f8b6f13e} + ScreensaverProxy32 + 10.0 + + + + Application + true + v142 + MultiByte + Spectre + + + Application + false + v142 + true + MultiByte + Spectre + + + Application + true + v142 + MultiByte + Spectre + + + Application + false + v142 + true + MultiByte + Spectre + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)Bin\x64\ + $(SolutionDir).buildtmp\$(Configuration)\$(PlatformShortName)\$(ProjectName)\ + + + false + $(SolutionDir)Bin\x64\ + $(SolutionDir).buildtmp\$(Configuration)\$(PlatformShortName)\$(ProjectName)\ + + + true + $(SolutionDir)Bin\x64\ + $(SolutionDir).buildtmp\$(Configuration)\$(PlatformShortName)\$(ProjectName)\ + + + false + $(SolutionDir)Bin\x64\ + $(SolutionDir).buildtmp\$(Configuration)\$(PlatformShortName)\$(ProjectName)\ + + + + Level4 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + Level4 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + Level4 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + Level4 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/ScreensaverProxy32/ScreensaverProxy32.vcxproj.filters b/ScreensaverProxy32/ScreensaverProxy32.vcxproj.filters new file mode 100644 index 0000000..3b59de4 --- /dev/null +++ b/ScreensaverProxy32/ScreensaverProxy32.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + + + Source Files + + + Source Files + + + + + Source Files + + + \ No newline at end of file diff --git a/ScreensaverProxy32/ScreensaverProxy32.vcxproj.user b/ScreensaverProxy32/ScreensaverProxy32.vcxproj.user new file mode 100644 index 0000000..8376c54 --- /dev/null +++ b/ScreensaverProxy32/ScreensaverProxy32.vcxproj.user @@ -0,0 +1,19 @@ + + + + /msgId:1234 + WindowsLocalDebugger + + + /msgId:1234 + WindowsLocalDebugger + + + /msgId:1234 + WindowsLocalDebugger + + + /msgId:1234 + WindowsLocalDebugger + + \ No newline at end of file diff --git a/WinMute.sln b/WinMute.sln index 5da6b48..4bcb7d5 100644 --- a/WinMute.sln +++ b/WinMute.sln @@ -10,6 +10,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinMute", "WinMute\WinMute. EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ScreensaverNotify", "ScreensaverNotify\ScreensaverNotify.vcxproj", "{A48C241D-70F5-4919-BE06-8C5BE99CCC23}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ScreensaverNotify32", "ScreensaverNotify32\ScreensaverNotify32.vcxproj", "{2E31FF98-3CC4-4B3A-8BB1-2EFDE1331C9A}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ScreensaverProxy32", "ScreensaverProxy32\ScreensaverProxy32.vcxproj", "{B3023843-584D-4DBB-9041-19E1F8B6F13E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -34,6 +38,22 @@ Global {A48C241D-70F5-4919-BE06-8C5BE99CCC23}.Release|Win32.Build.0 = Release|Win32 {A48C241D-70F5-4919-BE06-8C5BE99CCC23}.Release|x64.ActiveCfg = Release|x64 {A48C241D-70F5-4919-BE06-8C5BE99CCC23}.Release|x64.Build.0 = Release|x64 + {2E31FF98-3CC4-4B3A-8BB1-2EFDE1331C9A}.Debug|Win32.ActiveCfg = Debug|Win32 + {2E31FF98-3CC4-4B3A-8BB1-2EFDE1331C9A}.Debug|Win32.Build.0 = Debug|Win32 + {2E31FF98-3CC4-4B3A-8BB1-2EFDE1331C9A}.Debug|x64.ActiveCfg = Debug|Win32 + {2E31FF98-3CC4-4B3A-8BB1-2EFDE1331C9A}.Debug|x64.Build.0 = Debug|Win32 + {2E31FF98-3CC4-4B3A-8BB1-2EFDE1331C9A}.Release|Win32.ActiveCfg = Release|Win32 + {2E31FF98-3CC4-4B3A-8BB1-2EFDE1331C9A}.Release|Win32.Build.0 = Release|Win32 + {2E31FF98-3CC4-4B3A-8BB1-2EFDE1331C9A}.Release|x64.ActiveCfg = Release|Win32 + {2E31FF98-3CC4-4B3A-8BB1-2EFDE1331C9A}.Release|x64.Build.0 = Release|Win32 + {B3023843-584D-4DBB-9041-19E1F8B6F13E}.Debug|Win32.ActiveCfg = Debug|Win32 + {B3023843-584D-4DBB-9041-19E1F8B6F13E}.Debug|Win32.Build.0 = Debug|Win32 + {B3023843-584D-4DBB-9041-19E1F8B6F13E}.Debug|x64.ActiveCfg = Debug|Win32 + {B3023843-584D-4DBB-9041-19E1F8B6F13E}.Debug|x64.Build.0 = Debug|Win32 + {B3023843-584D-4DBB-9041-19E1F8B6F13E}.Release|Win32.ActiveCfg = Release|Win32 + {B3023843-584D-4DBB-9041-19E1F8B6F13E}.Release|Win32.Build.0 = Release|Win32 + {B3023843-584D-4DBB-9041-19E1F8B6F13E}.Release|x64.ActiveCfg = Release|Win32 + {B3023843-584D-4DBB-9041-19E1F8B6F13E}.Release|x64.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/WinMute/ScreensaverNotifier.cpp b/WinMute/ScreensaverNotifier.cpp index 0c1aa92..fb26980 100644 --- a/WinMute/ScreensaverNotifier.cpp +++ b/WinMute/ScreensaverNotifier.cpp @@ -40,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. extern HINSTANCE hglobInstance; -static LPCTSTR SCRSVER_NOTIFY_WND_CLASS = _T("ScreensaverNotify"); +static LPCTSTR SCRSVER_NOTIFY_WND_CLASS = _T("LXS_WinMuteScreensaverNotifyClass"); #define SCRSV_TIMER_ID 1 @@ -78,6 +78,7 @@ ScreensaverNotifier::ScreensaverNotifier() : hookDll_(nullptr), hWnd_(nullptr), hNotifyWnd_(nullptr), + hJob_(nullptr), hookWndMsg_(0), regHook_(nullptr), unregHook_(nullptr) @@ -98,9 +99,10 @@ bool ScreensaverNotifier::Init() return true; } return RegisterWindowClass() && - InitWindow() && - InitWindowMessage() && - InitHookDll(); + InitWindow() && + InitWindowMessage() && + InitHookDll() && + InitHook32(); } bool ScreensaverNotifier::InitWindow() @@ -108,10 +110,10 @@ bool ScreensaverNotifier::InitWindow() hWnd_ = CreateWindowEx( WS_EX_TOOLWINDOW, SCRSVER_NOTIFY_WND_CLASS, - nullptr, + _T("LXS_WinMute_ScreensaverNotify"), WS_POPUP, 0, 0, 0, 0, - HWND_MESSAGE, + NULL, 0, hglobInstance, this); @@ -119,6 +121,7 @@ bool ScreensaverNotifier::InitWindow() PrintWindowsError(_T("CreateWindow")); return false; } + ShowWindow(hWnd_, SW_HIDE); return true; } @@ -132,6 +135,64 @@ bool ScreensaverNotifier::InitWindowMessage() return true; } +bool ScreensaverNotifier::InitHook32() +{ + bool success = false; +#ifdef _UNICODE + wchar_t commandLine[MAX_PATH + 1]; + swprintf_s(commandLine, L"/msgId:%d", hookWndMsg_); +#else + char commandLine[MAX_PATH + 1]; + sprintf_s(commandLine, "/msgId:%d", hookWndMsg_)); +#endif + + JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo; + ZeroMemory(&jobInfo, sizeof(jobInfo)); + jobInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + + STARTUPINFO startupInfo; + PROCESS_INFORMATION procInfo; + ZeroMemory(&startupInfo, sizeof(startupInfo)); + ZeroMemory(&procInfo, sizeof(procInfo)); + + hJob_ = CreateJobObject(NULL, NULL); + if (hJob_ == nullptr) { + PrintWindowsError(_T("CreateJobObject"), GetLastError()); + } else if (SetInformationJobObject( + hJob_, + JobObjectExtendedLimitInformation, + &jobInfo, + sizeof(jobInfo)) == FALSE) { + PrintWindowsError(_T("SetInformationJobObject"), GetLastError()); + CloseHandle(hJob_); + } else if (CreateProcess( + _T(".\\ScreensaverProxy32.exe"), + commandLine, + NULL, + NULL, + FALSE, + NORMAL_PRIORITY_CLASS, + NULL, + NULL, + &startupInfo, + &procInfo) == FALSE) { + PrintWindowsError(_T("CreateProcess")); + CloseHandle(hJob_); + hJob_ = nullptr; + } else { + if (AssignProcessToJobObject(hJob_, procInfo.hProcess) == FALSE) { + PrintWindowsError(_T("AssignProcessToJobObject"), GetLastError()); + CloseHandle(hJob_); + hJob_ = nullptr; + } else { + success = true; + } + CloseHandle(procInfo.hProcess); + CloseHandle(procInfo.hThread); + } + return success; +} + bool ScreensaverNotifier::InitHookDll() { if (hookDll_ == nullptr) { @@ -149,7 +210,6 @@ bool ScreensaverNotifier::InitHookDll() } } return hookDll_ != nullptr; - } bool ScreensaverNotifier::RegisterWindowClass() @@ -179,6 +239,10 @@ void ScreensaverNotifier::Unload() unregHook_ = nullptr; hookDll_ = nullptr; } + if (hJob_ != nullptr) { + CloseHandle(hJob_); + hJob_ = nullptr; + } } /* ========================================================================== */ diff --git a/WinMute/ScreensaverNotifier.h b/WinMute/ScreensaverNotifier.h index b2da60f..b9cb093 100644 --- a/WinMute/ScreensaverNotifier.h +++ b/WinMute/ScreensaverNotifier.h @@ -65,6 +65,7 @@ private: HINSTANCE hookDll_; HWND hWnd_, hNotifyWnd_; + HANDLE hJob_; UINT hookWndMsg_; RegisterHook regHook_; @@ -74,6 +75,7 @@ private: bool IsScreensaverRunning(); + bool InitHook32(); bool InitHookDll(); bool RegisterWindowClass(); bool InitWindow(); diff --git a/WinMute/Utility.cpp b/WinMute/Utility.cpp index a667308..1176ed4 100644 --- a/WinMute/Utility.cpp +++ b/WinMute/Utility.cpp @@ -57,7 +57,7 @@ void PrintWindowsError(LPTSTR lpszFunction, DWORD lastError) if (lpDisplayBuf) { StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf), - _T("%s failed with error %ul: %s"), + _T("%s failed with error %u: %s"), lpszFunction, lastError, reinterpret_cast(lpMsgBuf)); diff --git a/WinMute/WinMute.vcxproj b/WinMute/WinMute.vcxproj index 069a150..a2c96a3 100644 --- a/WinMute/WinMute.vcxproj +++ b/WinMute/WinMute.vcxproj @@ -43,11 +43,13 @@ Application Unicode v142 + Spectre Application Unicode v142 + Spectre diff --git a/WinMute/WinMute.vcxproj.user b/WinMute/WinMute.vcxproj.user index ace9a86..d7f1df8 100644 --- a/WinMute/WinMute.vcxproj.user +++ b/WinMute/WinMute.vcxproj.user @@ -1,3 +1,19 @@  + + $(OutputPath) + WindowsLocalDebugger + + + $(OutputPath) + WindowsLocalDebugger + + + $(OutputPath) + WindowsLocalDebugger + + + $(OutputPath) + WindowsLocalDebugger + \ No newline at end of file -- 2.43.0