From 332e5aecaf24a6d9abbf78d276839207471075cb Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sun, 20 Jan 2019 09:52:35 -0500 Subject: [PATCH] refactor --- GUI/exception.cpp | 13 ++----------- GUI/host/host.cpp | 20 ++++++++------------ GUI/host/util.cpp | 15 ++++++--------- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/GUI/exception.cpp b/GUI/exception.cpp index 6fde5a6..ba39979 100644 --- a/GUI/exception.cpp +++ b/GUI/exception.cpp @@ -21,11 +21,7 @@ namespace LONG WINAPI ExceptionLogger(EXCEPTION_POINTERS* exception) { - thread_local static auto _ = std::invoke([] - { - std::set_terminate(Terminate); - return 0; - }); + thread_local static auto _ = std::set_terminate(Terminate); MEMORY_BASIC_INFORMATION info = {}; VirtualQuery(exception->ExceptionRecord->ExceptionAddress, &info, sizeof(info)); @@ -47,10 +43,5 @@ namespace return EXCEPTION_CONTINUE_SEARCH; } - auto _ = std::invoke([] - { - AddVectoredExceptionHandler(FALSE, ExceptionLogger); - SetUnhandledExceptionFilter([](auto) -> LONG { Terminate(); }); - return 0; - }); + auto _ = (AddVectoredExceptionHandler(FALSE, ExceptionLogger), SetUnhandledExceptionFilter([](auto) -> LONG { Terminate(); })); } diff --git a/GUI/host/host.cpp b/GUI/host/host.cpp index 8662c00..0cbe5ab 100644 --- a/GUI/host/host.cpp +++ b/GUI/host/host.cpp @@ -120,16 +120,6 @@ namespace processRecordsByIds->erase(processId); }).detach(); } - - void StartCapturingClipboard() - { - SetWindowsHookExW(WH_GETMESSAGE, [](int statusCode, WPARAM wParam, LPARAM lParam) - { - if (statusCode == HC_ACTION && wParam == PM_REMOVE && ((MSG*)lParam)->message == WM_CLIPBOARDUPDATE) - if (auto text = Util::GetClipboardText()) Host::GetThread(Host::clipboard)->AddSentence(text.value()); - return CallNextHookEx(NULL, statusCode, wParam, lParam); - }, NULL, GetCurrentThreadId()); - } } namespace Host @@ -144,8 +134,14 @@ namespace Host processRecordsByIds->try_emplace(console.processId, console.processId, INVALID_HANDLE_VALUE); textThreadsByParams->insert({ console, std::make_unique(console, HookParam{}, CONSOLE) }); textThreadsByParams->insert({ Host::clipboard, std::make_unique(Host::clipboard, HookParam{}, CLIPBOARD) }); - StartCapturingClipboard(); CreatePipe(); + + SetWindowsHookExW(WH_GETMESSAGE, [](int statusCode, WPARAM wParam, LPARAM lParam) + { + if (statusCode == HC_ACTION && wParam == PM_REMOVE && ((MSG*)lParam)->message == WM_CLIPBOARDUPDATE) + if (auto text = Util::GetClipboardText()) Host::GetThread(Host::clipboard)->AddSentence(text.value()); + return CallNextHookEx(NULL, statusCode, wParam, lParam); + }, NULL, GetCurrentThreadId()); } bool InjectProcess(DWORD processId, DWORD timeout) @@ -174,7 +170,7 @@ namespace Host #endif if (LPVOID remoteData = VirtualAllocEx(process, nullptr, (location.size() + 1) * sizeof(wchar_t), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) { - WriteProcessMemory(process, remoteData, location.c_str(), location.size() * 2 + 2, nullptr); + WriteProcessMemory(process, remoteData, location.c_str(), (location.size() + 1) * sizeof(wchar_t), nullptr); if (AutoHandle<> thread = CreateRemoteThread(process, nullptr, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, remoteData, 0, nullptr)) { WaitForSingleObject(thread, timeout); diff --git a/GUI/host/util.cpp b/GUI/host/util.cpp index 8b40437..3b2a9cf 100644 --- a/GUI/host/util.cpp +++ b/GUI/host/util.cpp @@ -6,12 +6,9 @@ namespace Util { std::optional GetModuleFilename(DWORD processId, HMODULE module) { - if (AutoHandle<> process = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId)) - { - std::vector buffer(MAX_PATH); + std::vector buffer(MAX_PATH); + if (AutoHandle<> process = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId)) if (GetModuleFileNameExW(process, module, buffer.data(), MAX_PATH)) return buffer.data(); - return {}; - } return {}; } @@ -27,10 +24,10 @@ namespace Util if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return {}; if (!OpenClipboard(NULL)) return {}; - std::optional ret; - if (AutoHandle> clipboard = GetClipboardData(CF_UNICODETEXT)) ret = (wchar_t*)GlobalLock(clipboard); + std::optional text; + if (AutoHandle> clipboard = GetClipboardData(CF_UNICODETEXT)) text = (wchar_t*)GlobalLock(clipboard); CloseClipboard(); - return ret; + return text; } std::optional StringToWideString(std::string text, UINT encoding) @@ -45,7 +42,7 @@ namespace Util wchar_t* end = text.data() + text.size(); for (int len = text.size() / 3; len > 6; --len) if (wcsncmp(end - len * 3, end - len * 2, len) == 0 && wcsncmp(end - len * 3, end - len * 1, len) == 0) - return true | RemoveRepetition(text = end - len); + return RemoveRepetition(text = end - len), true; return false; } }