From dbeec07673dfa2d4666f3c012e67a7e83597484d Mon Sep 17 00:00:00 2001 From: Dongle Date: Thu, 17 Nov 2022 17:49:47 +0700 Subject: [PATCH] Extend frame after successful hooking. --- ExplorerFrame/ExplorerFrame.cpp | 67 +++++++++++++++++++++++++---- ExplorerFrame/ExplorerFrame.h | 2 + ExplorerFrame/ExplorerFrame.vcxproj | 3 ++ ExplorerFrame/framework.h | 3 +- 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/ExplorerFrame/ExplorerFrame.cpp b/ExplorerFrame/ExplorerFrame.cpp index 03a57d2..95cbed6 100644 --- a/ExplorerFrame/ExplorerFrame.cpp +++ b/ExplorerFrame/ExplorerFrame.cpp @@ -143,6 +143,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) } catch (...) {} } + PostQuitMessage(0); break; } @@ -185,6 +186,36 @@ void WinEventProc( HandleWindow(hwnd); } +bool IsWindowOfInterest(HWND hWnd) +{ + if (!IsWindow(hWnd) || !IsWindowVisible(hWnd)) + return false; + + WCHAR lpClassName[MAX_PATH]; + + GetClassName(hWnd, lpClassName, MAX_PATH); + + auto styleEx = GetWindowLongPtrW(hWnd, GWL_EXSTYLE); + + if (styleEx & WS_EX_NOACTIVATE || styleEx & WS_EX_LAYERED) + return false; + + if (styleEx & WS_EX_APPWINDOW) + return true; + + auto style = GetWindowLongPtrW(hWnd, GWL_STYLE); + + if (!style) + return false; + + auto hasTitleBar = style & WS_BORDER && style & WS_DLGFRAME; + + if ((styleEx & WS_EX_TOOLWINDOW || style & WS_POPUP) && !hasTitleBar) + return false; + + return true; +} + void HandleWindow(HWND hwnd) { DWORD processId; @@ -196,15 +227,33 @@ void HandleWindow(HWND hwnd) CloseHandle(processHandle); // Check if the process is the Windows Explorer - if (_wcsicmp(processName, L"C:\\Windows\\explorer.exe") == 0 && hookingMap.find(tid) == hookingMap.end()) + if (_wcsicmp(processName, L"C:\\Windows\\explorer.exe") == 0) { - HMODULE dll = LoadLibrary(L"Injector.dll"); - if (!dll) - return; - auto procAddr = GetProcAddress(dll, "InjectExplorerHook"); - auto method = reinterpret_cast(procAddr); - HHOOK hook = method(hwnd); - hookingMap.insert(std::pair(tid, hook)); - FreeLibrary(dll); + bool isInjectionSuccessful = true; + if (hookingMap.find(tid) == hookingMap.end()) + { + HMODULE dll = LoadLibrary(L"Injector.dll"); + if (!dll) + return; + auto procAddr = GetProcAddress(dll, "InjectExplorerHook"); + auto method = reinterpret_cast(procAddr); + HHOOK hook = method(hwnd); + if (hook != NULL) + hookingMap.insert(std::pair(tid, hook)); + else + isInjectionSuccessful = false; + FreeLibrary(dll); + } + + if (IsWindowOfInterest(hwnd) && isInjectionSuccessful) + { + MARGINS margins = MARGINS { + .cxLeftWidth = -1, + .cxRightWidth = -1, + .cyTopHeight = -1, + .cyBottomHeight = -1 + }; + DwmExtendFrameIntoClientArea(hwnd, &margins); + } } } \ No newline at end of file diff --git a/ExplorerFrame/ExplorerFrame.h b/ExplorerFrame/ExplorerFrame.h index 3388861..33dc903 100644 --- a/ExplorerFrame/ExplorerFrame.h +++ b/ExplorerFrame/ExplorerFrame.h @@ -22,6 +22,8 @@ void WinEventProc( DWORD dwmsEventTime); void HandleWindow(HWND hwnd); +bool IsWindowOfInterest(HWND); + // Define SetPreferredAppMode typedef enum PREFERRED_APP_MODE { diff --git a/ExplorerFrame/ExplorerFrame.vcxproj b/ExplorerFrame/ExplorerFrame.vcxproj index 2a02ef4..cff23a2 100644 --- a/ExplorerFrame/ExplorerFrame.vcxproj +++ b/ExplorerFrame/ExplorerFrame.vcxproj @@ -74,6 +74,9 @@ stdcpp20 + + dwmapi.lib;%(AdditionalDependencies) + diff --git a/ExplorerFrame/framework.h b/ExplorerFrame/framework.h index 02e3b43..7da1fc2 100644 --- a/ExplorerFrame/framework.h +++ b/ExplorerFrame/framework.h @@ -15,4 +15,5 @@ #include #include #include -#include \ No newline at end of file +#include +#include \ No newline at end of file