home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / d / d009_2 / 1.ddi / SAMPLES / TRAPS / CWTRAP.C$ / CWTRAP.bin
Encoding:
Text File  |  1992-02-03  |  7.2 KB  |  207 lines

  1. //---------------------------------------------------------------------------
  2. // CWTRAP.C
  3. //
  4. // This module contains a trap to be used with Microsoft Test Driver.  It
  5. // installs a windows hook on WndProc calls, and invokes the Trap dispatcher
  6. // if the caption of the created window contains the text given by the user.
  7. //
  8. // Copyright (c) 1990-1992  Microsoft Corporation
  9. //
  10. //---------------------------------------------------------------------------
  11. #include <windows.h>
  12. #include <string.h>
  13.  
  14.  
  15. // WPMSG structure used by CALLWNDPROC hooks
  16. //---------------------------------------------------------------------------
  17. typedef struct
  18. {
  19.     WORD    hlParam;
  20.     WORD    llParam;
  21.     WORD    wParam;
  22.     WORD    wMsg;
  23.     WORD    hWnd;
  24. } WPMSG;
  25.  
  26. // Module globals.  Note that this is a SINGLE-INSTANCE library!
  27. //---------------------------------------------------------------------------
  28. HANDLE  hInst;                              // Module-instance handle (dll)
  29. FARPROC CWTrapProc = NULL;                  // Trap Proc
  30. FARPROC OldHook;                            // The old windows hook
  31. int     CWTrapID;                           // Trap ID
  32. int     TimerID = 0;                        // Timer ID
  33. char    Title[256] = "";                    // Target title text (caption)
  34. char    Actual[256];                        // Actual title
  35.  
  36.  
  37. //---------------------------------------------------------------------------
  38. // TriggerTrap
  39. //
  40. // This function is called by a Windows timer, and triggers a testdrvr trap
  41. // telling it that the window in question has been sent a WM_CREATE message.
  42. //
  43. // RETURNS:     Nothing
  44. //---------------------------------------------------------------------------
  45. void FAR PASCAL TriggerTrap (HWND hWnd, WORD wMsg, int nID, DWORD time)
  46. {
  47.     KillTimer (NULL, TimerID);
  48.     TimerID = 0;
  49.     if (CWTrapProc)
  50.         CWTrapProc (CWTrapID);
  51. }
  52.  
  53. //---------------------------------------------------------------------------
  54. // CWWndProcHook
  55. //
  56. // This is the WndProc Hook installed by the trap code.  If this routine is
  57. // called, it is assumed that CWTrapProc has been initialized, and calls it
  58. // if appropriate.
  59. //
  60. // RETURNS:     Nothing
  61. //---------------------------------------------------------------------------
  62. void FAR PASCAL CWWndProcHook (int nCode, WORD wParam, DWORD lParam)
  63. {
  64.     // Get out if nCode < 0 (because we're told to)
  65.     //-----------------------------------------------------------------------
  66.     if (nCode >= 0)
  67.         {
  68.         // Get out if not a WM_CREATE message
  69.         //-------------------------------------------------------------------
  70.         if (((WPMSG far *)lParam)->wMsg == WM_CREATE)
  71.             {
  72.             // Get out if the window being created does not have a WS_CAPTION
  73.             // style bit set
  74.             //---------------------------------------------------------------
  75.             if (GetWindowLong((HWND)((WPMSG far *)lParam)->hWnd, GWL_STYLE)
  76.                                  & WS_CAPTION)
  77.                 {
  78.                 // Get the window text of the window being created -- if the
  79.                 // given text (in Title[]) appears in it, then set a timer to
  80.                 // call testdrvr's Trap Dispatcher.
  81.                 //-----------------------------------------------------------
  82.                 HWND    hwnd;
  83.  
  84.                 hwnd = (HWND)((WPMSG far *)lParam)->hWnd;
  85.                 GetWindowText (hwnd, Actual, sizeof(Actual));
  86.                 if ((!Title[0]) || (_fstrstr (Actual, Title)))
  87.                     TimerID = SetTimer (NULL, 1, 5, TriggerTrap);
  88.                 }
  89.             }
  90.         }
  91.  
  92.     // Okay, we're done.  Let the normal routine do its thing...
  93.     //-----------------------------------------------------------------------
  94.     DefHookProc (nCode, wParam, lParam, &OldHook);
  95. }
  96.  
  97. //---------------------------------------------------------------------------
  98. // CWTrap
  99. //
  100. // This is a TESTDRVR trap routine.  It notifies testdrvr of any WM_CREATE
  101. // msgs sent to windows with captions matching that in Title[].
  102. //
  103. // RETURNS:     Nothing
  104. //---------------------------------------------------------------------------
  105. VOID FAR PASCAL CWTrap (int TrapID, int Action, FARPROC TrapProc)
  106. {
  107.     if (Action)
  108.         {
  109.         // Action=TRUE means activate the trap.  Keep track of the ID value
  110.         // of this trap (in CWTrapID) and the address of the trap dispatcher.
  111.         // The CALLWNDPROC hook calls this address with this ID to trigger
  112.         // the trap code set up in TESTDRVR.  Then, set the hook for watching
  113.         // the messages going through the callwndproc hook.
  114.         //-------------------------------------------------------------------
  115.         if (CWTrapProc)
  116.             return;
  117.         CWTrapProc = TrapProc;
  118.         CWTrapID = TrapID;
  119.         OldHook = SetWindowsHook (WH_CALLWNDPROC, CWWndProcHook);
  120.         }
  121.     else
  122.         {
  123.         // Action=FALSE means deactivate the trap.  Unhook the callwndproc
  124.         // hook and kill any pending timers.
  125.         //-------------------------------------------------------------------
  126.         if (CWTrapProc)
  127.             {
  128.             if (TimerID)
  129.                 KillTimer (NULL, TimerID);
  130.             UnhookWindowsHook (WH_CALLWNDPROC, CWWndProcHook);
  131.             CWTrapProc = NULL;
  132.             }
  133.         }
  134. }
  135.  
  136.  
  137. //---------------------------------------------------------------------------
  138. // SetCWTitle
  139. //
  140. // This function copies the string given into Title[], for detection of
  141. // particular WM_CREATE messages.
  142. //
  143. // RETURNS:     Nothing
  144. //---------------------------------------------------------------------------
  145. VOID FAR PASCAL SetCWTitle (LPSTR NewTitle)
  146. {
  147.     _fstrcpy (Title, NewTitle);
  148. }
  149.  
  150. //---------------------------------------------------------------------------
  151. // GetCWTitle
  152. //
  153. // This function copies the caption of the last window which caused a CW
  154. // trap to occur into the buffer given, up to the number of bytes given.
  155. //
  156. // RETURNS:     Nothing
  157. //---------------------------------------------------------------------------
  158. VOID FAR PASCAL GetCWTitle (LPSTR dest, int maxsize)
  159. {
  160.     _fstrncpy (dest, Actual, maxsize-1);
  161.     dest[maxsize-1] = 0;
  162. }
  163.  
  164.  
  165. //---------------------------------------------------------------------------
  166. // LibMain
  167. //
  168. // This is the entry point to the dll.
  169. //
  170. // RETURNS:     1
  171. //---------------------------------------------------------------------------
  172. int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg,
  173.                         WORD wHeapSize, LPSTR lpCmdLine)
  174. {
  175.     hInst = hInstance;
  176.     return(1);
  177. }
  178.  
  179.  
  180. //---------------------------------------------------------------------------
  181. //
  182. // WINDOWS EXIT PROCEDURE
  183. //   This routine is required for all DLL's.  It provides a means for cleaning
  184. //   up prior to closing the DLL.
  185. //
  186. // CALLED ROUTINES
  187. //   -none-
  188. //
  189. // PARAMETERS
  190. //   WORD  wParam        = TRUE is system shutdown
  191. //
  192. // GLOBAL VARIABLES
  193. //   -none-
  194. //
  195. // RETURNS
  196. //   -none-
  197. //---------------------------------------------------------------------------
  198. void FAR PASCAL WEP(WORD wParam)
  199. {
  200.     if (wParam && CWTrapProc)
  201.         {
  202.         if (TimerID)
  203.             KillTimer (NULL, TimerID);
  204.         UnhookWindowsHook (WH_CALLWNDPROC, CWWndProcHook);
  205.         }
  206. }
  207.