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.

326 lines
9.3 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. EncompassMonitor.cpp
  5. Abstract:
  6. Filters messages from the apps CBT WindowsHook.
  7. Notes:
  8. This is a general purpose shim.
  9. History:
  10. 1/30/2001 a-larrsh Created
  11. --*/
  12. #include "precomp.h"
  13. IMPLEMENT_SHIM_BEGIN(EncompassMonitor)
  14. #include "ShimHookMacro.h"
  15. APIHOOK_ENUM_BEGIN
  16. APIHOOK_ENUM_ENTRY(SetWindowsHookExA)
  17. APIHOOK_ENUM_END
  18. // Local Hook Information
  19. HHOOK g_hCBTHook = NULL;
  20. HOOKPROC g_OriginalEncompassMonitorCBTProc = NULL;
  21. // Shared Data Infomation
  22. #define SHARED_SECTION_NAME "EncompassMonitor_SharedMemoryData"
  23. typedef struct
  24. {
  25. char szModuleFileName[MAX_PATH];
  26. HANDLE hModule;
  27. HOOKPROC pfnHookProc;
  28. } SHARED_HOOK_INFO, *PSHARED_HOOK_INFO;
  29. HANDLE g_hSharedMapping = NULL;
  30. PSHARED_HOOK_INFO g_pSharedHookInfo = NULL;
  31. // Creates Shared memory. Only called by the originial SHIM
  32. void CreateSharedMemory(HMODULE hModule, HOOKPROC pfnHookProc)
  33. {
  34. HANDLE hSharedFile;
  35. char szTempPath[MAX_PATH];
  36. char szTempFileName[MAX_PATH];
  37. DWORD dwTemp;
  38. // create the memory mapped file necessary to comunicate between the original Instanace of SHIM
  39. // and the following instances of SHIMS
  40. if (GetTempPathA(sizeof(szTempPath), szTempPath) == 0)
  41. {
  42. DPFN( eDbgLevelError, "GetTempPath failed\n");
  43. goto errCreateSharedSection;
  44. }
  45. if (GetTempFileNameA(szTempPath, "mem", NULL, szTempFileName) == 0)
  46. {
  47. DPFN( eDbgLevelError, "GetTempFileName failed\n");
  48. goto errCreateSharedSection;
  49. }
  50. hSharedFile = CreateFileA( szTempFileName,
  51. GENERIC_READ | GENERIC_WRITE,
  52. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  53. NULL,
  54. CREATE_ALWAYS,
  55. FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
  56. NULL);
  57. if (hSharedFile == NULL)
  58. {
  59. DPFN( eDbgLevelError, "CreateFile failed to create '%s'\n", szTempFileName);
  60. goto errCreateSharedSection;
  61. }
  62. // Increase size of file (create the mapping)
  63. g_hSharedMapping = CreateFileMappingA( hSharedFile,
  64. NULL,
  65. PAGE_READWRITE,
  66. NULL,
  67. sizeof(SHARED_HOOK_INFO),
  68. SHARED_SECTION_NAME);
  69. if (g_hSharedMapping == NULL)
  70. {
  71. DPFN( eDbgLevelError, "CreateFileMapping failed\n");
  72. goto errCreateSharedSection;
  73. }
  74. g_pSharedHookInfo = (PSHARED_HOOK_INFO)MapViewOfFile(g_hSharedMapping,
  75. FILE_MAP_ALL_ACCESS,
  76. 0,
  77. 0,
  78. sizeof(SHARED_HOOK_INFO));
  79. if (g_pSharedHookInfo == NULL)
  80. {
  81. DWORD dwErr = GetLastError();
  82. DPFN( eDbgLevelError, "MapViewOfFile failed [%d]", (int)dwErr);
  83. goto errCreateSharedSection;
  84. }
  85. CloseHandle(hSharedFile);
  86. g_pSharedHookInfo->hModule = hModule;
  87. g_pSharedHookInfo->pfnHookProc = pfnHookProc;
  88. GetModuleFileNameA(hModule, g_pSharedHookInfo->szModuleFileName, MAX_PATH);
  89. if (!FlushViewOfFile(g_pSharedHookInfo, sizeof(SHARED_HOOK_INFO)))
  90. {
  91. DPFN( eDbgLevelError, "FlushViewOfFile failed\n");
  92. goto errCreateSharedSection;
  93. }
  94. DPFN( eDbgLevelInfo, "WRITE::Shared Section Successful");
  95. DPFN( eDbgLevelInfo, "WRITE::g_pSharedHookInfo->hModule=%x", g_pSharedHookInfo->hModule);
  96. DPFN( eDbgLevelInfo, "WRITE::g_pSharedHookInfo->pfnHookProc=%x", g_pSharedHookInfo->pfnHookProc);
  97. DPFN( eDbgLevelInfo, "WRITE::g_pSharedHookInfo->szModuleFileName=%s", g_pSharedHookInfo->szModuleFileName);
  98. return;
  99. errCreateSharedSection:
  100. DPFN( eDbgLevelError, "WRITE::Shared Section FAILED");
  101. return;
  102. }
  103. // Gets Shared Memory - Only called by injected versions of hook function
  104. void GetSharedMemory()
  105. {
  106. HANDLE hSharedFileMapping = NULL;
  107. void *pSharedMem = NULL;
  108. hSharedFileMapping = OpenFileMappingA( FILE_MAP_ALL_ACCESS,
  109. FALSE,
  110. SHARED_SECTION_NAME);
  111. if (hSharedFileMapping != NULL)
  112. {
  113. PSHARED_HOOK_INFO pSharedHookInfo = (PSHARED_HOOK_INFO)MapViewOfFile( hSharedFileMapping,
  114. FILE_MAP_ALL_ACCESS,
  115. 0,
  116. 0,
  117. 0);
  118. if (pSharedHookInfo)
  119. {
  120. DPFN( eDbgLevelInfo, "READ::pSharedHookInfo->hModule=%x", pSharedHookInfo->hModule);
  121. DPFN( eDbgLevelInfo, "READ::pSharedHookInfo->pfnHookProc=%x", pSharedHookInfo->pfnHookProc);
  122. DPFN( eDbgLevelInfo, "READ::pSharedHookInfo->szModuleFileName=%s", pSharedHookInfo->szModuleFileName);
  123. // Load DLL with origianl CBT Proc in it.
  124. HANDLE hMod = LoadLibraryA(pSharedHookInfo->szModuleFileName);
  125. if (!hMod)
  126. {
  127. DPFN( eDbgLevelError, "LoadLibrary(\"%s\") - FAILED", pSharedHookInfo->szModuleFileName);
  128. }
  129. g_OriginalEncompassMonitorCBTProc = (HOOKPROC)((DWORD)hMod + ((DWORD)pSharedHookInfo->pfnHookProc) - (DWORD)pSharedHookInfo->hModule);
  130. DPFN( eDbgLevelInfo, "READ::Shared Section Successful - Original Hook at %x", g_OriginalEncompassMonitorCBTProc);
  131. CloseHandle(hSharedFileMapping);
  132. UnmapViewOfFile(pSharedHookInfo);
  133. }
  134. else
  135. {
  136. DPFN( eDbgLevelError, "MapViewOfFile() Failed");
  137. }
  138. }
  139. else
  140. {
  141. DPFN( eDbgLevelError, "READ::Shared Section Failed");
  142. }
  143. }
  144. // Replacement CBT Hook function
  145. LRESULT CALLBACK Filtered_EncompassMonitorCBTProc(
  146. int nCode, // hook code
  147. WPARAM wParam, // depends on hook code
  148. LPARAM lParam // depends on hook code
  149. )
  150. {
  151. LRESULT lResult = 0; // Allow operation to continue
  152. bool bFilterMessage = false;
  153. if(g_OriginalEncompassMonitorCBTProc == NULL)
  154. {
  155. GetSharedMemory();
  156. }
  157. if (nCode == HCBT_CREATEWND)
  158. {
  159. CBT_CREATEWNDA *pccw = (CBT_CREATEWNDA*)lParam;
  160. if ( (IS_INTRESOURCE(pccw->lpcs->lpszClass)) )
  161. {
  162. char szBuf[256];
  163. GetClassNameA((HWND)wParam, szBuf, 255);
  164. bFilterMessage=true;
  165. DPFN( eDbgLevelInfo, "[%x] - Filtered_EncompassMonitorCBTProc::HCBT_CREATEWND %s [ATOM CLASS FILTERED]", g_OriginalEncompassMonitorCBTProc, szBuf);
  166. }
  167. else
  168. {
  169. DPFN( eDbgLevelInfo, "[%x] - Filtered_EncompassMonitorCBTProc::HCBT_CREATEWND %s ", g_OriginalEncompassMonitorCBTProc, pccw->lpcs->lpszClass);
  170. }
  171. }
  172. if ( g_OriginalEncompassMonitorCBTProc )
  173. {
  174. if (bFilterMessage)
  175. {
  176. lResult = CallNextHookEx(g_hCBTHook, nCode, wParam, lParam);
  177. }
  178. else
  179. {
  180. lResult = g_OriginalEncompassMonitorCBTProc(nCode, wParam, lParam);
  181. }
  182. }
  183. else
  184. {
  185. DPFN( eDbgLevelError, "Filtered_EncompassMonitorCBTProc:: ** BAD g_OriginalEncompassMonitorCBTProc2 **");
  186. lResult = CallNextHookEx(g_hCBTHook, nCode, wParam, lParam);
  187. }
  188. return lResult;
  189. }
  190. // SHIMMED API
  191. HHOOK APIHOOK(SetWindowsHookExA)(
  192. int idHook, // hook type
  193. HOOKPROC lpfn, // hook procedure
  194. HINSTANCE hMod, // handle to application instance
  195. DWORD dwThreadId // thread identifier
  196. )
  197. {
  198. static int nNumCBThooks = 0;
  199. HHOOK hHook;
  200. if (idHook == WH_CBT)
  201. {
  202. nNumCBThooks++;
  203. switch(nNumCBThooks)
  204. {
  205. case 1:
  206. hHook = ORIGINAL_API(SetWindowsHookExA)(idHook, lpfn, hMod, dwThreadId);
  207. DPFN( eDbgLevelInfo, "%x=SetWindowsHookEx(%d, %x, %x, %x) - Ignoring First Hook Call", hHook, idHook, lpfn, hMod, dwThreadId);
  208. break;
  209. case 2:
  210. g_OriginalEncompassMonitorCBTProc = lpfn;
  211. g_hCBTHook = hHook = ORIGINAL_API(SetWindowsHookExA)(idHook, Filtered_EncompassMonitorCBTProc, g_hinstDll, dwThreadId);
  212. DPFN( eDbgLevelInfo, "%x=SetWindowsHookEx(%d, %x, %x, %x) - Replacing Hook with Filtered_EncompassMonitorCBTProc", hHook, idHook, lpfn, hMod, dwThreadId);
  213. CreateSharedMemory(hMod, lpfn);
  214. break;
  215. default:
  216. hHook = ORIGINAL_API(SetWindowsHookExA)(idHook, lpfn, hMod, dwThreadId);
  217. DPFN( eDbgLevelError, "SetWindowsHookEx -- More then 2 WH_CBT hooks [%d]", nNumCBThooks);
  218. break;
  219. }
  220. }
  221. else
  222. {
  223. hHook = ORIGINAL_API(SetWindowsHookExA)(idHook, lpfn, hMod, dwThreadId);
  224. }
  225. return hHook;
  226. }
  227. BOOL
  228. NOTIFY_FUNCTION(DWORD fdwReason)
  229. {
  230. if (fdwReason == DLL_PROCESS_DETACH)
  231. {
  232. if (g_hSharedMapping)
  233. {
  234. CloseHandle(g_hSharedMapping);
  235. g_hSharedMapping = NULL;
  236. }
  237. if (g_pSharedHookInfo)
  238. {
  239. UnmapViewOfFile(g_pSharedHookInfo);
  240. g_pSharedHookInfo = NULL;
  241. }
  242. }
  243. return TRUE;
  244. }
  245. /*++
  246. Register hooked functions
  247. --*/
  248. HOOK_BEGIN
  249. CALL_NOTIFY_FUNCTION
  250. APIHOOK_ENTRY(USER32.DLL, SetWindowsHookExA)
  251. HOOK_END
  252. IMPLEMENT_SHIM_END