HookTool.cpp
上传用户:jstlsd
上传日期:2007-01-13
资源大小:186k
文件大小:3k
源码类别:

钩子与API截获

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. //
  3. // HookTool.cpp
  4. //
  5. // SUBSYSTEM:   Hook system
  6. //
  7. // MODULE:      Hook tool    
  8. //
  9. // DESCRIPTION: Defines the entry point for the DLL application.
  10. // 
  11. //             
  12. // AUTHOR: Ivo Ivanov (ivopi@hotmail.com)
  13. // DATE: 2001 December v1.00
  14. //
  15. //---------------------------------------------------------------------------
  16. #include "..CommonCommon.h"
  17. #include "..CommonSysUtils.h"
  18. #include "ModuleScope.h"
  19. //---------------------------------------------------------------------------
  20. //
  21. // Shared by all processes variables
  22. //
  23. //---------------------------------------------------------------------------
  24. #pragma data_seg(".HKT")
  25. // The hook handle
  26. HHOOK sg_hGetMsgHook       = NULL;
  27. // Indicates whether the hook has been installed
  28. BOOL  sg_bHookInstalled    = FALSE;
  29. // We get this from the application who calls SetWindowsHookEx()'s wrapper
  30. HWND  sg_hwndServer        = NULL; 
  31. #pragma data_seg()
  32. //---------------------------------------------------------------------------
  33. //
  34. // Global (per process) variables
  35. //
  36. //---------------------------------------------------------------------------
  37. static CModuleScope* g_pModuleScope = NULL;
  38. //---------------------------------------------------------------------------
  39. //
  40. // Forward declarations
  41. //
  42. //---------------------------------------------------------------------------
  43. BOOL WINAPI InstallHook(
  44. BOOL bActivate, 
  45. HWND hWndServer
  46. );
  47. LRESULT CALLBACK GetMsgProc(
  48. int code,       // hook code
  49. WPARAM wParam,  // removal option
  50. LPARAM lParam   // message
  51. );
  52. //---------------------------------------------------------------------------
  53. // DllMain
  54. //
  55. // Entry point
  56. //---------------------------------------------------------------------------
  57. BOOL APIENTRY DllMain( 
  58. HANDLE hModule, 
  59. DWORD  ul_reason_for_call, 
  60. LPVOID lpReserved
  61. )
  62. {
  63. BOOL bResult = TRUE;
  64. switch (ul_reason_for_call)
  65. {
  66. case DLL_PROCESS_ATTACH:
  67. {
  68. // We disable thread notifications
  69. // Prevent the system from calling DllMain
  70. // when threads are created or destroyed.
  71. ::DisableThreadLibraryCalls( (HINSTANCE)hModule );
  72. g_pModuleScope = CModuleScope::GetInstance(
  73. &sg_hwndServer, 
  74. &sg_bHookInstalled,
  75. &sg_hGetMsgHook
  76. );
  77. g_pModuleScope->ManageModuleEnlistment();
  78. break;
  79. }
  80. case DLL_PROCESS_DETACH:
  81. {
  82. //
  83. // The DLL is being unmapped from the process's address space.
  84. //
  85. g_pModuleScope->ManageModuleDetachment();
  86. break;
  87. }
  88. } // switch
  89. return TRUE;
  90. }
  91. //---------------------------------------------------------------------------
  92. // InstallHook
  93. //
  94. //---------------------------------------------------------------------------
  95. BOOL WINAPI InstallHook(
  96. BOOL bActivate, 
  97. HWND hWndServer
  98. )
  99. {
  100. return g_pModuleScope->InstallHookMethod(bActivate, hWndServer);
  101. }
  102. //---------------------------------------------------------------------------
  103. // GetMsgProc
  104. //
  105. // Filter function for the WH_GETMESSAGE - it's just a dummy function
  106. //---------------------------------------------------------------------------
  107. LRESULT CALLBACK GetMsgProc(
  108. int code,       // hook code
  109. WPARAM wParam,  // removal option
  110. LPARAM lParam   // message
  111. )
  112. {
  113. //
  114. // We must pass the all messages on to CallNextHookEx.
  115. //
  116. return ::CallNextHookEx(sg_hGetMsgHook, code, wParam, lParam);
  117. }
  118. //----------------------------End of the file -------------------------------