Skip to content

Commit

Permalink
properly detach process for easyhook static lib
Browse files Browse the repository at this point in the history
  • Loading branch information
snowie2000 committed Sep 9, 2024
1 parent ebb860e commit 0bbafc1
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,49 @@ extern BYTE* AACache, *AACacheFull;
extern HFONT g_alterGUIFont;
extern void DebugOut(const WCHAR* szFormat, ...);


void EZHookMain(HINSTANCE instance, DWORD reason, LPVOID lpReserved) {
#ifdef STATIC_LIB
switch (reason) {
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
EasyHookDllMain(instance, reason, lpReserved);
}
#else
switch (reason) {
case DLL_PROCESS_ATTACH:
{
LPWSTR dllPath = new WCHAR[MAX_PATH + 1];
int nSize = GetModuleFileName(g_dllInstance, dllPath, MAX_PATH + 1);
WCHAR* p = &dllPath[nSize];
while (*--p != L'\\');
*p = L'\0';
#ifdef _WIN64
wcscat(dllPath, L"\\easyhk64.dll");
#else
wcscat(dllPath, L"\\easyhk32.dll");
#endif
HMODULE hEasyhk = LoadLibrary(dllPath);
delete[]dllPath;
if (!hEasyhk) {
DebugOut(L"Failed to load Easyhook, exiting");
return false;
}
}
}
#endif
}

extern COLORCACHE* g_AACache2[MAX_CACHE_SIZE];
HANDLE hDelayHook = 0;
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID lpReserved)
{
try {
static bool bDllInited = false;
BOOL IsUnload = false, bEnableDW = true, bUseFontSubstitute = false;


switch (reason) {
case DLL_PROCESS_ATTACH:
#ifdef DEBUG
Expand All @@ -463,30 +499,8 @@ BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID lpReserved)
if (bDllInited)
return true;
g_dllInstance = instance;

#ifdef EASYHOOK
#ifdef STATIC_LIB
EasyHookDllMain(instance, reason, lpReserved);
#else
{
LPWSTR dllPath = new WCHAR[MAX_PATH + 1];
int nSize = GetModuleFileName(g_dllInstance, dllPath, MAX_PATH + 1);
WCHAR* p = &dllPath[nSize];
while (*--p != L'\\');
*p = L'\0';
#ifdef _WIN64
wcscat(dllPath, L"\\easyhk64.dll");
#else
wcscat(dllPath, L"\\easyhk32.dll");
#endif
HMODULE hEasyhk = LoadLibrary(dllPath);
delete[]dllPath;
if (!hEasyhk) {
DebugOut(L"Failed to load Easyhook, exiting");
return false;
}
}
#endif
EZHookMain(instance, reason, lpReserved);
#endif
//初期化順序
//DLL_PROCESS_DETACHではこれの逆順にする
Expand Down Expand Up @@ -604,9 +618,15 @@ BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID lpReserved)
//APITracer::Finish();
break;
case DLL_THREAD_ATTACH:
#ifdef EASYHOOK
EZHookMain(instance, reason, lpReserved);
#endif
break;
case DLL_THREAD_DETACH:
g_TLInfo.ThreadTerm();
#ifdef EASYHOOK
EZHookMain(instance, reason, lpReserved);
#endif
break;
case DLL_PROCESS_DETACH:
// RemoveManagerHook();
Expand Down Expand Up @@ -647,6 +667,9 @@ BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID lpReserved)
g_TLInfo.ProcessTerm();
CCriticalSectionLock::Term();
COwnedCriticalSectionLock::Term();
#ifdef EASYHOOK
EZHookMain(instance, reason, lpReserved);
#endif
break;
}
return TRUE;
Expand Down

0 comments on commit 0bbafc1

Please sign in to comment.