Skip to content

Commit

Permalink
Architecture and code prettifying.
Browse files Browse the repository at this point in the history
  • Loading branch information
dongle-the-gadget committed Nov 11, 2022
1 parent 9f0057c commit 7468fb3
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 207 deletions.
20 changes: 10 additions & 10 deletions ExplorerFrame.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Injector", "Injector\Inject
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|ARM64 = Release|ARM64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B0911B85-3398-4A47-B535-9814519C37BC}.Debug|ARM64.ActiveCfg = Debug|ARM64
{B0911B85-3398-4A47-B535-9814519C37BC}.Debug|ARM64.Build.0 = Debug|ARM64
{B0911B85-3398-4A47-B535-9814519C37BC}.Debug|x64.ActiveCfg = Debug|x64
{B0911B85-3398-4A47-B535-9814519C37BC}.Debug|x64.Build.0 = Debug|x64
{B0911B85-3398-4A47-B535-9814519C37BC}.Debug|x86.ActiveCfg = Debug|Win32
{B0911B85-3398-4A47-B535-9814519C37BC}.Debug|x86.Build.0 = Debug|Win32
{B0911B85-3398-4A47-B535-9814519C37BC}.Release|ARM64.ActiveCfg = Release|ARM64
{B0911B85-3398-4A47-B535-9814519C37BC}.Release|ARM64.Build.0 = Release|ARM64
{B0911B85-3398-4A47-B535-9814519C37BC}.Release|x64.ActiveCfg = Release|x64
{B0911B85-3398-4A47-B535-9814519C37BC}.Release|x64.Build.0 = Release|x64
{B0911B85-3398-4A47-B535-9814519C37BC}.Release|x86.ActiveCfg = Release|Win32
{B0911B85-3398-4A47-B535-9814519C37BC}.Release|x86.Build.0 = Release|Win32
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Debug|ARM64.ActiveCfg = Debug|ARM64
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Debug|ARM64.Build.0 = Debug|ARM64
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Debug|x64.ActiveCfg = Debug|x64
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Debug|x64.Build.0 = Debug|x64
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Debug|x86.ActiveCfg = Debug|Win32
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Debug|x86.Build.0 = Debug|Win32
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Release|ARM64.ActiveCfg = Release|ARM64
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Release|ARM64.Build.0 = Release|ARM64
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Release|x64.ActiveCfg = Release|x64
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Release|x64.Build.0 = Release|x64
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Release|x86.ActiveCfg = Release|Win32
{AF25E190-DFAE-4D35-8D91-970E952BD305}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
272 changes: 130 additions & 142 deletions ExplorerFrame/ExplorerFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@
#include "..\Injector\WndProcExport.h"

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

hUxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
hUxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);

MyRegisterClass(hInstance);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

MSG msg;
MSG msg;

// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int) msg.wParam;
return (int)msg.wParam;
}

//
Expand All @@ -42,24 +42,24 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_EXPLORERFRAME));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"ExplorerFrame";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

auto a = RegisterClassExW(&wcex);
return a;
WNDCLASSEXW wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_EXPLORERFRAME));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"ExplorerFrame";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

auto a = RegisterClassExW(&wcex);
return a;
}

//
Expand All @@ -74,51 +74,51 @@ ATOM MyRegisterClass(HINSTANCE hInstance)
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable

static const fnSetPreferredAppMode SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
SetPreferredAppMode(ForceDark);

HWND hWnd = CreateWindowW(L"ExplorerFrame", L"", 0,
0, 0, 0, 0, nullptr, nullptr, hInstance, nullptr);

if (!hWnd)
{
return FALSE;
}

// Create a new win event hook for every window creation.
HWINEVENTHOOK hook = SetWinEventHook(
EVENT_OBJECT_CREATE,
EVENT_OBJECT_CREATE,
hInstance,
WinEventProc,
0,
0,
WINEVENT_OUTOFCONTEXT);

// Enumerate opened windows.
EnumWindows([](HWND hwnd, LPARAM lParam) -> BOOL {
HandleWindow(hwnd);
return TRUE;
}, NULL);

// Create a notification icon, using version 4
// with Windows' built it tooltip and context menu support.
NOTIFYICONDATA nid = { sizeof(NOTIFYICONDATA) };
wcscpy_s(nid.szTip, L"ExplorerFrame");
nid.hWnd = hWnd;
nid.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_EXPLORERFRAME));
// nid.guidItem = notifyIconGuid;
nid.uID = 1;
nid.uCallbackMessage = WM_USER + 1;
nid.uVersion = NOTIFYICON_VERSION_4;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP /*| NIF_GUID*/;

Shell_NotifyIcon(NIM_ADD, &nid);
Shell_NotifyIcon(NIM_SETVERSION, &nid);

return TRUE;
hInst = hInstance; // Store instance handle in our global variable

static const fnSetPreferredAppMode SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
SetPreferredAppMode(ForceDark);

HWND hWnd = CreateWindowW(L"ExplorerFrame", L"", 0,
0, 0, 0, 0, nullptr, nullptr, hInstance, nullptr);

if (!hWnd)
{
return FALSE;
}

