GPCS4/3rdParty/tinydbr/windows/dllnotify.cpp
Asuka 279ba8e2ed add tinydbr library and reconstruct 3rdparty libraries
now we have the full power to develop this shit
2022-03-11 02:05:59 +08:00

70 lines
1.6 KiB
C++

#include "dllnotify.h"
#ifndef STATUS_SUCCESS
#define STATUS_SUCCESS 0
#endif
typedef NTSTATUS (NTAPI *PfuncLdrRegisterDllNotification)(
ULONG Flags,
PLDR_DLL_NOTIFICATION_FUNCTION NotificationFunction,
PVOID Context,
PVOID* Cookie);
typedef NTSTATUS (NTAPI *PfuncLdrUnregisterDllNotification)(
PVOID Cookie);
PfuncLdrRegisterDllNotification LdrRegisterDllNotification = nullptr;
PfuncLdrUnregisterDllNotification LdrUnregisterDllNotification = nullptr;
void* InstallDllNotification(PLDR_DLL_NOTIFICATION_FUNCTION NotificationFunction, PVOID Context)
{
void* pCookie = nullptr;
do
{
if (!LdrRegisterDllNotification)
{
HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
if (!hNtdll)
{
break;
}
LdrRegisterDllNotification = (PfuncLdrRegisterDllNotification)GetProcAddress(hNtdll, "LdrRegisterDllNotification");
if (!LdrRegisterDllNotification)
{
break;
}
}
NTSTATUS status = LdrRegisterDllNotification(0, NotificationFunction, Context, &pCookie);
if (status != STATUS_SUCCESS)
{
break;
}
} while (false);
return pCookie;
}
void UninstallDllNotification(void* Cookie)
{
do
{
if (!LdrUnregisterDllNotification)
{
HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
if (!hNtdll)
{
break;
}
LdrUnregisterDllNotification = (PfuncLdrUnregisterDllNotification)GetProcAddress(hNtdll, "LdrUnregisterDllNotification");
if (!LdrUnregisterDllNotification)
{
break;
}
}
LdrUnregisterDllNotification(Cookie);
} while (false);
}