Skip to content

Commit

Permalink
Add NanaZipFrierenGlobalInitialize and NanaZipFrierenGlobalUninitialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouriNaruto committed May 14, 2024
1 parent 28a8bed commit d02a4d7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
39 changes: 27 additions & 12 deletions NanaZip.Frieren/NanaZip.Frieren.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ namespace
PVOID Detoured;
};

FunctionItem g_FunctionTable[FunctionType::MaximumFunction];

thread_local bool volatile g_ThreadInitialized = false;
thread_local HHOOK volatile g_WindowsHookHandle = nullptr;
thread_local FunctionItem g_FunctionTable[FunctionType::MaximumFunction];

LRESULT CALLBACK WindowSubclassCallback(
_In_ HWND hWnd,
Expand Down Expand Up @@ -262,6 +263,28 @@ EXTERN_C HRESULT WINAPI NanaZipFrierenThreadInitialize()
return HRESULT_FROM_WIN32(::GetLastError());
}

g_ThreadInitialized = true;

return S_OK;
}

EXTERN_C HRESULT WINAPI NanaZipFrierenThreadUninitialize()
{
if (!g_ThreadInitialized)
{
return S_OK;
}

g_ThreadInitialized = false;

::UnhookWindowsHookEx(g_WindowsHookHandle);
g_WindowsHookHandle = nullptr;

return S_OK;
}

EXTERN_C HRESULT WINAPI NanaZipFrierenGlobalInitialize()
{
g_FunctionTable[FunctionType::GetSysColor].Original =
::GetSysColor;
g_FunctionTable[FunctionType::GetSysColor].Detoured =
Expand Down Expand Up @@ -295,19 +318,14 @@ EXTERN_C HRESULT WINAPI NanaZipFrierenThreadInitialize()
}
::DetourTransactionCommit();

g_ThreadInitialized = true;
::NanaZipFrierenThreadInitialize();

return S_OK;
}

EXTERN_C HRESULT WINAPI NanaZipFrierenThreadUninitialize()
EXTERN_C HRESULT WINAPI NanaZipFrierenGlobalUninitialize()
{
if (!g_ThreadInitialized)
{
return S_OK;
}

g_ThreadInitialized = false;
::NanaZipFrierenThreadUninitialize();

::DetourTransactionBegin();
::DetourUpdateThread(::GetCurrentThread());
Expand All @@ -324,8 +342,5 @@ EXTERN_C HRESULT WINAPI NanaZipFrierenThreadUninitialize()
}
::DetourTransactionCommit();

::UnhookWindowsHookEx(g_WindowsHookHandle);
g_WindowsHookHandle = nullptr;

return S_OK;
}
18 changes: 16 additions & 2 deletions NanaZip.Frieren/NanaZip.Frieren.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,31 @@
#include <Windows.h>

/**
* @brief Initialize NanaZip.Frieren for thread.
* @brief Initialize NanaZip.Frieren for non-main thread.
* @return If the function succeeds, it returns S_OK. Otherwise, it returns an
* HRESULT error code.
*/
EXTERN_C HRESULT WINAPI NanaZipFrierenThreadInitialize();

/**
* @brief Uninitialize NanaZip.Frieren for thread.
* @brief Uninitialize NanaZip.Frieren for non-main thread.
* @return If the function succeeds, it returns S_OK. Otherwise, it returns an
* HRESULT error code.
*/
EXTERN_C HRESULT WINAPI NanaZipFrierenThreadUninitialize();

/**
* @brief Initialize NanaZip.Frieren for main thread.
* @return If the function succeeds, it returns S_OK. Otherwise, it returns an
* HRESULT error code.
*/
EXTERN_C HRESULT WINAPI NanaZipFrierenGlobalInitialize();

/**
* @brief Uninitialize NanaZip.Frieren for main thread.
* @return If the function succeeds, it returns S_OK. Otherwise, it returns an
* HRESULT error code.
*/
EXTERN_C HRESULT WINAPI NanaZipFrierenGlobalUninitialize();

#endif // !NANAZIP_FRIEREN

0 comments on commit d02a4d7

Please sign in to comment.