// Create a new win event hook for every window creation.
HWINEVENTHOOK hook = SetWinEventHook(
EVENT_OBJECT_CREATE,
EVENT_OBJECT_CREATE,
hInstance,
WinEventProc,
0,
0,
WINEVENT_OUTOFCONTEXT);

// Enumerate opened windows.
EnumWindows([](HWND hwnd, LPARAM lParam) -> BOOL {
HandleWindow(hwnd);
return TRUE;
}, NULL);

// Create a notification icon, using version 4
// with Windows' built it tooltip and context menu support.
NOTIFYICONDATA nid = { sizeof(NOTIFYICONDATA) };
wcscpy_s(nid.szTip, L"ExplorerFrame");
nid.hWnd = hWnd;
nid.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_EXPLORERFRAME));
// nid.guidItem = notifyIconGuid;
nid.uID = 1;
nid.uCallbackMessage = WM_USER + 1;
nid.uVersion = NOTIFYICON_VERSION_4;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP /*| NIF_GUID*/;

Shell_NotifyIcon(NIM_ADD, &nid);
Shell_NotifyIcon(NIM_SETVERSION, &nid);

return TRUE;
}

//
Expand All @@ -133,20 +133,20 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
{
for (const auto& [ _, value ] : hookingMap) {
// Try to unhook DLL (wil::unique_hhook::get()), do nothing if failed.
try {
UnhookWindowsHookEx(value);
}
catch (...) {}
}
PostQuitMessage(0);
break;
}
switch (message)
{
case WM_DESTROY:
{
for (const auto& [_, value] : hookingMap) {
// Try to unhook DLL (wil::unique_hhook::get()), do nothing if failed.
try {
UnhookWindowsHookEx(value);
}
catch (...) {}
}
PostQuitMessage(0);
break;
}
// Create a new case for our notification icon
case WM_USER + 1:
switch (LOWORD(lParam))
Expand All @@ -155,19 +155,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Get the mouse location using WPARAM.
// Note: if the mouse is outside the notification icon rect, use the center point of the icon instead.
POINT pt = { GET_X_LPARAM(wParam), GET_Y_LPARAM(wParam) };
if (pt.x == -1 && pt.y == -1)
{
RECT rc;
// NotifyIconGetRect
NOTIFYICONIDENTIFIER id;
id.cbSize = sizeof(NOTIFYICONIDENTIFIER);
id.hWnd = hWnd;
id.uID = 1;
Shell_NotifyIconGetRect(&id, &rc);
pt.x = (rc.left + rc.right) / 2;
pt.y = (rc.top + rc.bottom) / 2;
}
POINT pt = { GET_X_LPARAM(wParam), GET_Y_LPARAM(wParam) };
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, 1, L"Exit");
SetForegroundWindow(hWnd);
Expand All @@ -181,44 +169,44 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
break;
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

void WinEventProc(
HWINEVENTHOOK hWinEventHook,
DWORD event,
HWND hwnd,
LONG idObject,
LONG idChild,
DWORD idEventThread,
DWORD dwmsEventTime)
HWINEVENTHOOK hWinEventHook,
DWORD event,
HWND hwnd,
LONG idObject,
LONG idChild,
DWORD idEventThread,
DWORD dwmsEventTime)
{
HandleWindow(hwnd);
HandleWindow(hwnd);
}

void HandleWindow(HWND hwnd)
{
DWORD processId;
DWORD tid = GetWindowThreadProcessId(hwnd, &processId);
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
TCHAR processName[MAX_PATH];
DWORD length = MAX_PATH;
QueryFullProcessImageNameW(processHandle, 0, processName, &length);
CloseHandle(processHandle);

// Check if the process is the Windows Explorer
if (wcscmp(processName, L"C:\\Windows\\explorer.exe") == 0 && hookingMap.find(tid) == hookingMap.end())
{
HMODULE dll = LoadLibrary(L"Injector.dll");
if (!dll)
return;
auto procAddr = GetProcAddress(dll, "InjectExplorerHook");
auto method = reinterpret_cast<PFN_INJECT_EXPLORER_HOOK>(procAddr);
HHOOK hook = method(hwnd);
hookingMap.insert(std::pair<DWORD, HHOOK>(tid, hook));
FreeLibrary(dll);
}
DWORD processId;
DWORD tid = GetWindowThreadProcessId(hwnd, &processId);
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
TCHAR processName[MAX_PATH];
DWORD length = MAX_PATH;
QueryFullProcessImageNameW(processHandle, 0, processName, &length);
CloseHandle(processHandle);

// Check if the process is the Windows Explorer
if (wcscmp(processName, L"C:\\Windows\\explorer.exe") == 0 && hookingMap.find(tid) == hookingMap.end())
{
HMODULE dll = LoadLibrary(L"Injector.dll");
if (!dll)
return;
auto procAddr = GetProcAddress(dll, "InjectExplorerHook");
auto method = reinterpret_cast<PFN_INJECT_EXPLORER_HOOK>(procAddr);
HHOOK hook = method(hwnd);
hookingMap.insert(std::pair<DWORD, HHOOK>(tid, hook));
FreeLibrary(dll);
}
}
Loading

0 comments on commit 7468fb3

Please sign in to comment.