MinHook.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #pragma once
  29. #if !(defined _M_IX86) && !(defined _M_X64) && !(defined __i386__) && !(defined __x86_64__)
  30. #error MinHook supports only x86 and x64 systems.
  31. #endif
  32. #include <windows.h>
  33. // MinHook Error Codes.
  34. typedef enum MH_STATUS
  35. {
  36. // Unknown error. Should not be returned.
  37. MH_UNKNOWN = -1,
  38. // Successful.
  39. MH_OK = 0,
  40. // MinHook is already initialized.
  41. MH_ERROR_ALREADY_INITIALIZED,
  42. // MinHook is not initialized yet, or already uninitialized.
  43. MH_ERROR_NOT_INITIALIZED,
  44. // The hook for the specified target function is already created.
  45. MH_ERROR_ALREADY_CREATED,
  46. // The hook for the specified target function is not created yet.
  47. MH_ERROR_NOT_CREATED,
  48. // The hook for the specified target function is already enabled.
  49. MH_ERROR_ENABLED,
  50. // The hook for the specified target function is not enabled yet, or already
  51. // disabled.
  52. MH_ERROR_DISABLED,
  53. // The specified pointer is invalid. It points the address of non-allocated
  54. // and/or non-executable region.
  55. MH_ERROR_NOT_EXECUTABLE,
  56. // The specified target function cannot be hooked.
  57. MH_ERROR_UNSUPPORTED_FUNCTION,
  58. // Failed to allocate memory.
  59. MH_ERROR_MEMORY_ALLOC,
  60. // Failed to change the memory protection.
  61. MH_ERROR_MEMORY_PROTECT,
  62. // The specified module is not loaded.
  63. MH_ERROR_MODULE_NOT_FOUND,
  64. // The specified function is not found.
  65. MH_ERROR_FUNCTION_NOT_FOUND
  66. }
  67. MH_STATUS;
  68. // Can be passed as a parameter to MH_EnableHook, MH_DisableHook,
  69. // MH_QueueEnableHook or MH_QueueDisableHook.
  70. #define MH_ALL_HOOKS NULL
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. // Initialize the MinHook library. You must call this function EXACTLY ONCE
  75. // at the beginning of your program.
  76. MH_STATUS WINAPI MH_Initialize(VOID);
  77. // Uninitialize the MinHook library. You must call this function EXACTLY
  78. // ONCE at the end of your program.
  79. MH_STATUS WINAPI MH_Uninitialize(VOID);
  80. // Creates a hook for the specified target function, in disabled state.
  81. // Parameters:
  82. // pTarget [in] A pointer to the target function, which will be
  83. // overridden by the detour function.
  84. // pDetour [in] A pointer to the detour function, which will override
  85. // the target function.
  86. // ppOriginal [out] A pointer to the trampoline function, which will be
  87. // used to call the original target function.
  88. // This parameter can be NULL.
  89. MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID *ppOriginal);
  90. // Creates a hook for the specified API function, in disabled state.
  91. // Parameters:
  92. // pszModule [in] A pointer to the loaded module name which contains the
  93. // target function.
  94. // pszProcName [in] A pointer to the target function name, which will be
  95. // overridden by the detour function.
  96. // pDetour [in] A pointer to the detour function, which will override
  97. // the target function.
  98. // ppOriginal [out] A pointer to the trampoline function, which will be
  99. // used to call the original target function.
  100. // This parameter can be NULL.
  101. MH_STATUS WINAPI MH_CreateHookApi(
  102. LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal);
  103. // Creates a hook for the specified API function, in disabled state.
  104. // Parameters:
  105. // pszModule [in] A pointer to the loaded module name which contains the
  106. // target function.
  107. // pszProcName [in] A pointer to the target function name, which will be
  108. // overridden by the detour function.
  109. // pDetour [in] A pointer to the detour function, which will override
  110. // the target function.
  111. // ppOriginal [out] A pointer to the trampoline function, which will be
  112. // used to call the original target function.
  113. // This parameter can be NULL.
  114. // ppTarget [out] A pointer to the target function, which will be used
  115. // with other functions.
  116. // This parameter can be NULL.
  117. MH_STATUS WINAPI MH_CreateHookApiEx(
  118. LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal, LPVOID *ppTarget);
  119. // Removes an already created hook.
  120. // Parameters:
  121. // pTarget [in] A pointer to the target function.
  122. MH_STATUS WINAPI MH_RemoveHook(LPVOID pTarget);
  123. // Enables an already created hook.
  124. // Parameters:
  125. // pTarget [in] A pointer to the target function.
  126. // If this parameter is MH_ALL_HOOKS, all created hooks are
  127. // enabled in one go.
  128. MH_STATUS WINAPI MH_EnableHook(LPVOID pTarget);
  129. // Disables an already created hook.
  130. // Parameters:
  131. // pTarget [in] A pointer to the target function.
  132. // If this parameter is MH_ALL_HOOKS, all created hooks are
  133. // disabled in one go.
  134. MH_STATUS WINAPI MH_DisableHook(LPVOID pTarget);
  135. // Queues to enable an already created hook.
  136. // Parameters:
  137. // pTarget [in] A pointer to the target function.
  138. // If this parameter is MH_ALL_HOOKS, all created hooks are
  139. // queued to be enabled.
  140. MH_STATUS WINAPI MH_QueueEnableHook(LPVOID pTarget);
  141. // Queues to disable an already created hook.
  142. // Parameters:
  143. // pTarget [in] A pointer to the target function.
  144. // If this parameter is MH_ALL_HOOKS, all created hooks are
  145. // queued to be disabled.
  146. MH_STATUS WINAPI MH_QueueDisableHook(LPVOID pTarget);
  147. // Applies all queued changes in one go.
  148. MH_STATUS WINAPI MH_ApplyQueued(VOID);
  149. // Translates the MH_STATUS to its name as a string.
  150. const char * WINAPI MH_StatusToString(MH_STATUS status);
  151. #ifdef __cplusplus
  152. }
  153. #endif