|
| 1 | +/* |
| 2 | + * MinHook - The Minimalistic API Hooking Library for x64/x86 |
| 3 | + * Copyright (C) 2009-2017 Tsuda Kageyu. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 18 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 19 | + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER |
| 20 | + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +#pragma once |
| 30 | + |
| 31 | +#if !(defined _M_IX86) && !(defined _M_X64) && !(defined __i386__) && !(defined __x86_64__) |
| 32 | + #error MinHook supports only x86 and x64 systems. |
| 33 | +#endif |
| 34 | + |
| 35 | +#include <windows.h> |
| 36 | + |
| 37 | +// MinHook Error Codes. |
| 38 | +typedef enum MH_STATUS |
| 39 | +{ |
| 40 | + // Unknown error. Should not be returned. |
| 41 | + MH_UNKNOWN = -1, |
| 42 | + |
| 43 | + // Successful. |
| 44 | + MH_OK = 0, |
| 45 | + |
| 46 | + // MinHook is already initialized. |
| 47 | + MH_ERROR_ALREADY_INITIALIZED, |
| 48 | + |
| 49 | + // MinHook is not initialized yet, or already uninitialized. |
| 50 | + MH_ERROR_NOT_INITIALIZED, |
| 51 | + |
| 52 | + // The hook for the specified target function is already created. |
| 53 | + MH_ERROR_ALREADY_CREATED, |
| 54 | + |
| 55 | + // The hook for the specified target function is not created yet. |
| 56 | + MH_ERROR_NOT_CREATED, |
| 57 | + |
| 58 | + // The hook for the specified target function is already enabled. |
| 59 | + MH_ERROR_ENABLED, |
| 60 | + |
| 61 | + // The hook for the specified target function is not enabled yet, or already |
| 62 | + // disabled. |
| 63 | + MH_ERROR_DISABLED, |
| 64 | + |
| 65 | + // The specified pointer is invalid. It points the address of non-allocated |
| 66 | + // and/or non-executable region. |
| 67 | + MH_ERROR_NOT_EXECUTABLE, |
| 68 | + |
| 69 | + // The specified target function cannot be hooked. |
| 70 | + MH_ERROR_UNSUPPORTED_FUNCTION, |
| 71 | + |
| 72 | + // Failed to allocate memory. |
| 73 | + MH_ERROR_MEMORY_ALLOC, |
| 74 | + |
| 75 | + // Failed to change the memory protection. |
| 76 | + MH_ERROR_MEMORY_PROTECT, |
| 77 | + |
| 78 | + // The specified module is not loaded. |
| 79 | + MH_ERROR_MODULE_NOT_FOUND, |
| 80 | + |
| 81 | + // The specified function is not found. |
| 82 | + MH_ERROR_FUNCTION_NOT_FOUND |
| 83 | +} |
| 84 | +MH_STATUS; |
| 85 | + |
| 86 | +// Can be passed as a parameter to MH_EnableHook, MH_DisableHook, |
| 87 | +// MH_QueueEnableHook or MH_QueueDisableHook. |
| 88 | +#define MH_ALL_HOOKS NULL |
| 89 | + |
| 90 | +#ifdef __cplusplus |
| 91 | +extern "C" { |
| 92 | +#endif |
| 93 | + |
| 94 | + // Initialize the MinHook library. You must call this function EXACTLY ONCE |
| 95 | + // at the beginning of your program. |
| 96 | + MH_STATUS WINAPI MH_Initialize(VOID); |
| 97 | + |
| 98 | + // Uninitialize the MinHook library. You must call this function EXACTLY |
| 99 | + // ONCE at the end of your program. |
| 100 | + MH_STATUS WINAPI MH_Uninitialize(VOID); |
| 101 | + |
| 102 | + // Creates a hook for the specified target function, in disabled state. |
| 103 | + // Parameters: |
| 104 | + // pTarget [in] A pointer to the target function, which will be |
| 105 | + // overridden by the detour function. |
| 106 | + // pDetour [in] A pointer to the detour function, which will override |
| 107 | + // the target function. |
| 108 | + // ppOriginal [out] A pointer to the trampoline function, which will be |
| 109 | + // used to call the original target function. |
| 110 | + // This parameter can be NULL. |
| 111 | + MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID *ppOriginal); |
| 112 | + |
| 113 | + // Creates a hook for the specified API function, in disabled state. |
| 114 | + // Parameters: |
| 115 | + // pszModule [in] A pointer to the loaded module name which contains the |
| 116 | + // target function. |
| 117 | + // pszProcName [in] A pointer to the target function name, which will be |
| 118 | + // overridden by the detour function. |
| 119 | + // pDetour [in] A pointer to the detour function, which will override |
| 120 | + // the target function. |
| 121 | + // ppOriginal [out] A pointer to the trampoline function, which will be |
| 122 | + // used to call the original target function. |
| 123 | + // This parameter can be NULL. |
| 124 | + MH_STATUS WINAPI MH_CreateHookApi( |
| 125 | + LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal); |
| 126 | + |
| 127 | + // Creates a hook for the specified API function, in disabled state. |
| 128 | + // Parameters: |
| 129 | + // pszModule [in] A pointer to the loaded module name which contains the |
| 130 | + // target function. |
| 131 | + // pszProcName [in] A pointer to the target function name, which will be |
| 132 | + // overridden by the detour function. |
| 133 | + // pDetour [in] A pointer to the detour function, which will override |
| 134 | + // the target function. |
| 135 | + // ppOriginal [out] A pointer to the trampoline function, which will be |
| 136 | + // used to call the original target function. |
| 137 | + // This parameter can be NULL. |
| 138 | + // ppTarget [out] A pointer to the target function, which will be used |
| 139 | + // with other functions. |
| 140 | + // This parameter can be NULL. |
| 141 | + MH_STATUS WINAPI MH_CreateHookApiEx( |
| 142 | + LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal, LPVOID *ppTarget); |
| 143 | + |
| 144 | + // Removes an already created hook. |
| 145 | + // Parameters: |
| 146 | + // pTarget [in] A pointer to the target function. |
| 147 | + MH_STATUS WINAPI MH_RemoveHook(LPVOID pTarget); |
| 148 | + |
| 149 | + // Enables an already created hook. |
| 150 | + // Parameters: |
| 151 | + // pTarget [in] A pointer to the target function. |
| 152 | + // If this parameter is MH_ALL_HOOKS, all created hooks are |
| 153 | + // enabled in one go. |
| 154 | + MH_STATUS WINAPI MH_EnableHook(LPVOID pTarget); |
| 155 | + |
| 156 | + // Disables an already created hook. |
| 157 | + // Parameters: |
| 158 | + // pTarget [in] A pointer to the target function. |
| 159 | + // If this parameter is MH_ALL_HOOKS, all created hooks are |
| 160 | + // disabled in one go. |
| 161 | + MH_STATUS WINAPI MH_DisableHook(LPVOID pTarget); |
| 162 | + |
| 163 | + // Queues to enable an already created hook. |
| 164 | + // Parameters: |
| 165 | + // pTarget [in] A pointer to the target function. |
| 166 | + // If this parameter is MH_ALL_HOOKS, all created hooks are |
| 167 | + // queued to be enabled. |
| 168 | + MH_STATUS WINAPI MH_QueueEnableHook(LPVOID pTarget); |
| 169 | + |
| 170 | + // Queues to disable an already created hook. |
| 171 | + // Parameters: |
| 172 | + // pTarget [in] A pointer to the target function. |
| 173 | + // If this parameter is MH_ALL_HOOKS, all created hooks are |
| 174 | + // queued to be disabled. |
| 175 | + MH_STATUS WINAPI MH_QueueDisableHook(LPVOID pTarget); |
| 176 | + |
| 177 | + // Applies all queued changes in one go. |
| 178 | + MH_STATUS WINAPI MH_ApplyQueued(VOID); |
| 179 | + |
| 180 | + // Translates the MH_STATUS to its name as a string. |
| 181 | + const char * WINAPI MH_StatusToString(MH_STATUS status); |
| 182 | + |
| 183 | +#ifdef __cplusplus |
| 184 | +} |
| 185 | +#endif |
0 commit comments