home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / debug.cpp < prev    next >
C/C++ Source or Header  |  1997-10-25  |  4KB  |  155 lines

  1. #include "CkyPch.h"
  2. #include "debug.h"
  3.  
  4.  
  5. #ifdef _DEBUG
  6.  
  7. void __cdecl
  8. Trace(
  9.     LPCTSTR ptszFormat,
  10.     ...)
  11. {
  12.     TCHAR tszBuff[2048];
  13.     va_list args;
  14.     
  15.     va_start(args, ptszFormat);
  16.     _vstprintf(tszBuff, ptszFormat, args);
  17.     va_end(args);
  18.  
  19.     OutputDebugString(tszBuff);
  20. }
  21.  
  22.  
  23. #define ACTIVE_SERVER_PAGES
  24.  
  25. # if defined(_MSC_VER)  &&  (_MSC_VER >= 1000)
  26.  
  27.  
  28. #  ifdef ACTIVE_SERVER_PAGES
  29.  
  30. // The default assertion mechanism set up by Visual C++ 4 will not
  31. // work with Active Server Pages because it's running inside a service
  32. // and there is no desktop to interact with.
  33.  
  34. // Note: for this to work properly, #define _WIN32_WINNT 0x400 before
  35. // including <winuser.h> or MB_SERVICE_NOTIFICATION won't be #define'd.
  36.  
  37. int
  38. AspAssertHandler(
  39.     int   nReportType,
  40.     char* pszErrorText,
  41.     int*  pnReturn)
  42. {
  43.     const char szInfo[] = " (Press ABORT to terminate IIS,"
  44.                           " RETRY to debug this failure,"
  45.                           " or IGNORE to continue.)";
  46.     char* pszMessageTitle = NULL;
  47.     
  48.     // These flags enable message boxes to show up on the user's console
  49.     switch (nReportType)
  50.     {
  51.     case _CRT_WARN:
  52.         pszMessageTitle = "Warning";
  53.         break;
  54.     case _CRT_ERROR:
  55.         pszMessageTitle = "Fatal Error";
  56.         break;
  57.     case _CRT_ASSERT:
  58.         pszMessageTitle = "Assertion Failed";
  59.         break;
  60.     }   
  61.     
  62.     char* pszMessageText =
  63.         static_cast<char*>(_alloca(strlen(pszErrorText) + strlen(szInfo) + 1));
  64.  
  65.     strcpy(pszMessageText, pszErrorText);
  66.     strcat(pszMessageText, szInfo);
  67.     
  68.     const int n = MessageBoxA(NULL, pszMessageText, pszMessageTitle,
  69.                               (MB_SERVICE_NOTIFICATION | MB_TOPMOST
  70.                                | MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION));
  71.  
  72.     if (n == IDABORT)
  73.     {
  74.         exit(1);
  75.     }
  76.     else if (n == IDRETRY)
  77.     {
  78.         *pnReturn = 1;   // tell _CrtDbgReport to start the debugger
  79.         return TRUE;     // tell _CrtDbgReport to run
  80.     }
  81.     
  82.     *pnReturn = 0;       // nothing for _CrtDbgReport to do
  83.  
  84.     return FALSE;
  85. }
  86.  
  87. #  endif // ACTIVE_SERVER_PAGES
  88. # endif // _MSC_VER >= 1000
  89.  
  90.  
  91.  
  92. void
  93. DebugInit()
  94. {
  95. # if defined(_MSC_VER)  &&  (_MSC_VER >= 1000)
  96. #  ifdef ACTIVE_SERVER_PAGES
  97.     // If we end up in _CrtDbgReport, don't put up a message box
  98.     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
  99.     _CrtSetReportMode(_CRT_WARN,   _CRTDBG_MODE_DEBUG);
  100.     _CrtSetReportMode(_CRT_ERROR,  _CRTDBG_MODE_DEBUG);
  101.  
  102.     // Use AspAssertHandler to put up a message box instead
  103.     _CrtSetReportHook(AspAssertHandler);
  104. #  endif // ACTIVE_SERVER_PAGES
  105.  
  106.     // Enable debug heap allocations & check for memory leaks at program exit
  107.     // The memory leak check will not be performed if inetinfo.exe is
  108.     // run directly under a debugger, only if it is run as a service.
  109.     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF
  110.                    | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
  111. # endif // _MSC_VER >= 1000
  112. }
  113.  
  114.  
  115.  
  116. void
  117. DebugTerm()
  118. {
  119. # if defined(_MSC_VER)  &&  (_MSC_VER >= 1000)
  120. #  ifdef ACTIVE_SERVER_PAGES
  121.     // Turn off AspAssertHandler, so that we don't get numerous message boxes
  122.     // if there are memory leaks on shutdown
  123.     _CrtSetReportHook(NULL);
  124. #  endif // ACTIVE_SERVER_PAGES
  125. # endif // _MSC_VER >= 1000
  126. }
  127.  
  128. #endif //_DEBUG
  129.  
  130.  
  131.  
  132. BOOL
  133. IsValidString(
  134.     LPCTSTR ptsz,
  135.     int nLength /* =-1 */)
  136. {
  137.     if (ptsz == NULL)
  138.         return FALSE;
  139.  
  140.     return !IsBadStringPtr(ptsz, nLength);
  141. }
  142.  
  143.  
  144.  
  145. BOOL
  146. IsValidAddress(
  147.     LPCVOID pv,
  148.     UINT nBytes,
  149.     BOOL fReadWrite /* =TRUE */)
  150. {
  151.     return (pv != NULL
  152.             &&  !IsBadReadPtr(pv, nBytes)
  153.             &&  (!fReadWrite  ||  !IsBadWritePtr((LPVOID) pv, nBytes)));
  154. }
  155.