Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. // DLL.cpp : Defines the exported functions for the DLL.
  2. //
  3.  
  4. #include "pch.h"
  5. #include "framework.h"
  6. #include "DLL.h"
  7. #define DLLEXPORT extern "C" __declspec (dllexport)
  8. // GlobalHook_Test.cpp : define the export functions of DLL application .
  9.  
  10.  
  11. extern HMODULE g_hDllModule; //save the Dll handle for calling by SetWindowsHookEx()
  12.  
  13. // to share the same memory with the host process
  14. #pragma data_seg("mydata")
  15. //define a static variable g_hHook of the type HHook, initialize it by NULL
  16. HHOOK g_hHook = NULL;
  17. #pragma data_seg()
  18. #pragma comment(linker, "/SECTION:mydata,RWS")
  19.  
  20. //The Hook Callback Function GetMsgProc
  21. LRESULT CALLBACK GetMsgProc(
  22. int code,
  23. WPARAM wParam,
  24. LPARAM lParam)
  25. {
  26.  
  27. //add something here for debug
  28. // 1. show some messages
  29. int msgboxID = MessageBox(
  30. NULL,
  31. (LPCWSTR)L"Hello World",
  32. (LPCWSTR)L"123",
  33. NULL
  34. );
  35. //Pass the the next hook function if there is a hook function
  36. // to do ...
  37. return ::CallNextHookEx(g_hHook, code, wParam, lParam);
  38. }
  39.  
  40. // Set the global hook
  41. DLLEXPORT BOOL WINAPI SetGlobalHook()
  42. {
  43. //2. Use SetWindowsHookEx(p1,p2,p3,p4) to bind the
  44. // keyboard message p1=WH_KEYBOARD with p2=(HOOKPROC)GetMsgProc, and p3 is the Dll handle, p4 is an int value
  45. g_hHook = SetWindowsHookEx(WH_KEYBOARD, &GetMsgProc, g_hDllModule, NULL);
  46. //to do ...
  47. //3. you can also add mouse messages here
  48.  
  49. //to do ...
  50. if (NULL == g_hHook) // failure
  51. {
  52. return FALSE;
  53. }
  54. return TRUE; //success
  55. }
  56.  
  57. // Unload the global hook
  58. DLLEXPORT BOOL WINAPI UnsetGlobalHook()
  59. {
  60. if (g_hHook)
  61. {
  62. //4. you cna also Unhook the Windows Hook "g_hHaook" here with UnHookWindowsHookEx(p1) by
  63. // letting equal p1 to g_hHook
  64. //to do ...
  65. //4. you cna also Unhook the Windows Hook "g_hHook" here with UnHookWindowsHookEx(p1) by
  66. UnhookWindowsHookEx(g_hHook);
  67. MessageBox(
  68. NULL,
  69. (LPCWSTR)L"Unhooked successfuly",
  70. (LPCWSTR)L"Unsetgloabl Hook",
  71. NULL
  72. );
  73. }
  74. return TRUE;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement