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

  1. //---------------------------------------------------------------------------
  2. // RIPTRAP.C
  3. //
  4. // This module contains a TESTDRVR trap.  It installs a notification
  5. // callback function using the TOOLHELP.DLL 'NotifyRegister' function.
  6. // and invokes the TESTDRVR Trap Dispatcher if a RIP occurs.
  7. //
  8. //---------------------------------------------------------------------------
  9.  
  10. #include <string.h>
  11. #include <windows.h>
  12. #include <toolhelp.h>
  13.  
  14. void FAR PASCAL log(LPSTR Message, int flag);
  15.  
  16.  
  17. HANDLE             hInst;                      // Module-instance handle (dll)
  18. int                RIPTrapID;                  // Trap ID
  19. void (far pascal *TrapDispatcher)(int) = NULL; // The WTD Trap Dispathcher
  20. BOOL               fRegResult ;                // Results of Register/UnRegister.
  21. HANDLE             hTask;
  22. LPFNNOTIFYCALLBACK lpfnCallback;               // Pointer to the callback function.
  23. NFYRIP             RIPInfo;                    // Record of RIP information.
  24. int                hFile, result;
  25. int                RIPFlag = 0;
  26. int                NumCalls = 0;
  27. int                StrWaiting = FALSE;
  28. char               OutStrBuffer[215];
  29.  
  30.  
  31.  
  32. //---------------------------------------------------------------------------
  33. // RIPCallBackProc
  34. //
  35. // This is the callback function installed by the trap code.  If this routine is
  36. // called, it is assumed that TrapDispatcher has been initialized, and calls it
  37. // if appropriate.
  38. //
  39. // RETURNS:     Per Windows Convention (nothing)
  40. //---------------------------------------------------------------------------
  41. BOOL FAR PASCAL RIPCallBackProc (WORD wParam, DWORD lParam)
  42. {
  43.  
  44.     RIPFlag = wParam;
  45.     NumCalls++;
  46.  
  47.     if (wParam == NFY_RIP){
  48.        RIPInfo.dwSize =    ((NFYRIP far *)lParam)->dwSize;
  49.        RIPInfo.wIP =       ((NFYRIP far *)lParam)->wIP;
  50.        RIPInfo.wCS =       ((NFYRIP far *)lParam)->wCS;
  51.        RIPInfo.wSS =       ((NFYRIP far *)lParam)->wSS;
  52.        RIPInfo.wBP =       ((NFYRIP far *)lParam)->wBP;
  53.        RIPInfo.wExitCode = ((NFYRIP far *)lParam)->wExitCode;
  54.        TrapDispatcher (RIPTrapID);  // Cause the trap to occur.
  55.        return(TRUE);                // return(TRUE) means WE handle the problem.
  56.                                     // The assumption is the TRAP handles.
  57.                                     // The problem.
  58.     }
  59.  
  60.     if (wParam == NFY_OUTSTR) {
  61.        _fstrncpy(OutStrBuffer, (char far *)lParam, sizeof (OutStrBuffer) - 1);
  62.        StrWaiting = TRUE;
  63.     }
  64.  
  65.     return(0);
  66.  
  67. }
  68.  
  69.  
  70. //---------------------------------------------------------------------------
  71. // RIPTrap
  72. //
  73. // This is a TESTDRVR trap installation routine.
  74. // It is called by TESTDRVR during the bind stage of compiling a script.
  75. // It installs the RIPCallBackProc which informs TESTDRVR of any RIPs
  76. // that occur in any application while the trap is installed.
  77. //
  78. // RETURNS:     Nothing
  79. //---------------------------------------------------------------------------
  80. VOID FAR PASCAL RIPTrap (int TrapID, int Action, FARPROC TrapProc)
  81. {
  82.  
  83.     if (Action)
  84.         if (TrapDispatcher)
  85.         {
  86.           return; // We've already been called and have
  87.                   // the address of the dispatcher.
  88.         }
  89.         else      // Install the given dispatcher and Trap ID.
  90.         {
  91.            TrapDispatcher = TrapProc; // Save the address of the TESTDRVR
  92.                                       // Trap Dispatcher.
  93.            RIPTrapID = TrapID;        // Save the ID of our TESTDRVR TRAP
  94.                                       // routine.
  95.  
  96.            lpfnCallback = MakeProcInstance(RIPCallBackProc, hInst);
  97.            fRegResult = NotifyRegister (0, lpfnCallback, NF_NORMAL | NF_RIP); // 0 means the current task
  98.         }
  99.     else
  100.         {
  101.         // Action=FALSE means deactivate the trap.
  102.         //-------------------------------------------------------------------
  103.             fRegResult = NotifyUnRegister (hTask); // NULL implies handle to current task.
  104.             TrapDispatcher = NULL;
  105.             RIPTrapID = NULL;
  106.             hTask = NULL;
  107.         }
  108. } // END RIPTrap()
  109.  
  110.  
  111. //---------------------------------------------------------------------------
  112. // LibMain
  113. //
  114. // This is the entry point to the dll.
  115. //
  116. // RETURNS:     1
  117. //---------------------------------------------------------------------------
  118. int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg,
  119.                         WORD wHeapSize, LPSTR lpCmdLine)
  120. {
  121.     hInst = hInstance;
  122.     TrapDispatcher = NULL;
  123.     return(1);
  124. }
  125.  
  126.  
  127. //---------------------------------------------------------------------------
  128. //
  129. // WINDOWS EXIT PROCEDURE
  130. //   This routine is required for all DLL's.  It provides a means for cleaning
  131. //   up prior to closing the DLL.
  132. //
  133. // CALLED ROUTINES
  134. //   -none-
  135. //
  136. // PARAMETERS
  137. //   WORD  wParam        = TRUE is system shutdown
  138. //
  139. // GLOBAL VARIABLES
  140. //   -none-
  141. //
  142. // RETURNS
  143. //   -none-
  144. //---------------------------------------------------------------------------
  145. void FAR PASCAL WEP(WORD wParam)
  146. {
  147.     // If TESTDRVR hangs or is not able to terminate normally,
  148.     // we have to unregister our RIP callback function here.
  149.     if (wParam && TrapDispatcher)
  150.     {
  151.         fRegResult = NotifyUnRegister (hTask); // NULL implies handle to current task.
  152.         TrapDispatcher = NULL;
  153.         hTask = NULL;
  154.     }
  155.     return;
  156. }
  157.  
  158.  
  159.  
  160. int FAR PASCAL GetDebugStr (LPSTR DebugStr)
  161. {
  162.     if (StrWaiting == TRUE)
  163.     {
  164.       _fstrncpy(DebugStr, OutStrBuffer, sizeof (OutStrBuffer) - 1);
  165.        StrWaiting = FALSE;
  166.        return(-1);
  167.     }
  168.  
  169.     else return(0);
  170.  
  171. }
  172.  
  173.  
  174. // GetRIPInfo can be used by a TRAP routine to get the
  175. // information about the RIP retrieved in RIPCallBackProc.
  176. //
  177. int FAR PASCAL GetRIPInfo (NFYRIP far * RInfo)
  178. {
  179.     RInfo->dwSize    = RIPInfo.dwSize;
  180.     RInfo->wIP       = RIPInfo.wIP;
  181.     RInfo->wCS       = RIPInfo.wCS;
  182.     RInfo->wSS       = RIPInfo.wSS;
  183.     RInfo->wBP       = RIPInfo.wBP;
  184.     RInfo->wExitCode = RIPInfo.wExitCode;
  185.  
  186.     return(0);
  187.  
  188. }
  189.  
  190.