Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

616 lines
21 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: detours.h
  4. // Module: detours.lib
  5. //
  6. // Detours for binary functions. Version 1.4. (Build 45)
  7. //
  8. // Copyright 1995-2001, Microsoft Corporation
  9. //
  10. #pragma once
  11. #ifndef _DETOURS_H_
  12. #define _DETOURS_H_
  13. #pragma comment(lib, "detours")
  14. //////////////////////////////////////////////////////////////////////////////
  15. //
  16. #ifndef GUID_DEFINED
  17. #define GUID_DEFINED
  18. typedef struct _GUID
  19. {
  20. DWORD Data1;
  21. WORD Data2;
  22. WORD Data3;
  23. BYTE Data4[ 8 ];
  24. } GUID;
  25. #endif // !GUID_DEFINED
  26. #if defined(__cplusplus)
  27. #ifndef _REFGUID_DEFINED
  28. #define _REFGUID_DEFINED
  29. #define REFGUID const GUID &
  30. #endif // !_REFGUID_DEFINED
  31. #else // !__cplusplus
  32. #ifndef _REFGUID_DEFINED
  33. #define _REFGUID_DEFINED
  34. #define REFGUID const GUID * const
  35. #endif // !_REFGUID_DEFINED
  36. #endif // !__cplusplus
  37. //
  38. //////////////////////////////////////////////////////////////////////////////
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif // __cplusplus
  42. /////////////////////////////////////////////////// Instruction Target Macros.
  43. //
  44. #define DETOUR_INSTRUCTION_TARGET_NONE ((PBYTE)0)
  45. #define DETOUR_INSTRUCTION_TARGET_DYNAMIC ((PBYTE)~0ul)
  46. /////////////////////////////////////////////////////////// Trampoline Macros.
  47. //
  48. // DETOUR_TRAMPOLINE(trampoline_prototype, target_name)
  49. //
  50. // The naked trampoline must be at least DETOUR_TRAMPOLINE_SIZE bytes.
  51. //
  52. #define DETOUR_TRAMPOLINE_SIZE 32
  53. #define DETOUR_SECTION_HEADER_SIGNATURE 0x00727444 // "Dtr\0"
  54. #define DETOUR_TRAMPOLINE(trampoline,target) \
  55. static PVOID __fastcall _Detours_GetVA_##target(VOID) \
  56. { \
  57. return ⌖ \
  58. } \
  59. \
  60. __declspec(naked) trampoline \
  61. { \
  62. __asm { nop };\
  63. __asm { nop };\
  64. __asm { call _Detours_GetVA_##target };\
  65. __asm { jmp eax };\
  66. __asm { ret };\
  67. __asm { nop };\
  68. __asm { nop };\
  69. __asm { nop };\
  70. __asm { nop };\
  71. __asm { nop };\
  72. __asm { nop };\
  73. __asm { nop };\
  74. __asm { nop };\
  75. __asm { nop };\
  76. __asm { nop };\
  77. __asm { nop };\
  78. __asm { nop };\
  79. __asm { nop };\
  80. __asm { nop };\
  81. __asm { nop };\
  82. __asm { nop };\
  83. __asm { nop };\
  84. __asm { nop };\
  85. __asm { nop };\
  86. __asm { nop };\
  87. __asm { nop };\
  88. __asm { nop };\
  89. }
  90. #define DETOUR_TRAMPOLINE_EMPTY(trampoline) \
  91. __declspec(naked) trampoline \
  92. { \
  93. __asm { nop };\
  94. __asm { nop };\
  95. __asm { xor eax, eax };\
  96. __asm { mov eax, [eax] };\
  97. __asm { ret };\
  98. __asm { nop };\
  99. __asm { nop };\
  100. __asm { nop };\
  101. __asm { nop };\
  102. __asm { nop };\
  103. __asm { nop };\
  104. __asm { nop };\
  105. __asm { nop };\
  106. __asm { nop };\
  107. __asm { nop };\
  108. __asm { nop };\
  109. __asm { nop };\
  110. __asm { nop };\
  111. __asm { nop };\
  112. __asm { nop };\
  113. __asm { nop };\
  114. __asm { nop };\
  115. __asm { nop };\
  116. __asm { nop };\
  117. __asm { nop };\
  118. __asm { nop };\
  119. __asm { nop };\
  120. __asm { nop };\
  121. __asm { nop };\
  122. __asm { nop };\
  123. }
  124. /////////////////////////////////////////////////////////// Binary Structures.
  125. //
  126. #pragma pack(push, 8)
  127. typedef struct _DETOUR_SECTION_HEADER
  128. {
  129. DWORD cbHeaderSize;
  130. DWORD nSignature;
  131. DWORD nDataOffset;
  132. DWORD cbDataSize;
  133. DWORD nOriginalImportVirtualAddress;
  134. DWORD nOriginalImportSize;
  135. DWORD nOriginalBoundImportVirtualAddress;
  136. DWORD nOriginalBoundImportSize;
  137. DWORD nOriginalIatVirtualAddress;
  138. DWORD nOriginalIatSize;
  139. DWORD nOriginalSizeOfImage;
  140. DWORD nReserve;
  141. } DETOUR_SECTION_HEADER, *PDETOUR_SECTION_HEADER;
  142. typedef struct _DETOUR_SECTION_RECORD
  143. {
  144. DWORD cbBytes;
  145. DWORD nReserved;
  146. GUID guid;
  147. } DETOUR_SECTION_RECORD, *PDETOUR_SECTION_RECORD;
  148. #pragma pack(pop)
  149. #define DETOUR_SECTION_HEADER_DECLARE(cbSectionSize) \
  150. { \
  151. sizeof(DETOUR_SECTION_HEADER),\
  152. DETOUR_SECTION_HEADER_SIGNATURE,\
  153. sizeof(DETOUR_SECTION_HEADER),\
  154. (cbSectionSize),\
  155. \
  156. 0,\
  157. 0,\
  158. 0,\
  159. 0,\
  160. \
  161. 0,\
  162. 0,\
  163. 0,\
  164. 0,\
  165. }
  166. ///////////////////////////////////////////////////////////// Binary Typedefs.
  167. //
  168. typedef BOOL (CALLBACK *PF_DETOUR_BINARY_BYWAY_CALLBACK)(PVOID pContext,
  169. PCHAR pszFile,
  170. PCHAR *ppszOutFile);
  171. typedef BOOL (CALLBACK *PF_DETOUR_BINARY_FILE_CALLBACK)(PVOID pContext,
  172. PCHAR pszOrigFile,
  173. PCHAR pszFile,
  174. PCHAR *ppszOutFile);
  175. typedef BOOL (CALLBACK *PF_DETOUR_BINARY_SYMBOL_CALLBACK)(PVOID pContext,
  176. DWORD nOrdinal,
  177. PCHAR pszOrigSymbol,
  178. PCHAR pszSymbol,
  179. PCHAR *ppszOutSymbol);
  180. typedef BOOL (CALLBACK *PF_DETOUR_BINARY_FINAL_CALLBACK)(PVOID pContext);
  181. typedef BOOL (CALLBACK *PF_DETOUR_BINARY_EXPORT_CALLBACK)(PVOID pContext,
  182. DWORD nOrdinal,
  183. PCHAR pszName,
  184. PBYTE pbCode);
  185. typedef VOID * PDETOUR_BINARY;
  186. typedef VOID * PDETOUR_LOADED_BINARY;
  187. //////////////////////////////////////////////////////// Trampoline Functions.
  188. //
  189. PBYTE WINAPI DetourFunction(PBYTE pbTargetFunction,
  190. PBYTE pbDetourFunction);
  191. BOOL WINAPI DetourFunctionWithEmptyTrampoline(PBYTE pbTrampoline,
  192. PBYTE pbTarget,
  193. PBYTE pbDetour);
  194. BOOL WINAPI DetourFunctionWithEmptyTrampolineEx(PBYTE pbTrampoline,
  195. PBYTE pbTarget,
  196. PBYTE pbDetour,
  197. PBYTE *ppbRealTrampoline,
  198. PBYTE *ppbRealTarget,
  199. PBYTE *ppbRealDetour);
  200. BOOL WINAPI DetourFunctionWithTrampoline(PBYTE pbTrampoline,
  201. PBYTE pbDetour);
  202. BOOL WINAPI DetourFunctionWithTrampolineEx(PBYTE pbTrampoline,
  203. PBYTE pbDetour,
  204. PBYTE *ppbRealTrampoline,
  205. PBYTE *ppbRealTarget);
  206. BOOL WINAPI DetourRemove(PBYTE pbTrampoline, PBYTE pbDetour);
  207. ////////////////////////////////////////////////////////////// Code Functions.
  208. //
  209. PBYTE WINAPI DetourFindFunction(PCHAR pszModule, PCHAR pszFunction);
  210. PBYTE WINAPI DetourGetFinalCode(PBYTE pbCode, BOOL fSkipJmp);
  211. PBYTE WINAPI DetourCopyInstruction(PBYTE pbDst, PBYTE pbSrc, PBYTE *ppbTarget);
  212. PBYTE WINAPI DetourCopyInstructionEx(PBYTE pbDst,
  213. PBYTE pbSrc,
  214. PBYTE *ppbTarget,
  215. LONG *plExtra);
  216. ///////////////////////////////////////////////////// Loaded Binary Functions.
  217. //
  218. HMODULE WINAPI DetourEnumerateModules(HMODULE hModuleLast);
  219. PBYTE WINAPI DetourGetEntryPoint(HMODULE hModule);
  220. BOOL WINAPI DetourEnumerateExports(HMODULE hModule,
  221. PVOID pContext,
  222. PF_DETOUR_BINARY_EXPORT_CALLBACK pfExport);
  223. PBYTE WINAPI DetourFindPayload(HMODULE hModule, REFGUID rguid, DWORD *pcbData);
  224. DWORD WINAPI DetourGetSizeOfPayloads(HMODULE hModule);
  225. ///////////////////////////////////////////////// Persistent Binary Functions.
  226. //
  227. BOOL WINAPI DetourBinaryBindA(PCHAR pszFile, PCHAR pszDll, PCHAR pszPath);
  228. BOOL WINAPI DetourBinaryBindW(PWCHAR pwzFile, PWCHAR pwzDll, PWCHAR pwzPath);
  229. #ifdef UNICODE
  230. #define DetourBinaryBind DetourBinaryBindW
  231. #else
  232. #define DetourBinaryBind DetourBinaryBindA
  233. #endif // !UNICODE
  234. PDETOUR_BINARY WINAPI DetourBinaryOpen(HANDLE hFile);
  235. PBYTE WINAPI DetourBinaryEnumeratePayloads(PDETOUR_BINARY pBinary,
  236. GUID *pGuid,
  237. DWORD *pcbData,
  238. DWORD *pnIterator);
  239. PBYTE WINAPI DetourBinaryFindPayload(PDETOUR_BINARY pBinary,
  240. REFGUID rguid,
  241. DWORD *pcbData);
  242. PBYTE WINAPI DetourBinarySetPayload(PDETOUR_BINARY pBinary,
  243. REFGUID rguid,
  244. PBYTE pbData,
  245. DWORD cbData);
  246. BOOL WINAPI DetourBinaryDeletePayload(PDETOUR_BINARY pBinary, REFGUID rguid);
  247. BOOL WINAPI DetourBinaryPurgePayloads(PDETOUR_BINARY pBinary);
  248. BOOL WINAPI DetourBinaryResetImports(PDETOUR_BINARY pBinary);
  249. BOOL WINAPI DetourBinaryEditImports(PDETOUR_BINARY pBinary,
  250. PVOID pContext,
  251. PF_DETOUR_BINARY_BYWAY_CALLBACK pfByway,
  252. PF_DETOUR_BINARY_FILE_CALLBACK pfFile,
  253. PF_DETOUR_BINARY_SYMBOL_CALLBACK pfSymbol,
  254. PF_DETOUR_BINARY_FINAL_CALLBACK pfFinal);
  255. BOOL WINAPI DetourBinaryWrite(PDETOUR_BINARY pBinary, HANDLE hFile);
  256. BOOL WINAPI DetourBinaryClose(PDETOUR_BINARY pBinary);
  257. /////////////////////////////////////////////// First Chance Exception Filter.
  258. //
  259. LPTOP_LEVEL_EXCEPTION_FILTER WINAPI
  260. DetourFirstChanceExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelFilter);
  261. ///////////////////////////////////////////////// Create Process & Inject Dll.
  262. //
  263. typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEA)
  264. (LPCSTR lpApplicationName,
  265. LPSTR lpCommandLine,
  266. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  267. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  268. BOOL bInheritHandles,
  269. DWORD dwCreationFlags,
  270. LPVOID lpEnvironment,
  271. LPCSTR lpCurrentDirectory,
  272. LPSTARTUPINFOA lpStartupInfo,
  273. LPPROCESS_INFORMATION lpProcessInformation);
  274. typedef BOOL (WINAPI *PDETOUR_CREATE_PROCESS_ROUTINEW)
  275. (LPCWSTR lpApplicationName,
  276. LPWSTR lpCommandLine,
  277. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  278. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  279. BOOL bInheritHandles,
  280. DWORD dwCreationFlags,
  281. LPVOID lpEnvironment,
  282. LPCWSTR lpCurrentDirectory,
  283. LPSTARTUPINFOW lpStartupInfo,
  284. LPPROCESS_INFORMATION lpProcessInformation);
  285. BOOL WINAPI DetourCreateProcessWithDllA(LPCSTR lpApplicationName,
  286. LPSTR lpCommandLine,
  287. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  288. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  289. BOOL bInheritHandles,
  290. DWORD dwCreationFlags,
  291. LPVOID lpEnvironment,
  292. LPCSTR lpCurrentDirectory,
  293. LPSTARTUPINFOA lpStartupInfo,
  294. LPPROCESS_INFORMATION lpProcessInformation,
  295. LPCSTR lpDllName,
  296. PDETOUR_CREATE_PROCESS_ROUTINEA
  297. pfCreateProcessA);
  298. BOOL WINAPI DetourCreateProcessWithDllW(LPCWSTR lpApplicationName,
  299. LPWSTR lpCommandLine,
  300. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  301. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  302. BOOL bInheritHandles,
  303. DWORD dwCreationFlags,
  304. LPVOID lpEnvironment,
  305. LPCWSTR lpCurrentDirectory,
  306. LPSTARTUPINFOW lpStartupInfo,
  307. LPPROCESS_INFORMATION lpProcessInformation,
  308. LPCWSTR lpDllName,
  309. PDETOUR_CREATE_PROCESS_ROUTINEW
  310. pfCreateProcessW);
  311. #ifdef UNICODE
  312. #define DetourCreateProcessWithDll DetourCreateProcessWithDllW
  313. #define PDETOUR_CREATE_PROCESS_ROUTINE PDETOUR_CREATE_PROCESS_ROUTINEW
  314. #else
  315. #define DetourCreateProcessWithDll DetourCreateProcessWithDllA
  316. #define PDETOUR_CREATE_PROCESS_ROUTINE PDETOUR_CREATE_PROCESS_ROUTINEA
  317. #endif // !UNICODE
  318. BOOL WINAPI DetourContinueProcessWithDllA(HANDLE hProcess, LPCSTR lpDllName);
  319. BOOL WINAPI DetourContinueProcessWithDllW(HANDLE hProcess, LPCWSTR lpDllName);
  320. #ifdef UNICODE
  321. #define DetourContinueProcessWithDll DetourContinueProcessWithDllW
  322. #else
  323. #define DetourContinueProcessWithDll DetourContinueProcessWithDllA
  324. #endif // !UNICODE
  325. //
  326. //////////////////////////////////////////////////////////////////////////////
  327. #ifdef __cplusplus
  328. }
  329. #endif // __cplusplus
  330. /////////////////////////////////////////////////////////////////// Old Names.
  331. //
  332. #define ContinueProcessWithDll DetourContinueProcessWithDll
  333. #define ContinueProcessWithDllA DetourContinueProcessWithDllA
  334. #define ContinueProcessWithDllW DetourContinueProcessWithDllW
  335. #define CreateProcessWithDll DetourCreateProcessWithDll
  336. #define CreateProcessWithDllA DetourCreateProcessWithDllA
  337. #define CreateProcessWithDllW DetourCreateProcessWithDllW
  338. #define DETOUR_TRAMPOLINE_WO_TARGET DETOUR_TRAMPOLINE_EMPTY
  339. #define DetourBinaryPurgePayload DetourBinaryPurgePayloads
  340. #define DetourEnumerateExportsForInstance DetourEnumerateExports
  341. #define DetourEnumerateInstances DetourEnumerateModules
  342. #define DetourFindEntryPointForInstance DetourGetEntryPoint
  343. #define DetourFindFinalCode DetourGetFinalCode
  344. #define DetourFindPayloadInBinary DetourFindPayload
  345. #define DetourGetSizeOfBinary DetourGetSizeOfPayloads
  346. #define DetourRemoveWithTrampoline DetourRemove
  347. #define PCREATE_PROCESS_ROUTINE PDETOUR_CREATE_PROCESS_ROUTINE
  348. #define PCREATE_PROCESS_ROUTINEA PDETOUR_CREATE_PROCESS_ROUTINEA
  349. #define PCREATE_PROCESS_ROUTINEW PDETOUR_CREATE_PROCESS_ROUTINEW
  350. //
  351. //////////////////////////////////////////////// Detours Internal Definitions.
  352. //
  353. #ifdef __cplusplus
  354. #ifdef DETOURS_INTERNAL
  355. //////////////////////////////////////////////////////////////////////////////
  356. //
  357. #ifdef IMAGEAPI // defined by IMAGEHLP.H
  358. typedef LPAPI_VERSION (NTAPI *PF_ImagehlpApiVersionEx)(LPAPI_VERSION AppVersion);
  359. typedef BOOL (NTAPI *PF_SymInitialize)(IN HANDLE hProcess,
  360. IN LPSTR UserSearchPath,
  361. IN BOOL fInvadeProcess);
  362. typedef DWORD (NTAPI *PF_SymSetOptions)(IN DWORD SymOptions);
  363. typedef DWORD (NTAPI *PF_SymGetOptions)(VOID);
  364. typedef BOOL (NTAPI *PF_SymLoadModule)(IN HANDLE hProcess,
  365. IN HANDLE hFile,
  366. IN PSTR ImageName,
  367. IN PSTR ModuleName,
  368. IN DWORD BaseOfDll,
  369. IN DWORD SizeOfDll);
  370. typedef BOOL (NTAPI *PF_SymGetModuleInfo)(IN HANDLE hProcess,
  371. IN DWORD dwAddr,
  372. OUT PIMAGEHLP_MODULE ModuleInfo);
  373. typedef BOOL (NTAPI *PF_SymGetSymFromName)(IN HANDLE hProcess,
  374. IN LPSTR Name,
  375. OUT PIMAGEHLP_SYMBOL Symbol);
  376. typedef BOOL (NTAPI *PF_BindImage)(IN LPSTR pszImageName,
  377. IN LPSTR pszDllPath,
  378. IN LPSTR pszSymbolPath);
  379. typedef struct _DETOUR_SYM_INFO
  380. {
  381. HANDLE hProcess;
  382. HMODULE hImageHlp;
  383. PF_ImagehlpApiVersionEx pfImagehlpApiVersionEx;
  384. PF_SymInitialize pfSymInitialize;
  385. PF_SymSetOptions pfSymSetOptions;
  386. PF_SymGetOptions pfSymGetOptions;
  387. PF_SymLoadModule pfSymLoadModule;
  388. PF_SymGetModuleInfo pfSymGetModuleInfo;
  389. PF_SymGetSymFromName pfSymGetSymFromName;
  390. PF_BindImage pfBindImage;
  391. } DETOUR_SYM_INFO, *PDETOUR_SYM_INFO;
  392. PDETOUR_SYM_INFO DetourLoadImageHlp(VOID);
  393. #endif // IMAGEAPI
  394. //////////////////////////////////////////////////////////////////////////////
  395. //
  396. class CDetourEnableWriteOnCodePage
  397. {
  398. public:
  399. CDetourEnableWriteOnCodePage(PBYTE pbCode, LONG cbCode = DETOUR_TRAMPOLINE_SIZE)
  400. {
  401. m_pbCode = pbCode;
  402. m_cbCode = cbCode;
  403. m_dwOldPerm = 0;
  404. m_hProcess = GetCurrentProcess();
  405. if (m_pbCode && m_cbCode) {
  406. if (!FlushInstructionCache(m_hProcess, pbCode, cbCode)) {
  407. return;
  408. }
  409. if (!VirtualProtect(pbCode,
  410. cbCode,
  411. PAGE_EXECUTE_READWRITE,
  412. &m_dwOldPerm)) {
  413. return;
  414. }
  415. }
  416. }
  417. ~CDetourEnableWriteOnCodePage()
  418. {
  419. if (m_dwOldPerm && m_pbCode && m_cbCode) {
  420. DWORD dwTemp = 0;
  421. if (!FlushInstructionCache(m_hProcess, m_pbCode, m_cbCode)) {
  422. return;
  423. }
  424. if (!VirtualProtect(m_pbCode, m_cbCode, m_dwOldPerm, &dwTemp)) {
  425. return;
  426. }
  427. }
  428. }
  429. BOOL SetPermission(DWORD dwPerms)
  430. {
  431. if (m_dwOldPerm && m_pbCode && m_cbCode) {
  432. m_dwOldPerm = dwPerms;
  433. return TRUE;
  434. }
  435. return FALSE;
  436. }
  437. BOOL IsValid(VOID)
  438. {
  439. return m_pbCode && m_cbCode && m_dwOldPerm;
  440. }
  441. private:
  442. HANDLE m_hProcess;
  443. PBYTE m_pbCode;
  444. LONG m_cbCode;
  445. DWORD m_dwOldPerm;
  446. };
  447. //////////////////////////////////////////////////////////////////////////////
  448. //
  449. inline PBYTE DetourGenMovEax(PBYTE pbCode, UINT32 nValue)
  450. {
  451. *pbCode++ = 0xB8;
  452. *((UINT32*&)pbCode)++ = nValue;
  453. return pbCode;
  454. }
  455. inline PBYTE DetourGenMovEbx(PBYTE pbCode, UINT32 nValue)
  456. {
  457. *pbCode++ = 0xBB;
  458. *((UINT32*&)pbCode)++ = nValue;
  459. return pbCode;
  460. }
  461. inline PBYTE DetourGenMovEcx(PBYTE pbCode, UINT32 nValue)
  462. {
  463. *pbCode++ = 0xB9;
  464. *((UINT32*&)pbCode)++ = nValue;
  465. return pbCode;
  466. }
  467. inline PBYTE DetourGenMovEdx(PBYTE pbCode, UINT32 nValue)
  468. {
  469. *pbCode++ = 0xBA;
  470. *((UINT32*&)pbCode)++ = nValue;
  471. return pbCode;
  472. }
  473. inline PBYTE DetourGenMovEsi(PBYTE pbCode, UINT32 nValue)
  474. {
  475. *pbCode++ = 0xBE;
  476. *((UINT32*&)pbCode)++ = nValue;
  477. return pbCode;
  478. }
  479. inline PBYTE DetourGenMovEdi(PBYTE pbCode, UINT32 nValue)
  480. {
  481. *pbCode++ = 0xBF;
  482. *((UINT32*&)pbCode)++ = nValue;
  483. return pbCode;
  484. }
  485. inline PBYTE DetourGenMovEbp(PBYTE pbCode, UINT32 nValue)
  486. {
  487. *pbCode++ = 0xBD;
  488. *((UINT32*&)pbCode)++ = nValue;
  489. return pbCode;
  490. }
  491. inline PBYTE DetourGenMovEsp(PBYTE pbCode, UINT32 nValue)
  492. {
  493. *pbCode++ = 0xBC;
  494. *((UINT32*&)pbCode)++ = nValue;
  495. return pbCode;
  496. }
  497. inline PBYTE DetourGenPush(PBYTE pbCode, UINT32 nValue)
  498. {
  499. *pbCode++ = 0x68;
  500. *((UINT32*&)pbCode)++ = nValue;
  501. return pbCode;
  502. }
  503. inline PBYTE DetourGenPushad(PBYTE pbCode)
  504. {
  505. *pbCode++ = 0x60;
  506. return pbCode;
  507. }
  508. inline PBYTE DetourGenPopad(PBYTE pbCode)
  509. {
  510. *pbCode++ = 0x61;
  511. return pbCode;
  512. }
  513. inline PBYTE DetourGenJmp(PBYTE pbCode, PBYTE pbJmpDst, PBYTE pbJmpSrc = 0)
  514. {
  515. if (pbJmpSrc == 0) {
  516. pbJmpSrc = pbCode;
  517. }
  518. *pbCode++ = 0xE9;
  519. *((INT32*&)pbCode)++ = pbJmpDst - (pbJmpSrc + 5);
  520. return pbCode;
  521. }
  522. inline PBYTE DetourGenCall(PBYTE pbCode, PBYTE pbJmpDst, PBYTE pbJmpSrc = 0)
  523. {
  524. if (pbJmpSrc == 0) {
  525. pbJmpSrc = pbCode;
  526. }
  527. *pbCode++ = 0xE8;
  528. *((INT32*&)pbCode)++ = pbJmpDst - (pbJmpSrc + 5);
  529. return pbCode;
  530. }
  531. inline PBYTE DetourGenBreak(PBYTE pbCode)
  532. {
  533. *pbCode++ = 0xcc;
  534. return pbCode;
  535. }
  536. inline PBYTE DetourGenRet(PBYTE pbCode)
  537. {
  538. *pbCode++ = 0xc3;
  539. return pbCode;
  540. }
  541. inline PBYTE DetourGenNop(PBYTE pbCode)
  542. {
  543. *pbCode++ = 0x90;
  544. return pbCode;
  545. }
  546. #endif DETOURS_INTERAL
  547. #endif // __cplusplus
  548. #endif // _DETOURS_H_
  549. //
  550. //////////////////////////////////////////////////////////////// End of File.