home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / spy / dll / hook.c next >
C/C++ Source or Header  |  1997-10-05  |  7KB  |  277 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /*****************************************************************************\
  13. * hook.c - Windows message spy application dll
  14. *
  15. * Functions:
  16. *
  17. * DllMain()
  18. * FindSpyWindow()
  19. * HookProc()
  20. * SpyGetMsgProc()
  21. * SpyCallWndProc()
  22. * DbgPrintf()
  23. *
  24. * Comments:
  25. *
  26. \*****************************************************************************/
  27.  
  28. #include <windows.h>
  29. #include "..\hook.h"
  30.  
  31.  
  32. PRIVATE HWND ghwndSpyHook = NULL;   // the handle back to the spy executable
  33. PRIVATE SPYMSGDATA gsmd;
  34. PRIVATE COPYDATASTRUCT gcds = { 0, sizeof(SPYMSGDATA), &gsmd };
  35.  
  36.  
  37. PRIVATE VOID FindSpyWindow(VOID);
  38.  
  39. #ifdef DBG
  40. VOID DbgPrintf(LPTSTR fmt, ...);
  41. #endif
  42.  
  43.  
  44.  
  45. /*****************************************************************************\
  46. * DllMain (hModule,cbHeap,lpchCmdLine)
  47. *
  48. * Called when the libary is loaded
  49. *
  50. * Arguments:
  51. *    PVOID hModule - Module handle for the libary.
  52. *    ULONG ulReason - DLL purpose
  53. *    PCONTEXT pctx - not used
  54. *
  55. * Returns:
  56. *    TRUE - Everything is ok
  57. *    FALSE- Error.
  58. \*****************************************************************************/
  59.  
  60. BOOL
  61. APIENTRY DllMain(
  62.     PVOID hModule,
  63.     ULONG ulReason,
  64.     PCONTEXT pctx
  65.     )
  66. {
  67.     UNREFERENCED_PARAMETER(hModule);
  68.     UNREFERENCED_PARAMETER(pctx);
  69.  
  70.     //
  71.     // This function is called for every instance of the DLL. We must find
  72.     // and store the handle to the spy window every time an instance of the
  73.     // DLL is instantiated.
  74.     //
  75.     if ( ulReason == DLL_PROCESS_ATTACH ) {
  76.         FindSpyWindow();
  77.     }
  78.  
  79.     return TRUE;
  80. }
  81.  
  82.  
  83.  
  84. /*****************************************************************************\
  85. * FindSpyWindow
  86. *
  87. * Finds the spy window and store a local copy in this instances data.
  88. * This must be called everytime that a new instance of the DLL is
  89. * created.
  90. *
  91. * Arguments:
  92. *    none
  93. *
  94. * Returns:
  95. *    VOID
  96. \*****************************************************************************/
  97.  
  98. PRIVATE VOID
  99. FindSpyWindow(
  100.     VOID
  101.     )
  102. {
  103.     ghwndSpyHook = FindWindow(HOOKWINDOWCLASS, HOOKWINDOWNAME);
  104. }
  105.  
  106.  
  107. /*****************************************************************************\
  108. * HookProc( hWnd, uiMessage, wParam, lParam )
  109. *
  110. * The hook proc for the windows hook being spied on
  111. *
  112. * Arguments:
  113. *    HWND hWnd - window handle for the parent window
  114. *    UINT uiMessage - message number
  115. *    WPARAM wParam - message-dependent
  116. *    LPARAM lParam - message-dependent
  117. *
  118. * Returns:
  119. *    0 if processed, nonzero if ignored
  120. \*****************************************************************************/
  121.  
  122. BOOL WINAPI
  123. HookProc(
  124.     HWND hwnd,
  125.     UINT uiMessage,
  126.     WPARAM wParam,
  127.     LPARAM lParam
  128.     )
  129. {
  130.     HWND hwndSpyingOn;
  131.     HWND hwndSpyApp;
  132.  
  133.     if (ghwndSpyHook == NULL || !IsWindow(ghwndSpyHook))
  134.     {
  135.         //
  136.         // Spy has terminated. Find the new window.
  137.         //
  138.         FindSpyWindow();
  139.     }
  140.  
  141.     if (ghwndSpyHook != NULL && hwnd != ghwndSpyHook)
  142.     {
  143.         hwndSpyingOn = (HWND)GetWindowLong(ghwndSpyHook, 0);
  144.         hwndSpyApp = (HWND)GetWindowLong(ghwndSpyHook, sizeof(HWND));
  145. //DbgPrintf("H ghwndSpyHook:%8.8x", ghwndSpyHook);
  146. //DbgPrintf("H hwndSpyingOn:%8.8x", hwndSpyingOn);
  147. //DbgPrintf("H hwndSpyApp:%8.8x", hwndSpyApp);
  148.  
  149.         //
  150.         // Send the message on asynchronously for Spy to deal with if
  151.         // it is the appropriate hwndSpyingOn window to spy on.
  152.         //
  153.  
  154.         if (hwndSpyingOn == hwnd
  155.             || (hwndSpyingOn == HWND_ALL && hwnd != hwndSpyApp
  156.             && !IsChild(hwndSpyApp, hwnd)))
  157.         {
  158.             gsmd.wParam = wParam;
  159.             gsmd.lParam = lParam;
  160.  
  161.             gcds.dwData = uiMessage;
  162.  
  163. //DbgPrintf("H Sending Message hwnd:%8.8x msg:%d", hwnd, uiMessage);
  164.             SendMessage(ghwndSpyHook, WM_COPYDATA, (WPARAM)hwnd, (LPARAM)&gcds);
  165. //DbgPrintf("H Sent Message hwnd:%8.8x msg:%d", hwnd, uiMessage);
  166.  
  167. //DbgPrintf("");
  168.             return TRUE;
  169.         }
  170. //DbgPrintf("");
  171.     }
  172.  
  173.     return FALSE;
  174. }
  175.  
  176.  
  177.  
  178. /*****************************************************************************\
  179. * SpyGetMsgProc
  180. *
  181. * The Get Message hook function.
  182. *
  183. \*****************************************************************************/
  184.  
  185. LRESULT CALLBACK
  186. SpyGetMsgProc(
  187.     INT hc,
  188.     WPARAM wParam,
  189.     LPARAM lParam
  190.     )
  191. {
  192.     PMSG pmsg;
  193.  
  194.     pmsg = (PMSG)lParam;
  195.  
  196.     if (hc >= 0 && pmsg && pmsg->hwnd)
  197.     {
  198.         return HookProc(pmsg->hwnd, pmsg->message, pmsg->wParam, pmsg->lParam);
  199.     }
  200.  
  201.     //
  202.     // Note that CallNextHookEx ignores the first parameter (hhook) so
  203.     // it is acceptable (barely) to pass in a NULL.
  204.     //
  205.     return CallNextHookEx(NULL, hc, wParam, lParam);
  206. }
  207.  
  208.  
  209.  
  210. /*****************************************************************************\
  211. * SpyCallWndProc
  212. *
  213. * The Call Window Proc (Send Message) hook function.
  214. *
  215. \*****************************************************************************/
  216.  
  217. LRESULT CALLBACK
  218. SpyCallWndProc(
  219.     INT hc,
  220.     WPARAM wParam,
  221.     LPARAM lParam
  222.     )
  223. {
  224.     PCWPSTRUCT pcwps;
  225.  
  226.     pcwps = (PCWPSTRUCT)lParam;
  227.  
  228.     if (hc >= 0 && pcwps && pcwps->hwnd)
  229.     {
  230.         return HookProc(pcwps->hwnd, pcwps->message, pcwps->wParam, pcwps->lParam);
  231.     }
  232.  
  233.     //
  234.     // Note that CallNextHookEx ignores the first parameter (hhook) so
  235.     // it is acceptable (barely) to pass in a NULL.
  236.     //
  237.     return CallNextHookEx(NULL, hc, wParam, lParam);
  238. }
  239.  
  240.  
  241.  
  242. #ifdef DBG
  243. /****************************************************************************
  244. * DBGprintf
  245. *
  246. * This debugging function prints out a string to the debug output.
  247. * An optional set of substitutional parameters can be specified,
  248. * and the final output will be the processed result of these combined
  249. * with the format string, just like printf.  A newline is always
  250. * output after every call to this function.
  251. *
  252. * Arguments:
  253. *   LPTSTR fmt - Format string (printf style).
  254. *   ...        - Variable number of arguments.
  255. * Returns:
  256. *    VOID
  257. \****************************************************************************/
  258.  
  259. VOID DbgPrintf(
  260.     LPTSTR fmt,
  261.     ...
  262.     )
  263. {
  264.     va_list marker;
  265.     TCHAR szBuf[256];
  266.  
  267.     va_start(marker, fmt);
  268.     wvsprintf(szBuf, fmt, marker);
  269.     va_end(marker);
  270.  
  271.     OutputDebugString(szBuf);
  272.     OutputDebugString(TEXT("\r\n"));
  273. }
  274. #endif
  275.  
  276.  
  277.