iatpatch.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #ifndef __IATPACH_H__
  2. #define __IATPACH_H__
  3. #pragma once
  4. #include "config.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. // note: you need to keep this one from windbg
  9. #include "DbgHelp.h"
  10. // This structure allows us to build a table of APIs which should be patched
  11. // through to replacement functions provided by VLD.
  12. typedef struct patchentry_s
  13. {
  14. LPCSTR exportmodulename; // The name of the module exporting the patched API.
  15. LPCSTR importname; // The name (or ordinal) of the imported API being patched.
  16. LPSTR modulepath; // (Optional) module path. If the module is loaded, then VLD will fill this in at startup.
  17. LPCVOID replacement; // Pointer to the function to which the imported API should be patched through to.
  18. } patchentry_t;
  19. // pe_is_import - Determines if the specified module imports the named import
  20. // from the named exporting module.
  21. //
  22. // Caution: This function is not thread-safe. It calls into the Debug Help
  23. // Library which is single-threaded. Therefore, calls to this function must
  24. // be synchronized.
  25. //
  26. // - importmodule (IN): Handle (base address) of the module to be searched to
  27. // see if it imports the specified import.
  28. //
  29. // - exportmodulename (IN): ANSI string containing the name of the module that
  30. // exports the import to be searched for.
  31. //
  32. // - importname (IN): ANSI string containing the name of the import to search
  33. // for. May be an integer cast to a string if the import is exported by
  34. // ordinal.
  35. //
  36. // Return Value:
  37. //
  38. // Returns TRUE if the module imports to the specified import. Otherwise
  39. // returns FALSE.
  40. //
  41. TOOLKIT_API BOOL pe_is_import_entry(HMODULE importmodule, LPCSTR exportmodulename, LPCSTR importname);
  42. // pe_is_patch - Determines if the specified module has been patched to use the
  43. // specified replacement.
  44. //
  45. // Caution: This function is not thread-safe. It calls into the Debug Help
  46. // Library which is single-threaded. Therefore, calls to this function must
  47. // be synchronized.
  48. //
  49. // - importmodule (IN): Handle (base address) of the module to be searched to
  50. // see if it imports the specified replacement export.
  51. //
  52. // - exportmodulename (IN): ANSI string containing the name of the module that
  53. // normally exports that import that would have been patched to use the
  54. // replacement export.
  55. //
  56. // - replacement (IN): Address of the replacement, or destination, function or
  57. // variable to search for.
  58. //
  59. // Return Value:
  60. //
  61. // Returns TRUE if the module has been patched to use the specified
  62. // replacement export.
  63. //
  64. TOOLKIT_API BOOL pe_is_patch_entry(HMODULE importmodule, LPCSTR exportmodulename, LPCVOID replacement);
  65. // pe_is_patch - Checks to see if any of the imports listed in the specified
  66. // patch table have been patched into the specified importmodule.
  67. //
  68. // Caution: This function is not thread-safe. It calls into the Debug Help
  69. // Library which is single-threaded. Therefore, calls to this function must
  70. // be synchronized.
  71. //
  72. // - importmodule (IN): Handle (base address) of the module to be queried to
  73. // determine if it has been patched.
  74. //
  75. // - patchtable (IN): An array of patchentry_t structures specifying all of the
  76. // import patches to search for.
  77. //
  78. // - tablesize (IN): Size, in entries, of the specified patch table.
  79. //
  80. // Return Value:
  81. //
  82. // Returns TRUE if at least one of the patches listed in the patch table is
  83. // installed in the importmodule. Otherwise returns FALSE.
  84. //
  85. TOOLKIT_API BOOL pe_is_patch(HMODULE importmodule, patchentry_t patchtable [], UINT tablesize);
  86. // pe_patch_import_str - Patches all future calls to an imported function, or references
  87. // to an imported variable, through to a replacement function or variable.
  88. // Patching is done by replacing the import's address in the specified target
  89. // module's Import Address Table (IAT) with the address of the replacement
  90. // function or variable.
  91. //
  92. // Caution: This function is not thread-safe. It calls into the Debug Help
  93. // Library which is single-threaded. Therefore, calls to this function must
  94. // be synchronized.
  95. //
  96. // - importmodule (IN): Handle (base address) of the target module for which
  97. // calls or references to the import should be patched.
  98. //
  99. // - exportmodulename (IN): ANSI string containing the name of the module that
  100. // exports the function of variable to be patched. This parameter must not
  101. // be NULL.
  102. //
  103. // - exportmodulepath (IN): (Optional) ANSI string containing the fully
  104. // qualified path of the module that exports the function or variable to be
  105. // patched. This parameter may be NULL. However, it is recommended that
  106. // this parameter be provided if possible, especially for any DLLs that may
  107. // reside in the side-by-side cache.
  108. //
  109. // - importname (IN): ANSI string containing the name of the imported function
  110. // or variable to be patched. May be an integer cast to a string if the
  111. // import is exported by ordinal.
  112. //
  113. // - replacement (IN): Address of the function or variable to which future
  114. // calls or references should be patched through to. This function or
  115. // variable can be thought of as effectively replacing the original import
  116. // from the point of view of the module specified by "importmodule".
  117. //
  118. // Return Value:
  119. //
  120. // Returns TRUE if the patch was installed into the import module. If the
  121. // import module does not import the specified export, so nothing changed,
  122. // then FALSE will be returned.
  123. //
  124. TOOLKIT_API BOOL pe_patch_entry(HMODULE importmodule, LPCSTR exportmodulename, LPCSTR exportmodulepath, LPCSTR importname,
  125. LPCVOID replacement);
  126. // pe_patch - Patches all imports listed in the supplied patch table, and
  127. // which are imported by the specified module, through to their respective
  128. // replacement functions.
  129. //
  130. // Caution: This function is not thread-safe. It calls into the Debug Help
  131. // Library which is single-threaded. Therefore, calls to this function must
  132. // be synchronized.
  133. //
  134. // Note: If the specified module does not import any of the functions listed
  135. // in the patch table, then nothing is changed for the specified module.
  136. //
  137. // - importmodule (IN): Handle (base address) of the target module which is to
  138. // have its imports patched.
  139. //
  140. // - patchtable (IN): An array of patchentry_t structures specifying all of the
  141. // imports to patch for the specified module.
  142. //
  143. // - tablesize (IN): Size, in entries, of the specified patch table.
  144. //
  145. // Return Value:
  146. //
  147. // Returns TRUE if at least one of the patches listed in the patch table was
  148. // installed in the importmodule. Otherwise returns FALSE.
  149. //
  150. TOOLKIT_API BOOL pe_patch(HMODULE importmodule, patchentry_t patchtable [], UINT tablesize);
  151. // pe_restore_patch_entry - Restores the IAT entry for an import previously patched via
  152. // a call to "patchimport" to the original address of the import.
  153. //
  154. // Caution: This function is not thread-safe. It calls into the Debug Help
  155. // Library which is single-threaded. Therefore, calls to this function must
  156. // be synchronized.
  157. //
  158. // - importmodule (IN): Handle (base address) of the target module for which
  159. // calls or references to the import should be restored.
  160. //
  161. // - exportmodulename (IN): ANSI string containing the name of the module that
  162. // exports the function of variable to be patched. This prarameter must not
  163. // be NULL.
  164. //
  165. // - exportmodulepath (IN): (Optional) ANSI string containing the fully
  166. // qualified path of the module that exports the function or variable to be
  167. // patched. This parameter may be NULL. However, it is recommended that
  168. // this parameter be provided if possible, especially for any DLLs that may
  169. // reside in the side-by-side cache.
  170. //
  171. // - importname (IN): ANSI string containing the name of the imported function
  172. // or variable to be restored. May be an integer cast to a string if the
  173. // import is exported by ordinal.
  174. //
  175. // - replacement (IN): Address of the function or variable which the import was
  176. // previously patched through to via a call to "patchimport".
  177. //
  178. // Return Value:
  179. //
  180. // None.
  181. //
  182. TOOLKIT_API VOID pe_restore_patch_entry(HMODULE importmodule, LPCSTR exportmodulename, LPCSTR exportmodulepath, LPCSTR importname,
  183. LPCVOID replacement);
  184. // restoremodule - Restores all imports listed in the supplied patch table, and
  185. // which are imported by the specified module, to their original functions.
  186. //
  187. // Caution: This function is not thread-safe. It calls into the Debug Help
  188. // Library which is single-threaded. Therefore, calls to this function must
  189. // be synchronized.
  190. //
  191. // Note: If the specified module does not import any of the functions listed
  192. // in the patch table, then nothing is changed for the specified module.
  193. //
  194. // - importmodule (IN): Handle (base address) of the target module which is to
  195. // have its imports restored.
  196. //
  197. // - patchtable (IN): Array of patchentry_t structures specifying all of the
  198. // imports to restore for the specified module.
  199. //
  200. // - tablesize (IN): Size, in entries, of the specified patch table.
  201. //
  202. // Return Value:
  203. //
  204. // None.
  205. //
  206. TOOLKIT_API VOID pe_restore_patch(HMODULE importmodule, patchentry_t patchtable [], UINT tablesize);
  207. #ifdef __cplusplus
  208. } // extern "C" {
  209. #endif
  210. #endif //__IATPACH_H__