From 24407f897951a4f02a72929b1adc5f57ecbeb581 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 2 Aug 2018 17:17:54 -0400 Subject: [PATCH] receive hook removal via pipe --- texthook/host.cc | 15 +++------------ texthook/pipe.cc | 8 ++++++-- texthook/textthread.cc | 7 +++---- texthook/textthread.h | 5 +---- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/texthook/host.cc b/texthook/host.cc index 1f41d26..d52b33d 100644 --- a/texthook/host.cc +++ b/texthook/host.cc @@ -73,8 +73,7 @@ namespace Host DLLEXPORT void Open() { - TextThread* console = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++); - console->Status() |= USING_UNICODE; + TextThread* console = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++, USING_UNICODE); if (onCreate) onCreate(console); CreateNewPipe(); } @@ -142,18 +141,11 @@ namespace Host DLLEXPORT bool RemoveHook(DWORD pid, DWORD addr) { - HANDLE hostPipe = processRecordsByIds[pid].hostPipe; - if (hostPipe == nullptr) return false; - HANDLE hookRemovalEvent = CreateEventW(nullptr, TRUE, FALSE, ITH_REMOVEHOOK_EVENT); BYTE buffer[sizeof(DWORD) * 2] = {}; *(DWORD*)buffer = HOST_COMMAND_REMOVE_HOOK; *(DWORD*)(buffer + sizeof(DWORD)) = addr; DWORD unused; - WriteFile(hostPipe, buffer, sizeof(DWORD) * 2, &unused, nullptr); - WaitForSingleObject(hookRemovalEvent, 1000); - CloseHandle(hookRemovalEvent); - RemoveThreads([](auto one, auto two) { return one.pid == two.pid && one.hook == two.hook; }, { pid, addr, 0, 0 }); - return true; + return WriteFile(processRecordsByIds[pid].hostPipe, buffer, sizeof(DWORD) * 2, &unused, nullptr); } DLLEXPORT HookParam GetHookParam(DWORD pid, DWORD addr) @@ -219,8 +211,7 @@ void DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD split, const BYTE * t TextThread *it; if ((it = textThreadsByParams[tp]) == nullptr) { - it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++); - if (Host::GetHookParam(pid, hook).type & USING_UNICODE) it->Status() |= USING_UNICODE; + it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++, Host::GetHookParam(pid, hook).type); if (onCreate) onCreate(it); } it->AddText(text, len); diff --git a/texthook/pipe.cc b/texthook/pipe.cc index 304ae21..b9ac4f3 100644 --- a/texthook/pipe.cc +++ b/texthook/pipe.cc @@ -47,12 +47,16 @@ DWORD WINAPI TextReceiver(LPVOID lpThreadParameter) if (*(DWORD*)buffer == HOST_NOTIFICATION) { - USES_CONVERSION; - switch (*(DWORD*)(buffer + 4)) // Artikash 7/17/2018: Notification type + switch (*(DWORD*)(buffer + sizeof(DWORD))) // Artikash 7/17/2018: Notification type { case HOST_NOTIFICATION_NEWHOOK: // Artikash 7/18/2018: Useless for now, but could be used to implement smth later break; + case HOST_NOTIFICATION_RMVHOOK: + RemoveThreads([](auto one, auto two) { return one.pid == two.pid && one.hook == two.hook; }, + { processId, *(DWORD*)(buffer + sizeof(DWORD) * 2) }); // Address + break; case HOST_NOTIFICATION_TEXT: + USES_CONVERSION; Host::AddConsoleOutput(A2W((LPCSTR)(buffer + sizeof(DWORD) * 2))); // Text break; } diff --git a/texthook/textthread.cc b/texthook/textthread.cc index 74177b3..4da5160 100644 --- a/texthook/textthread.cc +++ b/texthook/textthread.cc @@ -13,13 +13,12 @@ extern HWND dummyWindow; #define TT_LOCK CriticalSectionLocker ttLocker(ttCs) // Synchronized scope for accessing private data -TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, unsigned int splitDelay) : +TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, DWORD status) : storage(), sentenceBuffer(), - status(0), + status(status), flushTimer(0), threadNumber(threadNumber), - splitDelay(splitDelay), output(nullptr), tp(tp) { @@ -77,7 +76,7 @@ void TextThread::AddText(const BYTE *con, int len) { TT_LOCK; sentenceBuffer.insert(sentenceBuffer.end(), con, con + len); - flushTimer = SetTimer(dummyWindow, (UINT_PTR)this, splitDelay, + flushTimer = SetTimer(dummyWindow, (UINT_PTR)this, 250, // TODO: Let user change delay before sentenceBuffer is flushed [](HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { KillTimer(hWnd, idEvent); diff --git a/texthook/textthread.h b/texthook/textthread.h index 10711b7..83e981a 100644 --- a/texthook/textthread.h +++ b/texthook/textthread.h @@ -31,14 +31,12 @@ typedef std::function ThreadOutputCallb class TextThread { public: - TextThread(ThreadParameter tp, unsigned int threadNumber, unsigned int splitDelay = 250); + TextThread(ThreadParameter tp, unsigned int threadNumber, DWORD status); ~TextThread(); virtual std::wstring GetStore(); - DWORD &Status() { return status; } WORD Number() const { return threadNumber; } ThreadParameter GetThreadParameter() { return tp; } - void SetSplitDelay(unsigned int splitDelay) { this->splitDelay = splitDelay; } void RegisterOutputCallBack(ThreadOutputCallback cb) { output = cb; } @@ -55,7 +53,6 @@ private: ThreadParameter tp; unsigned int threadNumber; - unsigned int splitDelay; DWORD status; unsigned int flushTimer; };