1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
Userspace Virtual Filesystem
Copyright (C) 2015 Sebastian Herbord. All rights reserved.
This file is part of usvfs.
usvfs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
usvfs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with usvfs. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <map>
#include <windows_sane.h>
namespace HookLib
{
enum HookError
{
ERR_NONE,
ERR_INVALIDPARAMETERS, // parameters are invalid
ERR_FUNCEND, // function is too short to be hooked
ERR_JUMP, // function consists only of an unconditional jump. Maybe it has already
// been hooked?
ERR_RIP, // segment of the function to be overwritten contains a instruction-relative
// operation
ERR_RELJUMP // segment of the function to be overwritten contains a relative jump we
// can't relocated
};
typedef ULONG HOOKHANDLE;
static const HOOKHANDLE INVALID_HOOK = (HOOKHANDLE)-1;
///
/// \brief install a stub (function to be called before the target function)
/// \param functionAddress address of the function to stub
/// \param stubAddress address of the stub function. This function has to have the
/// signature of void foobar(LPVOID address).
/// address receives the address of the function.
/// \param error (optional) if set, the referenced variable will receive an error code
/// describing the problem (if any)
/// \return a handle to reference the hook in later operations or INVALID_HOOK on error
///
HOOKHANDLE InstallStub(LPVOID functionAddress, LPVOID stubAddress,
HookError* error = nullptr);
///
/// \brief install a stub (function to be called before the target function)
/// \param module the module containing the function to hook
/// \param functionName name of the function to stub (as exported by the library)
/// \param stubAddress address of the stub function. This function has to have the
/// signature of void foobar(LPVOID address).
/// address receives the address of the function.
/// \param error (optional) if set, the referenced variable will receive an error code
/// describing the problem (if any)
/// \return a handle to reference the hook in later operations or INVALID_HOOK on error
///
HOOKHANDLE InstallStub(HMODULE module, LPCSTR functionName, LPVOID stubAddress,
HookError* error = nullptr);
///
/// \brief install a hook (function replacing the existing functionality of the
/// function)
/// \param functionAddress address of the function to hook
/// \param hookAddress address of the replacement function. This function has to have
/// the exact same signature as the replaced function
/// \param error (optional) if set, the referenced variable will receive an error code
/// describing the problem (if any)
/// \return a handle to reference the hook in later operations or INVALID_HOOK on error
///
HOOKHANDLE InstallHook(LPVOID functionAddress, LPVOID hookAddress,
HookError* error = nullptr);
///
/// \brief install a hook (function replacing the existing functionality of the
/// function)
/// \param functionName name of the function to hook (as exported by the library)
/// \param hookAddress address of the replacement function. This function has to have
/// the exact same signature as the replaced function
/// \param error (optional) if set, the referenced variable will receive an error code
/// describing the problem (if any)
/// \return a handle to reference the hook in later operations or INVALID_HOOK on error
///
HOOKHANDLE InstallHook(HMODULE module, LPCSTR functionName, LPVOID hookAddress,
HookError* error = nullptr);
///
/// \brief remove a hook
/// \param handle handle returned in InstallStub or InstallHook
///
void RemoveHook(HOOKHANDLE handle);
///
/// \brief determine the type of a hook
/// \param handle the handle to look up
/// \return a string describing the used hooking mechanism
///
const char* GetHookType(HOOKHANDLE handle);
///
/// \brief retrieve the address that can be used to directly call a detour
/// \param handle handle for the hook
/// \return function address
///
LPVOID GetDetour(HOOKHANDLE handle);
///
/// \brief resolve an error code to a descriptive string
/// \param err the error code to resolve
/// \return the error string
///
const char* GetErrorString(HookError err);
} // namespace HookLib
|