Textractor_test/vnrhook/main.cc

95 lines
2.6 KiB
C++
Raw Normal View History

2018-08-23 23:53:23 +08:00
// main.cc
// 8/24/2013 jichi
// Branch: ITH_DLL/main.cpp, rev 128
// 8/24/2013 TODO: Clean up this file
#ifdef _MSC_VER
# pragma warning (disable:4100) // C4100: unreference formal parameter
//# pragma warning (disable:4733) // C4733: Inline asm assigning to 'FS:0' : handler not registered as safe handler
#endif // _MSC_VER
#include "main.h"
#include "defs.h"
2018-08-26 03:45:25 +08:00
#include "MinHook.h"
#include "pipe.h"
2018-08-23 23:53:23 +08:00
#include "engine/engine.h"
#include "engine/match.h"
#include "hijack/texthook.h"
#include "util/growl.h"
HANDLE hSection;
bool running;
2018-08-26 03:45:25 +08:00
int currentHook = 0, userhookCount = 0;
2018-08-23 23:53:23 +08:00
DWORD trigger = 0;
std::unique_ptr<WinMutex> sectionMutex;
2018-08-25 00:50:20 +08:00
2018-09-21 10:42:15 +08:00
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID)
2018-08-23 23:53:23 +08:00
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
sectionMutex = std::make_unique<WinMutex>(ITH_HOOKMAN_MUTEX_ + std::to_wstring(GetCurrentProcessId()));
2018-08-23 23:53:23 +08:00
if (GetLastError() == ERROR_ALREADY_EXISTS) return FALSE;
DisableThreadLibraryCalls(hModule);
// jichi 9/25/2013: Interprocedural communication with vnrsrv.
hSection = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, 0, HOOK_SECTION_SIZE, (ITH_SECTION_ + std::to_wstring(GetCurrentProcessId())).c_str());
::hookman = (TextHook*)MapViewOfFile(hSection, FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, HOOK_BUFFER_SIZE);
2018-08-26 03:45:25 +08:00
memset(::hookman, 0, HOOK_BUFFER_SIZE);
2018-08-23 23:53:23 +08:00
2018-08-26 03:45:25 +08:00
MH_Initialize();
2018-08-23 23:53:23 +08:00
::running = true;
CreatePipe();
}
break;
case DLL_PROCESS_DETACH:
{
::running = false;
2018-08-26 03:45:25 +08:00
MH_Uninitialize();
for (TextHook *man = ::hookman; man < ::hookman + MAX_HOOK; man++) if (man->hp.insertion_address) man->ClearHook();
2018-08-23 23:53:23 +08:00
//if (ith_has_section)
UnmapViewOfFile(::hookman);
CloseHandle(hSection);
//} ITH_EXCEPT {}
}
break;
}
return TRUE;
}
//extern "C" {
2018-08-26 03:45:25 +08:00
void NewHook(const HookParam &hp, LPCSTR lpname, DWORD flag)
2018-08-23 23:53:23 +08:00
{
std::string name = lpname;
2018-08-26 03:45:25 +08:00
if (++currentHook < MAX_HOOK)
{
if (name[0] == '\0') name = "UserHook" + std::to_string(userhookCount++);
2018-09-30 04:05:08 +08:00
ConsoleOutput(("Textractor: try inserting hook: " + name).c_str());
2018-08-23 23:53:23 +08:00
// jichi 7/13/2014: This function would raise when too many hooks added
2018-08-26 03:45:25 +08:00
::hookman[currentHook].InitHook(hp, name.c_str(), flag);
2018-09-30 04:05:08 +08:00
if (::hookman[currentHook].InsertHook()) ConsoleOutput(("Textractor: inserted hook: " + name).c_str());
else ConsoleOutput("Textractor:WARNING: failed to insert hook");
2018-08-23 23:53:23 +08:00
}
2018-09-30 04:05:08 +08:00
else ConsoleOutput("Textractor: too many hooks: can't insert");
2018-08-23 23:53:23 +08:00
}
2018-08-26 03:45:25 +08:00
2018-09-21 09:59:07 +08:00
void RemoveHook(uint64_t addr)
2018-08-23 23:53:23 +08:00
{
for (int i = 0; i < MAX_HOOK; i++)
if (abs((long long)(::hookman[i].hp.insertion_address - addr)) < 9)
2018-08-30 05:15:59 +08:00
{
2018-08-23 23:53:23 +08:00
::hookman[i].ClearHook();
2018-08-30 05:15:59 +08:00
return;
}
2018-08-23 23:53:23 +08:00
}
2018-08-26 03:45:25 +08:00
void SwitchTrigger(DWORD t)
2018-08-23 23:53:23 +08:00
{
trigger = t;
}
// EOF