Source code of Windows XP (NT5)
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.

182 lines
6.2 KiB

  1. /******************************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. WindowDetours.cpp
  5. Abstract:
  6. This file contains the trampolines for the detour of System functions.
  7. Revision History:
  8. Davide Massarenti (dmassare) 10/31/99
  9. created
  10. ******************************************************************************/
  11. #include "stdafx.h"
  12. ////////////////////////////////////////////////////////////////////////////////////////
  13. typedef std::map<HWND, DWORD> WindowsMap;
  14. typedef WindowsMap::iterator WindowsIter;
  15. static WindowsMap s_mapWindows;
  16. ////////////////////////////////////////////////////////////////////////////////////////
  17. DETOUR_TRAMPOLINE( HWND WINAPI Trampoline_CreateWindowExA( DWORD ,
  18. LPCSTR ,
  19. LPCSTR ,
  20. DWORD ,
  21. int ,
  22. int ,
  23. int ,
  24. int ,
  25. HWND ,
  26. HMENU ,
  27. HANDLE ,
  28. LPVOID ), CreateWindowExA );
  29. HWND WINAPI Detour_CreateWindowExA( DWORD dwExStyle ,
  30. LPCSTR lpClassName , // pointer to registered class name
  31. LPCSTR lpWindowName, // pointer to window name
  32. DWORD dwStyle , // window style
  33. int x , // horizontal position of window
  34. int y , // vertical position of window
  35. int nWidth , // window width
  36. int nHeight , // window height
  37. HWND hWndParent , // handle to parent or owner window
  38. HMENU hMenu , // menu handle or child identifier
  39. HANDLE hInstance , // handle to application instance
  40. LPVOID lpParam ) // window-creation data
  41. {
  42. HWND hwnd;
  43. hwnd = Trampoline_CreateWindowExA( dwExStyle ,
  44. lpClassName ,
  45. lpWindowName,
  46. dwStyle ,
  47. x ,
  48. y ,
  49. nWidth ,
  50. nHeight ,
  51. hWndParent ,
  52. hMenu ,
  53. hInstance ,
  54. lpParam );
  55. DebugLog( "%%%% CreateWindowExA %08lx : '%s'\n", hwnd, lpWindowName ? lpWindowName : "" );
  56. if(hwnd)
  57. {
  58. s_mapWindows[hwnd] = ::GetCurrentThreadId();
  59. }
  60. return hwnd;
  61. }
  62. ////////////////////////////////////////////////////////////////////////////////////////
  63. DETOUR_TRAMPOLINE( HWND WINAPI Trampoline_CreateWindowExW( DWORD ,
  64. LPCWSTR ,
  65. LPCWSTR ,
  66. DWORD ,
  67. int ,
  68. int ,
  69. int ,
  70. int ,
  71. HWND ,
  72. HMENU ,
  73. HANDLE ,
  74. LPVOID ), CreateWindowExW );
  75. HWND WINAPI Detour_CreateWindowExW( DWORD dwExStyle ,
  76. LPCWSTR lpClassName , // pointer to registered class name
  77. LPCWSTR lpWindowName, // pointer to window name
  78. DWORD dwStyle , // window style
  79. int x , // horizontal position of window
  80. int y , // vertical position of window
  81. int nWidth , // window width
  82. int nHeight , // window height
  83. HWND hWndParent , // handle to parent or owner window
  84. HMENU hMenu , // menu handle or child identifier
  85. HANDLE hInstance , // handle to application instance
  86. LPVOID lpParam ) // window-creation data
  87. {
  88. HWND hwnd;
  89. hwnd = Trampoline_CreateWindowExW( dwExStyle ,
  90. lpClassName ,
  91. lpWindowName,
  92. dwStyle ,
  93. x ,
  94. y ,
  95. nWidth ,
  96. nHeight ,
  97. hWndParent ,
  98. hMenu ,
  99. hInstance ,
  100. lpParam );
  101. DebugLog( L"%%%% CreateWindowExW %08lx : '%s'\n", hwnd, lpWindowName ? lpWindowName : L"" );
  102. if(hwnd)
  103. {
  104. s_mapWindows[hwnd] = ::GetCurrentThreadId();
  105. }
  106. return hwnd;
  107. }
  108. ////////////////////////////////////////////////////////////////////////////////
  109. DETOUR_TRAMPOLINE( BOOL WINAPI Trampoline_DestroyWindow( HWND ), DestroyWindow );
  110. BOOL WINAPI Detour_DestroyWindow( HWND hWnd ) // handle to window to destroy
  111. {
  112. BOOL res;
  113. WindowsIter it;
  114. DebugLog( "%%%% DestroyWindow %08lx\n", hWnd );
  115. it = s_mapWindows.find( hWnd );
  116. if(it != s_mapWindows.end())
  117. {
  118. if(it->second != ::GetCurrentThreadId())
  119. {
  120. // Window destroyed from the wrong thread!!
  121. DebugBreak();
  122. }
  123. s_mapWindows.erase( it );
  124. }
  125. else
  126. {
  127. // Window already destroyed!!
  128. DebugBreak();
  129. }
  130. res = Trampoline_DestroyWindow( hWnd );
  131. return res;
  132. }
  133. ////////////////////////////////////////////////////////////////////////////////
  134. void WindowDetours_Setup()
  135. {
  136. DetourFunctionWithTrampoline( (PBYTE)Trampoline_CreateWindowExA, (PBYTE)Detour_CreateWindowExA );
  137. DetourFunctionWithTrampoline( (PBYTE)Trampoline_CreateWindowExW, (PBYTE)Detour_CreateWindowExW );
  138. DetourFunctionWithTrampoline( (PBYTE)Trampoline_DestroyWindow , (PBYTE)Detour_DestroyWindow );
  139. }
  140. void WindowDetours_Remove()
  141. {
  142. DetourRemoveWithTrampoline( (PBYTE)Trampoline_CreateWindowExA, (PBYTE)Detour_CreateWindowExA );
  143. DetourRemoveWithTrampoline( (PBYTE)Trampoline_CreateWindowExW, (PBYTE)Detour_CreateWindowExW );
  144. DetourRemoveWithTrampoline( (PBYTE)Trampoline_DestroyWindow , (PBYTE)Detour_DestroyWindow );
  145. }