diff --git a/texthook/hookman.cc b/texthook/hookman.cc index 2571940..61e230b 100644 --- a/texthook/hookman.cc +++ b/texthook/hookman.cc @@ -35,11 +35,14 @@ HookManager::HookManager() : HookManager::~HookManager() { - HM_LOCK; + EnterCriticalSection(&hmCs); + RemoveThreads([](auto one, auto two) { return true; }, {}); + for (auto i : processRecordsByIds) UnRegisterProcess(i.first); + LeaveCriticalSection(&hmCs); DeleteCriticalSection(&hmCs); } -TextThread *HookManager::FindSingle(DWORD number) +TextThread* HookManager::FindSingle(DWORD number) { HM_LOCK; for (auto i : textThreadsByParams) @@ -48,26 +51,12 @@ TextThread *HookManager::FindSingle(DWORD number) return nullptr; } -void HookManager::RemoveSingleHook(DWORD pid, DWORD addr) +void HookManager::RemoveThreads(bool(*RemoveIf)(ThreadParameter, ThreadParameter), ThreadParameter cmp) { HM_LOCK; std::vector removedThreads; for (auto i : textThreadsByParams) - if (i.first.pid == pid && i.first.hook == addr) - { - if (remove) remove(i.second); - delete i.second; - removedThreads.push_back(i.first); - } - for (auto i : removedThreads) textThreadsByParams.erase(i); -} - -void HookManager::RemoveProcessContext(DWORD pid) -{ - HM_LOCK; - std::vector removedThreads; - for (auto i : textThreadsByParams) - if (i.first.pid == pid) + if (RemoveIf(i.first, cmp)) { if (remove) remove(i.second); delete i.second; @@ -98,7 +87,7 @@ void HookManager::UnRegisterProcess(DWORD pid) CloseHandle(pr.process_handle); CloseHandle(pr.hookman_section); processRecordsByIds.erase(pid); - RemoveProcessContext(pid); + RemoveThreads([](auto one, auto two) { return one.pid == two.pid; }, { pid, 0, 0, 0 }); if (detach) detach(pid); } @@ -149,7 +138,7 @@ HookParam HookManager::GetHookParam(DWORD pid, DWORD addr) std::wstring HookManager::GetHookName(DWORD pid, DWORD addr) { HM_LOCK; - std::string buffer; + std::string buffer = ""; ProcessRecord pr = processRecordsByIds[pid]; if (pr.hookman_map == nullptr) return L""; MutexLocker locker(pr.hookman_mutex); diff --git a/texthook/hookman.h b/texthook/hookman.h index f8c3c02..4d3037f 100644 --- a/texthook/hookman.h +++ b/texthook/hookman.h @@ -37,14 +37,13 @@ public: HookManager(); ~HookManager(); - TextThread *FindSingle(DWORD number); - HANDLE GetHostPipe(DWORD pid); + TextThread* FindSingle(DWORD number); void AddConsoleOutput(std::wstring text); void DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD split, const BYTE *text, int len); - void RemoveProcessContext(DWORD pid); // private - void RemoveSingleHook(DWORD pid, DWORD addr); + void RemoveThreads(bool(*RemoveIf)(ThreadParameter, ThreadParameter), ThreadParameter cmp); void RegisterProcess(DWORD pid, HANDLE hostPipe); void UnRegisterProcess(DWORD pid); + HANDLE GetHostPipe(DWORD pid); HookParam GetHookParam(DWORD pid, DWORD addr); std::wstring GetHookName(DWORD pid, DWORD addr); diff --git a/texthook/host.cc b/texthook/host.cc index 8a691e0..e6667dc 100644 --- a/texthook/host.cc +++ b/texthook/host.cc @@ -151,7 +151,7 @@ DLLEXPORT bool RemoveHook(DWORD pid, DWORD addr) WaitForSingleObject(hookRemovalEvent, 1000); CloseHandle(hookRemovalEvent); - man->RemoveSingleHook(pid, addr); + man->RemoveThreads([](auto one, auto two) { return one.pid == two.pid && one.hook == two.hook; }, { pid, addr, 0, 0 }); return true; } diff --git a/texthook/textthread.cc b/texthook/textthread.cc index 8f09409..d7ca42a 100644 --- a/texthook/textthread.cc +++ b/texthook/textthread.cc @@ -29,7 +29,8 @@ TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, unsigned i TextThread::~TextThread() { - TT_LOCK; + EnterCriticalSection(&ttCs); + LeaveCriticalSection(&ttCs); DeleteCriticalSection(&ttCs); }