forked from thelink2012/injector
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9da9f94
commit 1391267
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <MinHook.h> | ||
|
||
#include "FunctionHookMinHook.hpp" | ||
|
||
using namespace std; | ||
|
||
bool g_isMinHookInitialized{ false }; | ||
|
||
FunctionHookMinHook::FunctionHookMinHook(uintptr_t target, uintptr_t destination) | ||
: m_target{ 0 }, | ||
m_destination{ 0 }, | ||
m_original{ 0 } | ||
{ | ||
// Initialize MinHook if it hasn't been already. | ||
if (!g_isMinHookInitialized && MH_Initialize() == MH_OK) { | ||
g_isMinHookInitialized = true; | ||
} | ||
|
||
// Create the hook. Call create afterwards to prevent race conditions accessing FunctionHookMinHook before it leaves its constructor. | ||
if (auto status = MH_CreateHook(reinterpret_cast<void*>(target), reinterpret_cast<void*>(destination), (LPVOID*)&m_original); status == MH_OK) { | ||
m_target = target; | ||
m_destination = destination; | ||
} | ||
} | ||
|
||
FunctionHookMinHook::~FunctionHookMinHook() { | ||
remove(); | ||
} | ||
|
||
bool FunctionHookMinHook::create() { | ||
if (m_target == 0 || m_destination == 0 || m_original == 0) { | ||
return false; | ||
} | ||
|
||
if (auto status = MH_EnableHook((LPVOID)m_target); status != MH_OK) { | ||
m_original = 0; | ||
m_destination = 0; | ||
m_target = 0; | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool FunctionHookMinHook::remove() { | ||
// Don't try to remove invalid hooks. | ||
if (m_original == 0) { | ||
return true; | ||
} | ||
|
||
// Disable then remove the hook. | ||
if (MH_DisableHook((LPVOID)m_target) != MH_OK || | ||
MH_RemoveHook((LPVOID)m_target) != MH_OK) { | ||
return false; | ||
} | ||
|
||
// Invalidate the members. | ||
m_target = 0; | ||
m_destination = 0; | ||
m_original = 0; | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <windows.h> | ||
#include <cstdint> | ||
|
||
class FunctionHookMinHook { | ||
public: | ||
FunctionHookMinHook() = delete; | ||
FunctionHookMinHook(const FunctionHookMinHook& other) = delete; | ||
FunctionHookMinHook(FunctionHookMinHook&& other) = delete; | ||
FunctionHookMinHook(uintptr_t target, uintptr_t destination); | ||
virtual ~FunctionHookMinHook(); | ||
|
||
bool create(); | ||
|
||
// Called automatically by the destructor, but you can call it explicitly | ||
// if you need to remove the hook. | ||
bool remove(); | ||
|
||
auto get_original() const { | ||
return m_original; | ||
} | ||
|
||
template <typename T> | ||
T* get_original() const { | ||
return (T*)m_original; | ||
} | ||
|
||
auto is_valid() const { | ||
return m_original != 0; | ||
} | ||
|
||
FunctionHookMinHook& operator=(const FunctionHookMinHook& other) = delete; | ||
FunctionHookMinHook& operator=(FunctionHookMinHook&& other) = delete; | ||
|
||
private: | ||
uintptr_t m_target{ 0 }; | ||
uintptr_t m_destination{ 0 }; | ||
uintptr_t m_original{ 0 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 praydog | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |