Textractor_test/vnrhook/main.cc

98 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;
2018-08-26 03:45:25 +08:00
HANDLE hmMutex;
2018-08-25 00:50:20 +08:00
2018-08-23 23:53:23 +08:00
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID unused)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
::hmMutex = CreateMutexW(nullptr, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(GetCurrentProcessId())).c_str());
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.address) man->ClearHook();
2018-08-23 23:53:23 +08:00
//if (ith_has_section)
UnmapViewOfFile(::hookman);
CloseHandle(::hookPipe);
CloseHandle(hSection);
CloseHandle(hmMutex);
//} 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++);
ConsoleOutput(("NextHooker: 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);
if (::hookman[currentHook].InsertHook()) ConsoleOutput(("NextHooker: inserted hook: " + name).c_str());
else ConsoleOutput("NextHooker:WARNING: failed to insert hook");
2018-08-23 23:53:23 +08:00
}
2018-08-26 03:45:25 +08:00
else ConsoleOutput("NextHooker: too many hooks: can't insert");
2018-08-23 23:53:23 +08:00
}
2018-08-26 03:45:25 +08:00
void RemoveHook(unsigned __int64 addr)
2018-08-23 23:53:23 +08:00
{
for (int i = 0; i < MAX_HOOK; i++)
2018-08-26 03:45:25 +08:00
if (abs((long long)(::hookman[i].hp.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