L2Bot2.0/InjectionLibrary/Injector.cpp
2023-01-16 15:33:32 +04:00

35 lines
961 B
C++

#include "pch.h"
#include "Injector.h"
namespace InjectLibrary
{
HHOOK Injector::_hookHandle = nullptr;
Injector::Injector(const std::string& mutexName, int windowsMessage) : _mutexName(mutexName), _windowsMessage(windowsMessage)
{
}
void Injector::SetHook(const HINSTANCE moduleHandle)
{
if (moduleHandle) {
// Ñ ïîìîùüþ ìþòåêñà ïðîâåðÿåì, ÷òî åùå íå áûëî õóêà
HANDLE mutexHandle = CreateMutexA(nullptr, false, _mutexName.c_str());
if (GetLastError() != ERROR_ALREADY_EXISTS) {
_hookHandle = SetWindowsHookExA(_windowsMessage, (HOOKPROC)HookMessageProcedure, moduleHandle, 0);
_mutexHandle = mutexHandle;
}
else if (mutexHandle) {
CloseHandle(mutexHandle);
}
}
else {
UnhookWindowsHookEx(_hookHandle);
CloseHandle(_mutexHandle);
}
}
const LRESULT Injector::HookMessageProcedure(const DWORD code, const DWORD wParam, const DWORD lParam)
{
return CallNextHookEx(_hookHandle, code, wParam, lParam);
}
}