Track.cpp
上传用户:cxh888fhc
上传日期:2017-07-08
资源大小:240k
文件大小:3k
源码类别:

钩子与API截获

开发平台:

Visual C++

  1. #include <windows.h>
  2. #define DLL_EXPORT extern "C" __declspec(dllexport)
  3. HINSTANCE g_hinstDll=NULL;
  4. HHOOK g_hHook=NULL;
  5. DWORD MOVEMSG=RegisterWindowMessage("MOVEMSG");
  6. #pragma data_seg(".sdata")
  7. HWND g_hWnd=NULL;
  8. #pragma data_seg()
  9. #pragma comment(linker,"-section:.sdata,rws")
  10. DLL_EXPORT HHOOK SetFocusHook(HWND hWnd,DWORD dwThread);
  11. LRESULT CALLBACK CallWndRetProc(
  12.   int nCode,     // hook code
  13.   WPARAM wParam, // current-process flag
  14.   LPARAM lParam  // address of structure with message data
  15. );
  16. BOOL WINAPI DllMain(HINSTANCE hinstDll,DWORD fdwReason,PVOID fImpLoad)
  17. {
  18. switch(fdwReason)
  19. {
  20. case DLL_PROCESS_ATTACH:
  21. g_hinstDll=hinstDll;
  22. break;
  23. case DLL_THREAD_ATTACH:
  24. break;
  25. case DLL_THREAD_DETACH:
  26. break;
  27. case DLL_PROCESS_DETACH:
  28. break;
  29. }
  30. return TRUE;
  31. }
  32. HHOOK SetFocusHook(HWND hWnd,DWORD dwThread)
  33. {
  34. if(g_hHook!=NULL)
  35. {
  36. UnhookWindowsHookEx(g_hHook);
  37. }
  38. g_hHook=SetWindowsHookEx(WH_CALLWNDPROCRET,CallWndRetProc,g_hinstDll,dwThread);
  39. g_hWnd=hWnd;
  40. return g_hHook;
  41. }
  42. LRESULT CALLBACK CallWndRetProc(
  43.   int nCode,     // hook code
  44.   WPARAM wParam, // current-process flag
  45.   LPARAM lParam  // address of structure with message data
  46. )
  47. {
  48. if(nCode<0)
  49. {
  50. return CallNextHookEx(NULL,nCode,wParam,lParam);
  51. }
  52. CWPRETSTRUCT *p=(CWPRETSTRUCT *)lParam;
  53. // HDC hDC=GetDC(NULL);
  54. // static int i=0;
  55. if(p->message==WM_ACTIVATE)
  56. {
  57. WORD fActive=LOWORD(p->wParam);
  58. if(fActive==WA_ACTIVE||fActive==WA_CLICKACTIVE)
  59. {
  60. SetWindowPos(g_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE);
  61. // TextOut(hDC,0,i,"WA_ACTIVE",strlen("WA_ACTIVE"));
  62. // i+=15;
  63. }
  64. else if(fActive==WA_INACTIVE)
  65. {
  66. if(GetForegroundWindow()!=g_hWnd)
  67. {
  68. SetWindowPos(g_hWnd,HWND_BOTTOM,0,0,0,0,SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE);
  69. }
  70. // TextOut(hDC,0,i,"WA_INACTIVE",strlen("WA_INACTIVE"));
  71. // i+=15;
  72. }
  73. }
  74. if(p->message==WM_SIZE)
  75. {
  76. if(p->wParam==SIZE_MINIMIZED)
  77. {
  78. ShowWindow(g_hWnd,SW_HIDE);
  79. // TextOut(hDC,0,i,"SIZE_MINIMIZED",strlen("SIZE_MINIMIZED"));
  80. // i+=15;
  81. }
  82. else if(p->wParam==SIZE_RESTORED)
  83. {
  84. ShowWindow(g_hWnd,SW_SHOWNA);
  85. // TextOut(hDC,0,i,"SIZE_RESTORED",strlen("SIZE_RESTORED"));
  86. // i+=15;
  87. }
  88. }
  89. if(p->message==WM_DESTROY)
  90. {
  91. // TextOut(hDC,0,i,"WM_CLOSE",strlen("WM_CLOSE"));
  92. // i+=15;
  93. SendMessage(g_hWnd,WM_CLOSE,0,0);
  94. }
  95. if(p->message==WM_MOVE)
  96. {
  97. //如果出现移动的情况,则及时发送消息给显示标志对话框以便移动.
  98. SendMessage(g_hWnd,MOVEMSG,wParam,p->lParam);
  99. }
  100. // ReleaseDC(NULL,hDC);
  101. return CallNextHookEx(NULL,nCode,wParam,lParam);
  102. }