home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / mapi / common / mapidbg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-11  |  64.4 KB  |  2,488 lines

  1. /*
  2.  *  MAPIDBG.C
  3.  *
  4.  *  MAPI Debugging Utilities
  5.  *
  6.  *  Copyright (C) 1986-1996 Microsoft Corporation. All rights reserved.
  7.  */
  8.  
  9. #ifdef DEBUG
  10.  
  11. #pragma warning(disable:4100)   /* unreferenced formal parameter */
  12. #pragma warning(disable:4127)   /* conditional expression is constant */
  13. #pragma warning(disable:4201)   /* nameless struct/union */
  14. #pragma warning(disable:4206)   /* translation unit is empty */
  15. #pragma warning(disable:4209)   /* benign typedef redefinition */
  16. #pragma warning(disable:4214)   /* bit field types other than int */
  17. #pragma warning(disable:4001)   /* single line comments */
  18. #pragma warning(disable:4050)   /* different code attributes */
  19.  
  20. #ifdef _MAC
  21. #define INC_OLE2
  22. #include <windows.h>
  23. #include <macname1.h>
  24. #include <macos\menus.h>
  25. #include <stdio.h>
  26. #include <mapiprof.h>
  27.  
  28. #define GetPrivateProfileIntA       MAPIGetPrivateProfileInt
  29.  
  30. #elif defined(WIN16) || defined(_WIN32)
  31. #pragma warning(disable:4115)   /* named type definition in parentheses */
  32. #include <windows.h>
  33. #include <mapiwin.h>
  34.  
  35. #ifdef _WIN32
  36. #pragma warning(disable:4001)   /* single line comments */
  37. #pragma warning(disable:4115)   /* named type definition in parentheses */
  38. #pragma warning (disable:4514)  /* unreferenced inline function */
  39. #include <objerror.h>
  40. #endif
  41.  
  42. #else
  43.  
  44. #include <stdio.h>
  45. void __far __pascal OutputDebugString(char __far *);
  46. #define wvsprintf           vsprintf
  47. #define wsprintf            sprintf
  48.  
  49. #endif      /* _MAC */
  50.  
  51. #ifdef DOS
  52. #define lstrcpyA            strcpy
  53. #define lstrlenA            strlen
  54. #define lstrcatA            strcat
  55. #define wvsprintfA          wvsprintf
  56. #define wsprintfA           wsprintf
  57. #define OutputDebugStringA  OutputDebugString
  58. #endif
  59.  
  60. #include <mapidbg.h>
  61. #include <mapidefs.h>
  62. #include <mapitags.h>
  63. #include <mapicode.h>
  64. #include <stdarg.h>
  65. #include <string.h>
  66. #include <time.h>
  67. #ifdef _MAC
  68. #include <macname2.h>
  69. #endif
  70.  
  71. #if defined(DBCS) && defined(DOS)
  72. #include <gapidos.h>
  73. #endif
  74.  
  75. #if defined(DEBUG) && defined(_WINNT)
  76. #include <lmcons.h>
  77. #include <lmalert.h>
  78. #endif
  79.  
  80. /*  Patch/Hack for 16bit, optimized builds.
  81.  *
  82.  *  memcpy with a size of 0 bytes causes a
  83.  *  crash.
  84.  */
  85.  
  86. #ifndef __MEMCPY_H_
  87. #define __MEMCPY_H_
  88.  
  89. #if defined(WIN16) && !defined(DEBUG)
  90. #define MemCopy(_dst,_src,_cb)      do                                  \
  91.                                     {                                   \
  92.                                         size_t __cb = (size_t)(_cb);    \
  93.                                         if (__cb)                       \
  94.                                             memcpy(_dst,_src,__cb);     \
  95.                                     } while (FALSE)
  96. #else
  97. #define MemCopy(_dst,_src,_cb)  memcpy(_dst,_src,(size_t)(_cb))
  98. #endif
  99.  
  100. #endif
  101.  
  102. #if (defined(WIN16) || defined(DOS)) && !defined(NO_BASED_DEBUG)
  103. #define BASED_DEBUG __based(__segname("DEBUG_DATA"))
  104. #else
  105. #define BASED_DEBUG
  106. #endif
  107.  
  108. #if defined(WIN16)
  109. #define BASED_CODE          __based(__segname("_CODE"))
  110. #else
  111. #define BASED_CODE
  112. #endif
  113.  
  114.  
  115.  
  116. #if defined(WIN16) || defined(_WIN32)
  117. static BOOL fTraceEnabled               = -1;
  118. static BOOL fUseEventLog                = -1;
  119. static BOOL fAssertLeaks                = -1;
  120. #if defined(_WIN32) && !defined(_MAC)
  121. BOOL fInhibitTrapThread                 = 2;
  122. #endif
  123.  
  124. static char szKeyTraceEnabled[]         = "DebugTrace";
  125. static char szKeyInhibitTrapThread[]    = "TrapOnSameThread";
  126. static char szKeyEventLog[]             = "EventLog";
  127. static char szKeyUseVirtual[]           = "VirtualMemory";
  128. static char szKeyAssertLeaks[]          = "AssertLeaks";
  129. static char szKeyCheckOften[]           = "CheckHeapOften";
  130. static char szKeyFillRandom[]           = "MemoryFillRandom";
  131. static char szSectionDebug[]            = "General";
  132. static char szDebugIni[]                = "MAPIDBG.INI";
  133. #endif
  134.  
  135. #ifndef VTABLE_FILL
  136. #ifdef _MAC
  137. #define VTABLE_FILL     NULL,
  138. #else
  139. #define VTABLE_FILL
  140. #endif
  141. #endif
  142.  
  143. #if defined(DEBUG) && defined(_WINNT)
  144. typedef BOOL  (WINAPI   *ReportEventFN)(HANDLE, WORD, WORD, DWORD, PSID, WORD, DWORD, LPCTSTR *, LPVOID);
  145. typedef HANDLE (WINAPI  *RegisterEventSourceAFN)(LPCTSTR, LPCTSTR);
  146.  
  147. ReportEventFN pfnReportEvent = NULL;
  148. RegisterEventSourceAFN pfnRegisterEventSourceA = NULL;
  149. #endif
  150.  
  151.  
  152. #ifdef  WIN16
  153. #pragma code_seg("Debug")
  154. #endif  
  155.  
  156. #if defined( _WINNT)
  157.  
  158. /*++
  159.  
  160. Routine Description:
  161.  
  162.     This routine returns if the service specified is running interactively
  163.     (not invoked \by the service controller).
  164.  
  165. Arguments:
  166.  
  167.     None
  168.  
  169. Return Value:
  170.  
  171.     BOOL - TRUE if the service is an EXE.
  172.  
  173.  
  174. Note:
  175.  
  176. --*/
  177.  
  178. BOOL WINAPI IsDBGServiceAnExe( VOID )
  179. {
  180.     HANDLE hProcessToken = NULL;
  181.     DWORD groupLength = 50;
  182.  
  183.     PTOKEN_GROUPS groupInfo = (PTOKEN_GROUPS)LocalAlloc(0, groupLength);
  184.  
  185.     SID_IDENTIFIER_AUTHORITY siaNt = SECURITY_NT_AUTHORITY;
  186.     PSID InteractiveSid = NULL;
  187.     PSID ServiceSid = NULL;
  188.     DWORD i;
  189.  
  190.     // Start with assumption that process is an EXE, not a Service.
  191.     BOOL fExe = TRUE;
  192.  
  193.  
  194.     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hProcessToken))
  195.         goto ret;
  196.  
  197.     if (groupInfo == NULL)
  198.         goto ret;
  199.  
  200.     if (!GetTokenInformation(hProcessToken, TokenGroups, groupInfo,
  201.         groupLength, &groupLength))
  202.     {
  203.         if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  204.             goto ret;
  205.  
  206.         LocalFree(groupInfo);
  207.         groupInfo = NULL;
  208.     
  209.         groupInfo = (PTOKEN_GROUPS)LocalAlloc(0, groupLength);
  210.     
  211.         if (groupInfo == NULL)
  212.             goto ret;
  213.     
  214.         if (!GetTokenInformation(hProcessToken, TokenGroups, groupInfo,
  215.             groupLength, &groupLength))
  216.         {
  217.             goto ret;
  218.         }
  219.     }
  220.  
  221.     //
  222.     //  We now know the groups associated with this token.  We want to look to see if
  223.     //  the interactive group is active in the token, and if so, we know that
  224.     //  this is an interactive process.
  225.     //
  226.     //  We also look for the "service" SID, and if it's present, we know we're a service.
  227.     //
  228.     //  The service SID will be present iff the service is running in a
  229.     //  user account (and was invoked by the service controller).
  230.     //
  231.  
  232.  
  233.     if (!AllocateAndInitializeSid(&siaNt, 1, SECURITY_INTERACTIVE_RID, 0, 0,
  234.         0, 0, 0, 0, 0, &InteractiveSid))
  235.     {
  236.         goto ret;
  237.     }
  238.  
  239.     if (!AllocateAndInitializeSid(&siaNt, 1, SECURITY_SERVICE_RID, 0, 0, 0,
  240.         0, 0, 0, 0, &ServiceSid))
  241.     {
  242.         goto ret;
  243.     }
  244.  
  245.     for (i = 0; i < groupInfo->GroupCount ; i += 1)
  246.     {
  247.         SID_AND_ATTRIBUTES sanda = groupInfo->Groups[i];
  248.         PSID Sid = sanda.Sid;
  249.     
  250.         //
  251.         //  Check to see if the group we're looking at is one of
  252.         //  the 2 groups we're interested in.
  253.         //
  254.     
  255.         if (EqualSid(Sid, InteractiveSid))
  256.         {
  257.             //
  258.             //  This process has the Interactive SID in its
  259.             //  token.  This means that the process is running as
  260.             //  an EXE.
  261.             //
  262.             goto ret;
  263.         }
  264.         else if (EqualSid(Sid, ServiceSid))
  265.         {
  266.             //
  267.             //  This process has the Service SID in its
  268.             //  token.  This means that the process is running as
  269.             //  a service running in a user account.
  270.             //
  271.             fExe = FALSE;
  272.             goto ret;
  273.         }
  274.     }
  275.  
  276.     //
  277.     //  Neither Interactive or Service was present in the current users token,
  278.     //  This implies that the process is running as a service, most likely
  279.     //  running as LocalSystem.
  280.     //
  281.     fExe = FALSE;
  282.  
  283. ret:
  284.  
  285.     if (InteractiveSid)
  286.         FreeSid(InteractiveSid);
  287.  
  288.     if (ServiceSid)
  289.         FreeSid(ServiceSid);
  290.  
  291.     if (groupInfo)
  292.         LocalFree(groupInfo);
  293.  
  294.     if (hProcessToken)
  295.         CloseHandle(hProcessToken);
  296.  
  297.     return(fExe);
  298. }
  299.  
  300. #endif
  301.  
  302. /* LogIt */
  303.  
  304. #ifndef _MAC
  305. void    LogIt(LPSTR plpcText, BOOL  fUseAlert)
  306. {
  307. #if defined(DEBUG) && defined(_WINNT)
  308.     LPSTR           llpcStr[2];
  309.     static HANDLE   hEventSource = NULL;
  310.  
  311.     if (pfnRegisterEventSourceA == NULL)
  312.     {
  313.         /* This handle is not important as the lib will be freed on exit (and it's debug only) */
  314.         HINSTANCE       lhLib = LoadLibraryA("advapi32.dll");
  315.         
  316.         if (!lhLib)
  317.             return;
  318.         
  319.         pfnRegisterEventSourceA = (RegisterEventSourceAFN) GetProcAddress(lhLib, "RegisterEventSourceA");
  320.         pfnReportEvent = (ReportEventFN) GetProcAddress(lhLib, "ReportEventA");
  321.         
  322.         if (!pfnRegisterEventSourceA || !pfnReportEvent)
  323.             return;
  324.     }
  325.         
  326.     if (!hEventSource)                                      
  327.         hEventSource = pfnRegisterEventSourceA(NULL, "MAPIDebug");
  328.  
  329.     llpcStr[0] = "MAPI Debug Log";
  330.     llpcStr[1] = plpcText;
  331.  
  332.     pfnReportEvent(hEventSource,    /* handle of event source */
  333.         EVENTLOG_ERROR_TYPE,        /* event type             */
  334.         0,                          /* event category         */
  335.         0,                          /* event ID               */
  336.         NULL,                       /* current user's SID     */
  337.         2,                          /* strings in lpszStrings */
  338.         0,                          /* no bytes of raw data   */
  339.         llpcStr,                    /* array of error strings */
  340.         NULL);                      /* no raw data            */
  341.         
  342.     /* Now we generate an Alert! */
  343.     /* This code is adapted from PierreC's stuff, and NEEDS TO BE UNICODE!!!! */
  344.     if (fUseAlert)
  345.     {
  346. #define MAX_LINE        256
  347.  
  348. typedef NET_API_STATUS  (WINAPI *NAREFN)(TCHAR *, ADMIN_OTHER_INFO *, ULONG, TCHAR *);
  349.  
  350.         BYTE                rgb[sizeof(ADMIN_OTHER_INFO) + (sizeof(WCHAR) * MAX_LINE)];
  351.         ADMIN_OTHER_INFO *  poi     = (ADMIN_OTHER_INFO *) rgb;
  352.         WCHAR *             pch     = (WCHAR *) (rgb + sizeof(ADMIN_OTHER_INFO));
  353.         NET_API_STATUS      nas;
  354.         static   NAREFN     fnNetAlertRaiseEx = NULL;
  355.         
  356.         /* Resolve function here, never free library as it's debug only */
  357.         if (!fnNetAlertRaiseEx)
  358.         {
  359.             HINSTANCE       lhLib = LoadLibrary("NETAPI32.DLL");
  360.             if (lhLib)
  361.                 fnNetAlertRaiseEx = (NAREFN) GetProcAddress(lhLib, "NetAlertRaiseEx");
  362.         }
  363.         
  364.         if (fnNetAlertRaiseEx)
  365.         {
  366.             poi->alrtad_errcode = (DWORD) -1;
  367.             poi->alrtad_numstrings = 1;
  368.             
  369.             if (MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, plpcText, -1, pch, MAX_LINE))
  370.             {
  371.                 nas = fnNetAlertRaiseEx(
  372.                             (TCHAR *) L"ADMIN",
  373.                             poi, 
  374.                             sizeof(ADMIN_OTHER_INFO) + ((lstrlenW(pch) + 1) * sizeof(WCHAR)),
  375.                             (TCHAR *) L"MAPI Assert");
  376.                     
  377.                         
  378.             }
  379.         }
  380.     }
  381.     
  382. #endif /* DEBUG && NT */
  383. }
  384. #endif /* !_MAC */
  385.  
  386. /* DebugOutputFn ------------------------------------------------------------ */
  387.  
  388. char BASED_CODE szCR[] = "\r";
  389.  
  390. void DebugOutputFn(char *psz)
  391. {
  392. #if defined(_MAC)
  393.  
  394.     OutputDebugString(psz);
  395.  
  396. #else
  397.  
  398. #if defined(WIN16) || defined(_WIN32)
  399.     if (fTraceEnabled == -1)
  400.     {
  401.         fTraceEnabled = GetPrivateProfileIntA(szSectionDebug, szKeyTraceEnabled,
  402.             0, szDebugIni);
  403.  
  404.         fUseEventLog = GetPrivateProfileIntA(szSectionDebug, szKeyEventLog,
  405.             0, szDebugIni);     
  406.     }
  407.  
  408.     if (!fTraceEnabled)
  409.         return;
  410.  
  411.     if (fUseEventLog)
  412. #else
  413.     if (FALSE)
  414. #endif
  415.         LogIt(psz, FALSE);
  416.  
  417. #ifdef WIN16
  418.     OutputDebugString(psz);
  419.     OutputDebugString(szCR);
  420. #else
  421.     OutputDebugStringA(psz);
  422.     OutputDebugStringA(szCR);
  423. #endif
  424.     
  425. #endif  /* _MAC */
  426. }
  427.  
  428.  
  429. /* DebugTrapFn -------------------------------------------------------------- */
  430.  
  431. #if defined(_WIN32) && !defined(_MAC)
  432.  
  433. typedef struct {
  434.     char *      sz1;
  435.     char *      sz2;
  436.     UINT        rgf;
  437.     int         iResult;
  438. } MBContext;
  439.  
  440. DWORD WINAPI MessageBoxFnThreadMain(MBContext *pmbc)
  441. {
  442.     if (fUseEventLog)
  443.     {
  444.         LogIt(pmbc->sz1, TRUE);
  445.         pmbc->iResult = IDIGNORE;
  446.     }
  447.     else
  448.         pmbc->iResult = MessageBoxA(NULL, pmbc->sz1, pmbc->sz2,
  449.             pmbc->rgf | MB_SETFOREGROUND);
  450.  
  451.     return(0);
  452. }
  453.  
  454. int MessageBoxFn(char *sz1, char *sz2, UINT rgf)
  455. {
  456.     HANDLE      hThread;
  457.     DWORD       dwThreadId;
  458.     MBContext   mbc;
  459.  
  460.     mbc.sz1     = sz1;
  461.     mbc.sz2     = sz2;
  462.     mbc.rgf     = rgf;
  463.     mbc.iResult = IDRETRY;
  464.  
  465.     #if defined(_WIN32) && !defined(_MAC)
  466.     if (fInhibitTrapThread == 2)
  467.         fInhibitTrapThread = GetPrivateProfileIntA(szSectionDebug,
  468.             szKeyInhibitTrapThread, 0, szDebugIni);
  469.     #endif
  470.  
  471.     if (fInhibitTrapThread)
  472.     {
  473.         MessageBoxFnThreadMain(&mbc);
  474.     }
  475.     else
  476.     {
  477.         hThread = CreateThread(NULL, 0,
  478.             (PTHREAD_START_ROUTINE)MessageBoxFnThreadMain, &mbc, 0, &dwThreadId);
  479.  
  480.         if (hThread != NULL) {
  481.             WaitForSingleObject(hThread, INFINITE);
  482.             CloseHandle(hThread);
  483.         }
  484.     }
  485.  
  486.     return(mbc.iResult);
  487. }
  488. #else
  489. #define MessageBoxFn(sz1, sz2, rgf)     MessageBoxA(NULL, sz1, sz2, rgf)
  490. #endif
  491.  
  492. int EXPORTDBG __cdecl DebugTrapFn(int fFatal, char *pszFile, int iLine, char *pszFormat, ...)
  493. {
  494.     char    sz[512];
  495.     va_list vl;
  496.  
  497.     #if defined(WIN16) || defined(_WIN32)
  498.     int     id;
  499.     #endif
  500.     #if defined(_WIN32) && !defined(_MAC)
  501.     static int iServiceFlag = -1;
  502.     #endif
  503.  
  504.     lstrcpyA(sz, "++++ MAPI Debug Trap (");
  505.     _strdate(sz + lstrlenA(sz));
  506.     lstrcatA(sz, " ");
  507.     _strtime(sz + lstrlenA(sz));
  508.     lstrcatA(sz, ")\n");
  509.     DebugOutputFn(sz);
  510.  
  511.     va_start(vl, pszFormat);
  512.     wvsprintfA(sz, pszFormat, vl);
  513.     va_end(vl);
  514.  
  515.     wsprintfA(sz + lstrlenA(sz), "\n[File %s, Line %d]\n\n", pszFile, iLine);
  516.  
  517.     DebugOutputFn(sz);
  518.  
  519.     #if defined(DOS)
  520.     _asm { int 3 }
  521.     #endif
  522.  
  523. #if defined(WIN16) || defined(_WIN32)
  524.     /* Hold down control key to prevent MessageBox */
  525.     if ( GetAsyncKeyState(VK_CONTROL) >= 0 )
  526.     {
  527.         UINT uiFlags = MB_ABORTRETRYIGNORE;
  528.  
  529.         if (fFatal)
  530.             uiFlags |= MB_DEFBUTTON1;
  531.         else
  532.             uiFlags |= MB_DEFBUTTON3;
  533.  
  534.         #ifdef WIN16
  535.         uiFlags |= MB_ICONEXCLAMATION | MB_SYSTEMMODAL;
  536.         #else
  537.         uiFlags |= MB_ICONSTOP | MB_TASKMODAL;
  538.         #endif
  539.  
  540.         #if defined(_WIN32) && !defined(_MAC)
  541.         if (iServiceFlag == -1)
  542.         {
  543.             DWORD dwVersion = GetVersion();
  544.  
  545.             if (dwVersion & 0x80000000)
  546.             {
  547.                 if (LOBYTE(LOWORD(dwVersion)) < 4)
  548.                 {
  549.                     //  NT 3.51
  550.                     iServiceFlag = 0x00040000;
  551.                 }
  552.                 else
  553.                 {
  554.                     //  NT 4.0+
  555.                     iServiceFlag = 0x00200000;
  556.                 }
  557.             }
  558.             else
  559.                 //  not NT, skip this
  560.                 iServiceFlag = 0;
  561.         }
  562.  
  563.         if (!IsDBGServiceAnExe())
  564.             uiFlags |= (UINT) iServiceFlag;
  565.         #endif
  566.  
  567.         id = MessageBoxFn(sz, "MAPI Debug Trap", uiFlags);
  568.  
  569.         if (id == IDABORT)
  570.             *((LPBYTE)NULL) = 0;
  571.         else if (id == IDRETRY)
  572.             DebugBreak();
  573.     }
  574. #endif
  575.  
  576.     return(0);
  577. }
  578.  
  579. /* DebugTraceFn ------------------------------------------------------------- */
  580.  
  581. int EXPORTDBG __cdecl DebugTraceFn(char *pszFormat, ...)
  582. {
  583.     char    sz[768];
  584.     int     fAutoLF = 0;
  585.     va_list vl;
  586.  
  587.     if (*pszFormat == '~') {
  588.         pszFormat += 1;
  589.         fAutoLF = 1;
  590.     }
  591.  
  592.     va_start(vl, pszFormat);
  593.     wvsprintfA(sz, pszFormat, vl);
  594.     va_end(vl);
  595.  
  596. #ifndef _MAC
  597.     if (fAutoLF)
  598.         lstrcatA(sz, "\n");
  599. #endif
  600.  
  601.     DebugOutputFn(sz);
  602.  
  603.     return(0);
  604. }
  605.  
  606. /* DebugTraceProblemsFn */
  607.  
  608. void EXPORTDBG __cdecl DebugTraceProblemsFn(LPSTR sz, LPVOID pv)
  609. {
  610.     LPSPropProblemArray pprobs = (LPSPropProblemArray)pv;
  611.     SPropProblem *      pprob = pprobs->aProblem;
  612.     int                 cprob = (int)pprobs->cProblem;
  613.  
  614.     DebugTraceFn("%s: SetProps problem\n", sz);
  615.     while (cprob--)
  616.     {
  617.         DebugTraceFn("Property %s (index %ld): failed with %s\n",
  618.             SzDecodeUlPropTagFn(pprob->ulPropTag),
  619.             pprob->ulIndex,
  620.             SzDecodeScodeFn(pprob->scode));
  621.     }
  622. }
  623.  
  624. /* SCODE & PropTag decoding ------------------------------------------------- */
  625.  
  626. typedef struct
  627. {
  628.     char *          psz;
  629.     unsigned long   ulPropTag;
  630. } PT;
  631.  
  632. typedef struct
  633. {
  634.     char *  psz;
  635.     SCODE   sc;
  636. } SC;
  637.  
  638. #define Pt(_ptag)   {#_ptag, _ptag}
  639. #define Sc(_sc)     {#_sc, _sc}
  640.  
  641. #if !defined(DOS)
  642. static PT BASED_DEBUG rgpt[] = {
  643.     
  644. #include "_tags.h"
  645.     
  646. /*
  647.  * Property types
  648.  */
  649.     Pt(PR_NULL),
  650.     Pt(PT_UNSPECIFIED),
  651.     Pt(PT_NULL),
  652.     Pt(PT_I2),
  653.     Pt(PT_LONG),
  654.     Pt(PT_R4),
  655.     Pt(PT_DOUBLE),
  656.     Pt(PT_CURRENCY),
  657.     Pt(PT_APPTIME),
  658.     Pt(PT_ERROR),
  659.     Pt(PT_BOOLEAN),
  660.     Pt(PT_OBJECT),
  661.     Pt(PT_I8),
  662.     Pt(PT_STRING8),
  663.     Pt(PT_UNICODE),
  664.     Pt(PT_SYSTIME),
  665.     Pt(PT_CLSID),
  666.     Pt(PT_BINARY),
  667.     Pt(PT_TSTRING),
  668.     Pt(PT_MV_I2),
  669.     Pt(PT_MV_LONG),
  670.     Pt(PT_MV_R4),
  671.     Pt(PT_MV_DOUBLE),
  672.     Pt(PT_MV_CURRENCY),
  673.     Pt(PT_MV_APPTIME),
  674.     Pt(PT_MV_SYSTIME),
  675.     Pt(PT_MV_STRING8),
  676.     Pt(PT_MV_BINARY),
  677.     Pt(PT_MV_UNICODE),
  678.     Pt(PT_MV_CLSID),
  679.     Pt(PT_MV_I8)
  680. };
  681.  
  682. #define cpt (sizeof(rgpt) / sizeof(PT))
  683.  
  684. static SC BASED_DEBUG rgsc[] = {
  685.  
  686. /* FACILITY_NULL error codes from OLE */
  687.  
  688.     Sc(S_OK),
  689.     Sc(S_FALSE),
  690.  
  691.     Sc(E_UNEXPECTED),
  692.     Sc(E_NOTIMPL),
  693.     Sc(E_OUTOFMEMORY),
  694.     Sc(E_INVALIDARG),
  695.     Sc(E_NOINTERFACE),
  696.     Sc(E_POINTER),
  697.     Sc(E_HANDLE),
  698.     Sc(E_ABORT),
  699.     Sc(E_FAIL),
  700.     Sc(E_ACCESSDENIED),
  701.  
  702. /* MAPI error codes from MAPICODE.H */
  703. #include "_scode.h"
  704.                     
  705. };
  706.  
  707. #define csc (sizeof(rgsc) / sizeof(SC))
  708. #endif
  709.  
  710. char * EXPORTDBG __cdecl
  711. SzDecodeScodeFn(SCODE sc)
  712. {
  713.     static char rgch[64];
  714.  
  715.     #if !defined(DOS)
  716.     int isc;
  717.     for (isc = 0; isc < csc; ++isc)
  718.         if (sc == rgsc[isc].sc)
  719.             return rgsc[isc].psz;
  720.     #endif
  721.  
  722.     wsprintfA (rgch, "%08lX", sc);
  723.     return rgch;
  724. }
  725.  
  726. char * EXPORTDBG __cdecl
  727. SzDecodeUlPropTypeFn(unsigned long ulPropType)
  728. {
  729.     static char rgch[8];
  730.  
  731.     switch (ulPropType)
  732.     {
  733.     case PT_UNSPECIFIED:    return("PT_UNSPECIFIED");   break;
  734.     case PT_NULL:           return("PT_NULL");          break;
  735.     case PT_I2:             return("PT_I2");            break;
  736.     case PT_LONG:           return("PT_LONG");          break;
  737.     case PT_R4:             return("PT_R4");            break;
  738.     case PT_DOUBLE:         return("PT_DOUBLE");        break;
  739.     case PT_CURRENCY:       return("PT_CURRENCY");      break;
  740.     case PT_APPTIME:        return("PT_APPTIME");       break;
  741.     case PT_ERROR:          return("PT_ERROR");         break;
  742.     case PT_BOOLEAN:        return("PT_BOOLEAN");       break;
  743.     case PT_OBJECT:         return("PT_OBJECT");        break;
  744.     case PT_I8:             return("PT_I8");            break;
  745.     case PT_STRING8:        return("PT_STRING8");       break;
  746.     case PT_UNICODE:        return("PT_UNICODE");       break;
  747.     case PT_SYSTIME:        return("PT_SYSTIME");       break;
  748.     case PT_CLSID:          return("PT_CLSID");         break;
  749.     case PT_BINARY:         return("PT_BINARY");        break;
  750.     }
  751.  
  752.     wsprintfA(rgch, "0x%04lX", ulPropType);
  753.     return rgch;
  754. }
  755.  
  756. char *  EXPORTDBG __cdecl
  757. SzDecodeUlPropTagFn(unsigned long ulPropTag)
  758. {
  759.     static char rgch[64];
  760.  
  761.     #if !defined(DOS)
  762.     int ipt;
  763.     for (ipt = 0; ipt < cpt; ++ipt)
  764.         if (ulPropTag == rgpt[ipt].ulPropTag)
  765.             return rgpt[ipt].psz;
  766.     #endif
  767.  
  768.     wsprintfA(rgch, "PROP_TAG(%s, 0x%04lX)",
  769.         SzDecodeUlPropType(PROP_TYPE(ulPropTag)),
  770.         PROP_ID(ulPropTag));
  771.     return rgch;
  772. }
  773.  
  774. SCODE  EXPORTDBG __cdecl
  775. ScodeFromSzFn(char *psz)
  776. {
  777.     #if !defined(DOS)
  778.     int isc;
  779.     for (isc = 0; isc < csc; ++isc)
  780.         {
  781.         if (lstrcmpA(psz, rgsc[isc].psz) == 0)
  782.             {
  783.             return rgsc[isc].sc;
  784.             }
  785.         }
  786.     #endif
  787.     return 0;
  788. }
  789.  
  790. unsigned long EXPORTDBG __cdecl
  791. UlPropTagFromSzFn(char *psz)
  792. {
  793.     #if !defined(DOS)
  794.     int ipt;
  795.     for (ipt = 0; ipt < cpt; ++ipt)
  796.         {
  797.         if (lstrcmpA(psz, rgpt[ipt].psz) == 0)
  798.             {
  799.             return rgpt[ipt].ulPropTag;
  800.             }
  801.         }
  802.     #endif
  803.     return 0;
  804. }
  805.  
  806. /* ScCheckScFn -------------------------------------------------------------- */
  807.  
  808. #if !defined(DOS)
  809.  
  810. SCODE EXPORTDBG __cdecl ScCheckScFn(    SCODE   sc,
  811.                     SCODE * lpscLegal,
  812.                     char *  lpszMethod,
  813.                     char *  lpszFile,
  814.                     int     iLine)
  815. {
  816.     BOOL fIsQueryInterface = (lpscLegal == IUnknown_QueryInterface_Scodes);
  817.  
  818.     if (sc == S_OK)
  819.         return(sc);
  820.  
  821.     while( *lpscLegal != S_OK && sc != *lpscLegal )
  822.     {
  823.         lpscLegal++;
  824.     }
  825.  
  826.     if ( *lpscLegal == S_OK )
  827.     {
  828.         SCODE *lpscNextCommon = Common_Scodes;
  829.  
  830.         /* see if this is a common scode */
  831.             if ( !fIsQueryInterface )
  832.                 while(  *lpscNextCommon != S_OK &&
  833.                         sc != *lpscNextCommon )
  834.                 {
  835.                     lpscNextCommon++;
  836.                 }
  837.  
  838.         /* this is an illegal error or an RPC error */
  839.            if ( (*lpscNextCommon == S_OK || fIsQueryInterface) &&
  840.                 ( SCODE_FACILITY(sc) != FACILITY_RPC) )
  841.            {
  842.                 DebugTrace( "Unrecognized scode %s from %s\n\t in file %s line %d\n",
  843.                         SzDecodeScode( sc ), lpszMethod, lpszFile, iLine);
  844.             }
  845.     }
  846.  
  847.     return(sc);
  848. }
  849. #endif
  850.  
  851. /* SCODE lists -------------------------------------------------------------- */
  852.  
  853. #if !defined(DOS)
  854.  
  855. #define STANDARD_OPENENTRY_SCODES \
  856.     E_NOINTERFACE,  \
  857.     MAPI_E_NOT_FOUND
  858.  
  859. SCODE BASED_DEBUG Common_Scodes[] =
  860. {
  861.     MAPI_E_BAD_CHARWIDTH,
  862.     MAPI_E_CALL_FAILED,
  863.     MAPI_E_INVALID_ENTRYID,
  864.     MAPI_E_INVALID_OBJECT,
  865.     MAPI_E_INVALID_PARAMETER,
  866.     MAPI_E_NO_ACCESS,
  867.     MAPI_E_NO_SUPPORT,
  868.     MAPI_E_NOT_ENOUGH_MEMORY,
  869.     MAPI_E_UNKNOWN_FLAGS,
  870.     S_OK
  871. };
  872.  
  873. SCODE BASED_DEBUG MAPILogon_Scodes[] =
  874. {
  875.     MAPI_E_NOT_INITIALIZED,
  876.     MAPI_E_LOGON_FAILED,
  877.     S_OK
  878. };
  879.  
  880. SCODE BASED_DEBUG MAPIAllocateBuffer_Scodes[] =
  881. {
  882.     MAPI_E_NOT_INITIALIZED,
  883.     S_OK
  884. };
  885.  
  886. SCODE BASED_DEBUG MAPIAllocateMore_Scodes[] =
  887. {
  888.     MAPI_E_NOT_INITIALIZED,
  889.     S_OK
  890. };
  891.  
  892. SCODE BASED_DEBUG MAPIFreeBuffer_Scodes[] =
  893. {
  894.     S_OK
  895. };
  896.  
  897. SCODE BASED_DEBUG IUnknown_QueryInterface_Scodes[] =
  898. {
  899.     E_INVALIDARG,
  900.     E_NOINTERFACE,
  901.     S_OK
  902. };
  903.  
  904. SCODE BASED_DEBUG IUnknown_GetLastError_Scodes[] =
  905. {
  906.     MAPI_E_EXTENDED_ERROR,
  907.     S_OK
  908. };
  909.  
  910. SCODE BASED_DEBUG IMAPIProp_CopyTo_Scodes[] =
  911. {
  912.     MAPI_W_ERRORS_RETURNED,
  913.     MAPI_E_INVALID_TYPE,
  914.     MAPI_E_FOLDER_CYCLE,
  915.     MAPI_E_DECLINE_COPY,
  916.     E_NOINTERFACE,
  917.     S_OK
  918. };
  919.  
  920. SCODE BASED_DEBUG IMAPIProp_CopyProps_Scodes[] =
  921. {
  922.     MAPI_W_ERRORS_RETURNED,
  923.     MAPI_W_PARTIAL_COMPLETION,
  924.     MAPI_E_INVALID_TYPE,
  925.     MAPI_E_FOLDER_CYCLE,
  926.     MAPI_E_DECLINE_COPY,
  927.     E_NOINTERFACE,
  928.     S_OK
  929. };
  930.  
  931. SCODE BASED_DEBUG IMAPIProp_DeleteProps_Scodes[] =
  932. {
  933.     MAPI_W_ERRORS_RETURNED,
  934.     MAPI_E_INVALID_TYPE,
  935.     S_OK
  936. };
  937.  
  938. SCODE BASED_DEBUG IMAPIProp_GetIDsFromNames_Scodes[] =
  939. {
  940.     MAPI_W_ERRORS_RETURNED,
  941.     MAPI_E_TABLE_TOO_BIG,
  942.     S_OK
  943. };
  944.  
  945. SCODE BASED_DEBUG IMAPIProp_GetLastError_Scodes[] =
  946. {
  947.     MAPI_E_EXTENDED_ERROR,
  948.     S_OK
  949. };
  950.  
  951. SCODE BASED_DEBUG IMAPIProp_GetNamesFromIDs_Scodes[] =
  952. {
  953.     MAPI_W_ERRORS_RETURNED,
  954.     S_OK
  955. };
  956.  
  957. SCODE BASED_DEBUG IMAPIProp_GetPropList_Scodes[] =
  958. {
  959.     MAPI_W_ERRORS_RETURNED,
  960.     S_OK
  961. };
  962.  
  963. SCODE BASED_DEBUG IMAPIProp_GetProps_Scodes[] =
  964. {
  965.     MAPI_E_NOT_FOUND,
  966.     MAPI_E_OBJECT_DELETED,
  967.     MAPI_W_ERRORS_RETURNED,
  968.     S_OK
  969. };
  970.  
  971. SCODE BASED_DEBUG IMAPIProp_OpenProperty_Scodes[] =
  972. {
  973.     MAPI_E_INTERFACE_NOT_SUPPORTED,
  974.     MAPI_E_NOT_FOUND,
  975.     MAPI_E_OBJECT_DELETED,
  976.     S_OK
  977. };
  978.  
  979. SCODE BASED_DEBUG IMAPIProp_SetProps_Scodes[] =
  980. {
  981.     MAPI_E_COMPUTED,
  982.     MAPI_E_UNEXPECTED_TYPE,
  983.     MAPI_E_INVALID_TYPE,
  984.     S_OK
  985. };
  986.  
  987. SCODE BASED_DEBUG IMAPIProp_SaveChanges_Scodes[] =
  988. {
  989.     MAPI_E_NOT_ENOUGH_DISK,
  990.     MAPI_E_OBJECT_CHANGED,
  991.     MAPI_E_OBJECT_DELETED,
  992.     S_OK
  993. };
  994.  
  995. SCODE BASED_DEBUG IStream_Read_Scodes[] = {S_OK};
  996. SCODE BASED_DEBUG IStream_Write_Scodes[] = {S_OK};
  997. SCODE BASED_DEBUG IStream_Seek_Scodes[] = {S_OK};
  998. SCODE BASED_DEBUG IStream_SetSize_Scodes[] = {S_OK};
  999. SCODE BASED_DEBUG IStream_Tell_Scodes[] = {S_OK};
  1000. SCODE BASED_DEBUG IStream_LockRegion_Scodes[] = {S_OK};
  1001. SCODE BASED_DEBUG IStream_UnlockRegion_Scodes[] = {S_OK};
  1002. SCODE BASED_DEBUG IStream_Clone_Scodes[] = {S_OK};
  1003. SCODE BASED_DEBUG IStream_CopyTo_Scodes[] = {S_OK};
  1004. SCODE BASED_DEBUG IStream_Revert_Scodes[] = {S_OK};
  1005. SCODE BASED_DEBUG IStream_Stat_Scodes[] = {S_OK};
  1006. SCODE BASED_DEBUG IStream_Commit_Scodes[] = {S_OK};
  1007.  
  1008. SCODE BASED_DEBUG IMAPITable_GetLastError_Scodes[] = {S_OK};
  1009. SCODE BASED_DEBUG IMAPITable_Advise_Scodes[] =
  1010. {
  1011.     S_OK
  1012. };
  1013. SCODE BASED_DEBUG IMAPITable_Unadvise_Scodes[] = {S_OK};
  1014. SCODE BASED_DEBUG IMAPITable_GetStatus_Scodes[] = {S_OK};
  1015. SCODE BASED_DEBUG IMAPITable_SetColumns_Scodes[] =
  1016. {
  1017.     MAPI_E_BUSY,
  1018.     S_OK
  1019. };
  1020. SCODE BASED_DEBUG IMAPITable_QueryColumns_Scodes[] =
  1021. {
  1022.     MAPI_E_BUSY,
  1023.     S_OK
  1024. };
  1025. SCODE BASED_DEBUG IMAPITable_GetRowCount_Scodes[] =
  1026. {
  1027.     MAPI_E_BUSY,
  1028.     MAPI_W_APPROX_COUNT,
  1029.     S_OK
  1030. };
  1031. SCODE BASED_DEBUG IMAPITable_SeekRow_Scodes[] =
  1032. {
  1033.     MAPI_E_INVALID_BOOKMARK,
  1034.     MAPI_E_UNABLE_TO_COMPLETE,
  1035.     MAPI_W_POSITION_CHANGED,
  1036.     S_OK
  1037. };
  1038. SCODE BASED_DEBUG IMAPITable_SeekRowApprox_Scodes[] = {S_OK};
  1039. SCODE BASED_DEBUG IMAPITable_QueryPosition_Scodes[] = {S_OK};
  1040. SCODE BASED_DEBUG IMAPITable_FindRow_Scodes[] =
  1041. {
  1042.     MAPI_E_INVALID_BOOKMARK,
  1043.     MAPI_E_NOT_FOUND,
  1044.     MAPI_W_POSITION_CHANGED,
  1045.     S_OK
  1046. };
  1047. SCODE BASED_DEBUG IMAPITable_Restrict_Scodes[] =
  1048. {
  1049.     MAPI_E_BUSY,
  1050.     S_OK
  1051. };
  1052. SCODE BASED_DEBUG IMAPITable_CreateBookmark_Scodes[] =
  1053. {
  1054.     MAPI_E_UNABLE_TO_COMPLETE,
  1055.     S_OK
  1056. };
  1057. SCODE BASED_DEBUG IMAPITable_FreeBookmark_Scodes[] = {S_OK};
  1058. SCODE BASED_DEBUG IMAPITable_SortTable_Scodes[] =
  1059. {
  1060.     MAPI_E_TOO_COMPLEX,
  1061.     S_OK
  1062. };
  1063. SCODE BASED_DEBUG IMAPITable_QuerySortOrder_Scodes[] = {S_OK};
  1064. SCODE BASED_DEBUG IMAPITable_QueryRows_Scodes[] =
  1065. {
  1066.     MAPI_E_INVALID_BOOKMARK,
  1067.     MAPI_W_POSITION_CHANGED,
  1068.     S_OK
  1069. };
  1070.  
  1071. SCODE BASED_DEBUG IMAPITable_Abort_Scodes[] =
  1072. {
  1073.     MAPI_E_UNABLE_TO_ABORT,
  1074.     S_OK
  1075. };
  1076. SCODE BASED_DEBUG IMAPITable_ExpandRow_Scodes[] = {S_OK};
  1077. SCODE BASED_DEBUG IMAPITable_CollapseRow_Scodes[] = {S_OK};
  1078. SCODE BASED_DEBUG IMAPITable_WaitForCompletion_Scodes[] =
  1079. {
  1080.     MAPI_E_TIMEOUT,
  1081.     S_OK
  1082. };
  1083. SCODE BASED_DEBUG IMAPITable_GetCollapseState_Scodes[] = {S_OK};
  1084. SCODE BASED_DEBUG IMAPITable_SetCollapseState_Scodes[] = {S_OK};
  1085.  
  1086.  
  1087. SCODE BASED_DEBUG IMAPISession_LogOff_Scodes[] = {S_OK};
  1088. SCODE BASED_DEBUG IMAPISession_Release_Scodes[] = {S_OK};
  1089. SCODE BASED_DEBUG IMAPISession_GetLastError_Scodes[] =
  1090. {
  1091.     MAPI_E_EXTENDED_ERROR,
  1092.     S_OK
  1093. };
  1094. SCODE BASED_DEBUG IMAPISession_GetMsgStoresTable_Scodes[] = {S_OK};
  1095. SCODE BASED_DEBUG IMAPISession_GetStatusTable_Scodes[] = {S_OK};
  1096. SCODE BASED_DEBUG IMAPISession_OpenMsgStore_Scodes[] = {S_OK};
  1097. SCODE BASED_DEBUG IMAPISession_OpenAddressBook_Scodes[] = {S_OK};
  1098.  
  1099. SCODE BASED_DEBUG IMAPISession_OpenEntry_Scodes[] =
  1100. {
  1101.     STANDARD_OPENENTRY_SCODES,
  1102.     S_OK
  1103. };
  1104.  
  1105. SCODE BASED_DEBUG IMAPISession_OpenProfileSection_Scodes[] = {S_OK};
  1106. SCODE BASED_DEBUG IMAPISession_Advise_Scodes[] = {S_OK};
  1107. SCODE BASED_DEBUG IMAPISession_Unadvise_Scodes[] = {S_OK};
  1108. SCODE BASED_DEBUG IMAPISession_CompareEntryIDs_Scodes[] = {S_OK};
  1109. SCODE BASED_DEBUG IMAPISession_MessageOptions_Scodes[] = {S_OK};
  1110. SCODE BASED_DEBUG IMAPISession_QueryDefaultMessageOpt_Scodes[] = {S_OK};
  1111. SCODE BASED_DEBUG IMAPISession_EnumAdrTypes_Scodes[] = {S_OK};
  1112. SCODE BASED_DEBUG IMAPISession_QueryIdentity_Scodes[] = {S_OK};
  1113.  
  1114. SCODE BASED_DEBUG IMsgStore_OpenEntry_Scodes[] =
  1115. {
  1116.     STANDARD_OPENENTRY_SCODES,
  1117.     MAPI_E_SUBMITTED,
  1118.     S_OK
  1119. };
  1120.  
  1121. SCODE BASED_DEBUG IMsgStore_SetReceiveFolder_Scodes[] =
  1122. {
  1123.     MAPI_E_BAD_CHARWIDTH,
  1124.     MAPI_E_NOT_FOUND,
  1125.     S_OK
  1126. };
  1127.  
  1128. SCODE BASED_DEBUG IMsgStore_GetReceiveFolder_Scodes[] =
  1129. {
  1130.     MAPI_E_BAD_CHARWIDTH,
  1131.     S_OK
  1132. };
  1133.  
  1134. SCODE BASED_DEBUG IMsgStore_GetReceiveFolderTable_Scodes[] = {S_OK};
  1135. SCODE BASED_DEBUG IMsgStore_StoreLogoff_Scodes[] = {S_OK};
  1136. SCODE BASED_DEBUG IMsgStore_Advise_Scodes[] = {S_OK};
  1137. SCODE BASED_DEBUG IMsgStore_Unadvise_Scodes[] = {S_OK};
  1138. SCODE BASED_DEBUG IMsgStore_CompareEntryIDs_Scodes[] = {S_OK};
  1139. SCODE BASED_DEBUG IMsgStore_GetOutgoingQueue_Scodes[] = {
  1140.     MAPI_E_NO_SUPPORT,
  1141.     S_OK};
  1142. SCODE BASED_DEBUG IMsgStore_SetLockState_Scodes[] = {
  1143.     MAPI_E_NO_SUPPORT,
  1144.     MAPI_E_NOT_FOUND,
  1145.     S_OK};
  1146. SCODE BASED_DEBUG IMsgStore_FinishedMsg_Scodes[] = {
  1147.     MAPI_E_NO_SUPPORT,
  1148.     S_OK};
  1149. SCODE BASED_DEBUG IMsgStore_AbortSubmit_Scodes[] = {
  1150.     MAPI_E_UNABLE_TO_ABORT,
  1151.     MAPI_E_NOT_IN_QUEUE,
  1152.     S_OK};
  1153. SCODE BASED_DEBUG IMsgStore_NotifyNewMail_Scodes[] = {S_OK};
  1154.  
  1155. SCODE BASED_DEBUG IMAPIFolder_GetContentsTable_Scodes[] =
  1156. {
  1157.     MAPI_E_OBJECT_DELETED,
  1158.     S_OK
  1159. };
  1160.  
  1161. SCODE BASED_DEBUG IMAPIFolder_GetHierarchyTable_Scodes[] =
  1162. {
  1163.     MAPI_E_OBJECT_DELETED,
  1164.     S_OK
  1165. };
  1166.  
  1167. SCODE BASED_DEBUG IMAPIFolder_SaveContentsSort_Scodes[] =
  1168. {
  1169.     S_OK
  1170. };
  1171.  
  1172. SCODE BASED_DEBUG IMAPIFolder_OpenEntry_Scodes[] =
  1173. {
  1174.     STANDARD_OPENENTRY_SCODES,
  1175.     MAPI_E_SUBMITTED,
  1176.     S_OK
  1177. };
  1178.  
  1179. SCODE BASED_DEBUG IMAPIFolder_CreateMessage_Scodes[] =
  1180. {
  1181.     E_NOINTERFACE,
  1182.     S_OK
  1183. };
  1184.  
  1185. SCODE BASED_DEBUG IMAPIFolder_CopyMessages_Scodes[] =
  1186. {
  1187.     E_NOINTERFACE,
  1188.     MAPI_E_SUBMITTED,
  1189.     MAPI_E_DECLINE_COPY,
  1190.     S_OK
  1191. };
  1192.  
  1193. SCODE BASED_DEBUG IMAPIFolder_DeleteMessages_Scodes[] =
  1194. {
  1195.     MAPI_E_SUBMITTED,
  1196.     S_OK
  1197. };
  1198.  
  1199. SCODE BASED_DEBUG IMAPIFolder_CreateFolder_Scodes[] =
  1200. {
  1201.     E_NOINTERFACE,
  1202.     MAPI_E_COLLISION,
  1203.     S_OK
  1204. };
  1205.  
  1206. SCODE BASED_DEBUG IMAPIFolder_CopyFolder_Scodes[] =
  1207. {
  1208.     E_NOINTERFACE,
  1209.     MAPI_E_COLLISION,
  1210.     MAPI_E_FOLDER_CYCLE,
  1211.     MAPI_E_DECLINE_COPY,
  1212.     S_OK
  1213. };
  1214.  
  1215. SCODE BASED_DEBUG IMAPIFolder_DeleteFolder_Scodes[] =
  1216. {
  1217.     MAPI_E_HAS_FOLDERS,
  1218.     MAPI_E_HAS_MESSAGES,
  1219.     MAPI_E_SUBMITTED,
  1220.     S_OK
  1221. };
  1222.  
  1223. SCODE BASED_DEBUG IMAPIFolder_SetSearchCriteria_Scodes[] =
  1224. {
  1225.     S_OK
  1226. };
  1227.  
  1228. SCODE BASED_DEBUG IMAPIFolder_GetSearchCriteria_Scodes[] =
  1229. {
  1230.     MAPI_E_NOT_INITIALIZED,
  1231.     MAPI_E_CORRUPT_STORE,
  1232.     S_OK
  1233. };
  1234.  
  1235. SCODE BASED_DEBUG IMAPIFolder_SetReadFlags_Scodes[] =
  1236. {
  1237.     S_OK
  1238. };
  1239.  
  1240. SCODE BASED_DEBUG IMAPIFolder_GetMessageStatus_Scodes[] =
  1241. {
  1242.     S_OK
  1243. };
  1244.  
  1245. SCODE BASED_DEBUG IMAPIFolder_SetMessageStatus_Scodes[] =
  1246. {
  1247.     S_OK
  1248. };
  1249.  
  1250. SCODE BASED_DEBUG IMAPIFolder_EmptyFolder_Scodes[] =
  1251. {
  1252.     MAPI_E_SUBMITTED,
  1253.     S_OK
  1254. };
  1255.  
  1256. SCODE BASED_DEBUG IMessage_GetAttachmentTable_Scodes[] =
  1257. {
  1258.     S_OK
  1259. };
  1260.  
  1261. SCODE BASED_DEBUG IMessage_OpenAttach_Scodes[] =
  1262. {
  1263.     MAPI_E_NOT_FOUND,
  1264.     E_NOINTERFACE,
  1265.     S_OK
  1266. };
  1267.  
  1268. SCODE BASED_DEBUG IMessage_CreateAttach_Scodes[] =
  1269. {
  1270.     E_NOINTERFACE,
  1271.     S_OK
  1272. };
  1273.  
  1274. SCODE BASED_DEBUG IMessage_DeleteAttach_Scodes[] =
  1275. {
  1276.     S_OK
  1277. };
  1278.  
  1279. SCODE BASED_DEBUG IMessage_GetRecipientTable_Scodes[] =
  1280. {
  1281.     S_OK
  1282. };
  1283.  
  1284. SCODE BASED_DEBUG IMessage_ModifyRecipients_Scodes[] =
  1285. {
  1286.     MAPI_E_NOT_FOUND,
  1287.     S_OK
  1288. };
  1289.  
  1290. SCODE BASED_DEBUG IMessage_SubmitMessage_Scodes[] =
  1291. {
  1292.     MAPI_E_NO_RECIPIENTS,
  1293.     MAPI_E_NON_STANDARD,
  1294.     S_OK
  1295. };
  1296.  
  1297. SCODE BASED_DEBUG IMessage_SetReadFlag_Scodes[] =
  1298. {
  1299.     S_OK
  1300. };
  1301.  
  1302. SCODE BASED_DEBUG IAttach_SaveChanges_Scodes[] =
  1303. {
  1304.     S_OK
  1305. };
  1306.  
  1307. SCODE BASED_DEBUG IAddrBook_OpenEntry_Scodes[] =
  1308. {
  1309.     STANDARD_OPENENTRY_SCODES,
  1310.     S_OK
  1311. };
  1312.  
  1313. SCODE BASED_DEBUG IAddrBook_CompareEntryIDs_Scodes[] = {S_OK};
  1314. SCODE BASED_DEBUG IAddrBook_CreateOneOff_Scodes[] = {S_OK};
  1315. SCODE BASED_DEBUG IAddrBook_ResolveName_Scodes[] = {S_OK};
  1316. SCODE BASED_DEBUG IAddrBook_Address_Scodes[] = {S_OK};
  1317. SCODE BASED_DEBUG IAddrBook_Details_Scodes[] = {S_OK};
  1318. SCODE BASED_DEBUG IAddrBook_RecipOptions_Scodes[] = {S_OK};
  1319. SCODE BASED_DEBUG IAddrBook_QueryDefaultRecipOpt_Scodes[] = {S_OK};
  1320. SCODE BASED_DEBUG IAddrBook_ButtonPress_Scodes[] = {S_OK};
  1321. SCODE BASED_DEBUG IABContainer_GetContentsTable_Scodes[] = {S_OK};
  1322. SCODE BASED_DEBUG IABContainer_GetHierarchyTable_Scodes[] = {S_OK};
  1323. SCODE BASED_DEBUG INotifObj_ChangeEvMask_Scodes[] = {S_OK};
  1324. SCODE BASED_DEBUG IMAPIStatus_ChangePassword_Scodes[] = {S_OK};
  1325. SCODE BASED_DEBUG IMAPIStatus_FlushQueues_Scodes[] = {S_OK};
  1326. SCODE BASED_DEBUG IMAPIStatus_SettingsDialog_Scodes[] = {S_OK};
  1327. SCODE BASED_DEBUG IMAPIStatus_ValidateState_Scodes[] = {S_OK};
  1328. SCODE BASED_DEBUG SMAPI_MAPILogon_Scodes[] = {
  1329.     MAPI_E_LOGON_FAILED,
  1330.     S_OK};
  1331. SCODE BASED_DEBUG SMAPI_MAPILogoff_Scodes[] = {S_OK};
  1332. SCODE BASED_DEBUG SMAPI_MAPIFreeBuffer_Scodes[] = {S_OK};
  1333. SCODE BASED_DEBUG SMAPI_MAPISendMail_Scodes[] = {S_OK};
  1334. SCODE BASED_DEBUG SMAPI_MAPISendDocuments_Scodes[] = {S_OK};
  1335. SCODE BASED_DEBUG SMAPI_MAPIFindNext_Scodes[] = {S_OK};
  1336. SCODE BASED_DEBUG SMAPI_MAPIReadMail_Scodes[] = {S_OK};
  1337. SCODE BASED_DEBUG SMAPI_MAPISaveMail_Scodes[] = {S_OK};
  1338. SCODE BASED_DEBUG SMAPI_MAPIDeleteMail_Scodes[] = {S_OK};
  1339. SCODE BASED_DEBUG SMAPI_MAPIAddress_Scodes[] = {S_OK};
  1340. SCODE BASED_DEBUG SMAPI_MAPIResolveName_Scodes[] = {S_OK};
  1341. SCODE BASED_DEBUG SMAPI_MAPIDetails_Scodes[] = {S_OK};
  1342.  
  1343. SCODE BASED_DEBUG IMSProvider_Logon_Scodes[] = {
  1344.     MAPI_E_UNCONFIGURED,
  1345.     MAPI_E_FAILONEPROVIDER,
  1346.     MAPI_E_STRING_TOO_LONG,
  1347.     MAPI_E_LOGON_FAILED,
  1348.     MAPI_E_CORRUPT_STORE,
  1349.     MAPI_E_USER_CANCEL,
  1350.     S_OK};
  1351. SCODE BASED_DEBUG IMSProvider_Deinit_Scodes[] = {
  1352.     S_OK};
  1353. SCODE BASED_DEBUG IMSProvider_Shutdown_Scodes[] = {
  1354.     S_OK};
  1355.  
  1356. SCODE BASED_DEBUG IMSProvider_Init_Scodes[] = {
  1357.     MAPI_E_VERSION,
  1358.     S_OK};
  1359. SCODE BASED_DEBUG IMSProvider_SpoolerLogon_Scodes[] = {
  1360.     MAPI_E_LOGON_FAILED,
  1361.     S_OK};
  1362.  
  1363. SCODE BASED_DEBUG IMSLogon_OpenEntry_Scodes[] =
  1364. {
  1365.     STANDARD_OPENENTRY_SCODES,
  1366.     S_OK
  1367. };
  1368.  
  1369. SCODE BASED_DEBUG IMSLogon_OpenStatusEntry_Scodes[] = {
  1370.     S_OK};
  1371.  
  1372. SCODE BASED_DEBUG IMSLogon_CompareEntryIDs_Scodes[] = {
  1373.     S_OK};
  1374.  
  1375. SCODE BASED_DEBUG IMSLogon_Advise_Scodes[] = {
  1376.     S_OK};
  1377. SCODE BASED_DEBUG IMSLogon_Unadvise_Scodes[] = {
  1378.     S_OK};
  1379. SCODE BASED_DEBUG IMSLogon_Logoff_Scodes[] = {
  1380.     S_OK};
  1381. #endif
  1382.  
  1383. /* DBGMEM ------------------------------------------------------------------- */
  1384.  
  1385. #undef  INTERFACE
  1386. #define INTERFACE struct _DBGMEM
  1387. DECLARE_INTERFACE(DBGMEM_)
  1388. {
  1389.     BEGIN_INTERFACE
  1390.     STDMETHOD(QueryInterface)       (THIS_ REFIID riid, LPVOID FAR* ppvObj) PURE; \
  1391.     STDMETHOD_(ULONG,AddRef)        (THIS) PURE; \
  1392.     STDMETHOD_(ULONG,Release)       (THIS) PURE; \
  1393.     STDMETHOD_(void FAR*, Alloc)    (THIS_ ULONG cb) PURE; \
  1394.     STDMETHOD_(void FAR*, Realloc)  (THIS_ void FAR* pv, ULONG cb) PURE; \
  1395.     STDMETHOD_(void, Free)          (THIS_ void FAR* pv) PURE; \
  1396.     STDMETHOD_(ULONG, GetSize)      (THIS_ void FAR* pv) PURE; \
  1397.     STDMETHOD_(int, DidAlloc)       (THIS_ void FAR* pv) PURE; \
  1398.     STDMETHOD_(void, HeapMinimize)  (THIS) PURE; \
  1399. };
  1400.  
  1401. extern DBGMEM_Vtbl vtblDBGMEM;
  1402.  
  1403. typedef struct _DBGMEM  DBGMEM,  FAR *PDBGMEM;
  1404. typedef struct _BLK     BLK,     *PBLK;
  1405. typedef struct _BLK UNALIGNED * PUABLK;
  1406. typedef struct _BLKTAIL BLKTAIL, *PBLKTAIL;
  1407.  
  1408. struct _DBGMEM {
  1409.     DBGMEM_Vtbl *       lpVtbl;
  1410.     ULONG               cRef;
  1411.     LPMALLOC            pmalloc;
  1412.     char                szSubsys[16];
  1413.     ULONG               ulAllocNum;
  1414.     ULONG               ulAllocAt;
  1415.     ULONG               ulFailureAt;
  1416.     BOOL                fCheckOften;
  1417.     BOOL                fUnleakable;
  1418.     ULONG               cbVirtual;
  1419.     BOOL                fFillRandom;
  1420.     int                 cbExtra;
  1421.     int                 cbTail;
  1422.     PBLK                pblkHead;
  1423. #if defined(_WIN32) && defined(_X86_)
  1424.     CRITICAL_SECTION    cs;
  1425. #endif
  1426. };
  1427.  
  1428. #define NCALLERS    12
  1429.  
  1430. struct _BLK {
  1431.     PDBGMEM         pdbgmem;        /* pointer to the allocator */
  1432.     PBLK            pblkNext;       /* next link in chain of allocated blocks */
  1433.     PBLK            pblkPrev;       /* prev link in chain of allocated blocks */
  1434.     ULONG           ulAllocNum;     /* internal allocation number */
  1435.     BOOL            fUnleakable;    /* TRUE if leak code should ignore block */
  1436.     #if defined(_WIN32) && defined(_X86_)
  1437.     FARPROC         pfnCallers[NCALLERS];
  1438.     #endif
  1439.     PBLKTAIL        pblktail;       /* pointer to block tail */
  1440. };
  1441.  
  1442. struct _BLKTAIL {
  1443.     PBLK            pblk;           /* pointer back to beginning of the block */
  1444. };
  1445.  
  1446. #define PblkToPv(pblk)          ((LPVOID)((PBLK)(pblk) + 1))
  1447. #define PvToPblk(pblk)          ((PBLK)(pv) - 1)
  1448. #define PblkClientSize(pblk)    ((ULONG)((char *)(pblk)->pblktail - (char *)PblkToPv(pblk)))
  1449. #define PblkAllocSize(pblk)     (PblkClientSize(pblk) + sizeof(BLK) + (pblk)->pdbgmem->cbTail)
  1450.  
  1451. #if defined(_WIN32) && defined(_X86_)
  1452. #define DBGMEM_EnterCriticalSection(pdbgmem)    \
  1453.         EnterCriticalSection(&(pdbgmem)->cs)
  1454. #define DBGMEM_LeaveCriticalSection(pdbgmem)    \
  1455.         LeaveCriticalSection(&(pdbgmem)->cs)
  1456. #else
  1457. #define DBGMEM_EnterCriticalSection(pdbgmem)
  1458. #define DBGMEM_LeaveCriticalSection(pdbgmem)
  1459. #endif
  1460.  
  1461. #define INITGUID
  1462. #include <initguid.h>
  1463.  
  1464. DEFINE_OLEGUID(DBGMEM_IID_IUnknown,     0x00000000L, 0, 0);
  1465. DEFINE_OLEGUID(DBGMEM_IID_IMalloc,      0x00000002L, 0, 0);
  1466. DEFINE_OLEGUID(DBGMEM_IID_IBaseMalloc,  0x000203FFL, 0, 0);
  1467.  
  1468. /* Forward Declarations ----------------------------------------------------- */
  1469.  
  1470. BOOL DBGMEM_ValidatePblk(PDBGMEM pdbgmem, PBLK pblk, char ** pszReason);
  1471. BOOL DBGMEM_ValidatePv(PDBGMEM pdbgmem, void * pv, char * pszFunc);
  1472. STDMETHODIMP_(void) DBGMEM_Free(PDBGMEM pdbgmem, void * pv);
  1473.  
  1474. /* Call Stack (_WIN32) ------------------------------------------------------- */
  1475.  
  1476. #if defined(_WIN32) && defined(_X86_)
  1477.  
  1478. #ifdef _WIN95
  1479. #define dwStackLimit    0x00400000      /*  4MB for Windows 95 */
  1480. #else
  1481. #define dwStackLimit    0x00010000      /*  64KB for NT */
  1482. #endif
  1483.  
  1484. void EXPORTDBG __cdecl GetCallStack(DWORD *pdwCaller, int cSkip, int cFind)
  1485. {
  1486.     DWORD * pdwStack;
  1487.     DWORD * pdwStackPrev = (DWORD *)0;
  1488.     DWORD   dwCaller;
  1489.  
  1490.     __asm mov pdwStack, ebp
  1491.  
  1492.     memset(pdwCaller, 0, cFind * sizeof(DWORD));
  1493.  
  1494.     while (cSkip + cFind > 0)
  1495.     {
  1496.         pdwStack = (DWORD *)*pdwStack;
  1497.  
  1498.         if (    pdwStack <= (DWORD *)dwStackLimit
  1499.             ||  pdwStackPrev >= pdwStack
  1500.             ||  IsBadReadPtr(pdwStack, 2 * sizeof(DWORD)))
  1501.             break;
  1502.  
  1503.         dwCaller = *(pdwStack + 1);
  1504.  
  1505.         if (dwCaller <= dwStackLimit)
  1506.             break;
  1507.         else if (cSkip > 0)
  1508.             cSkip -= 1;
  1509.         else
  1510.         {
  1511.             *pdwCaller++ = dwCaller;
  1512.             cFind -= 1;
  1513.  
  1514.             pdwStackPrev = pdwStack;
  1515.         }
  1516.     }
  1517. }
  1518.  
  1519. #endif
  1520.  
  1521. /* Virtual Memory Support (_WIN32) ------------------------------------------- */
  1522.  
  1523. #if defined(_WIN32) && (defined(_X86_) || defined(_PPC_) || defined(_MIPS_))
  1524.  
  1525. #define PAGE_SIZE       4096
  1526. #define PvToVMBase(pv)  ((void *)((ULONG)pv & 0xFFFF0000))
  1527.  
  1528. BOOL VMValidatePvEx(void *pv, ULONG cbCluster)
  1529. {
  1530.     void *  pvBase;
  1531.     BYTE *  pb;
  1532.  
  1533.     pvBase = PvToVMBase(pv);
  1534.  
  1535.     pb = (BYTE *)pvBase + sizeof(ULONG);
  1536.  
  1537.     while (pb < (BYTE *)pv) {
  1538.         if (*pb++ != 0xAD) {
  1539.             TrapSz1("VMValidatePvEx(pv=%08lX): Block leader has been overwritten", pv);
  1540.             return(FALSE);
  1541.         }
  1542.     }
  1543.  
  1544.     if (cbCluster != 1)
  1545.     {
  1546.         ULONG cb = *((ULONG *)pvBase);
  1547.         ULONG cbPad = 0;
  1548.  
  1549.         if (cb % cbCluster)
  1550.             cbPad = (cbCluster - (cb % cbCluster));
  1551.  
  1552.         if (cbPad)
  1553.         {
  1554.             BYTE *pbMac;
  1555.  
  1556.             pb = (BYTE *)pv + cb;
  1557.             pbMac = pb + cbPad;
  1558.  
  1559.             while (pb < pbMac)
  1560.             {
  1561.                 if (*pb++ != 0xBC)
  1562.                 {
  1563.                     TrapSz1("VMValidatePvEx(pv=%08lX): Block trailer has been "
  1564.                         "overwritten", pv);
  1565.                     return(FALSE);
  1566.                 }
  1567.             }
  1568.         }
  1569.     }
  1570.  
  1571.     return(TRUE);
  1572. }
  1573.  
  1574. void * EXPORTDBG __cdecl VMAlloc(ULONG cb)
  1575. {
  1576.     return VMAllocEx(cb, 1);
  1577. }
  1578.  
  1579. void * EXPORTDBG __cdecl VMAllocEx(ULONG cb, ULONG cbCluster)
  1580. {
  1581.     ULONG   cbAlloc;
  1582.     void *  pvR;
  1583.     void *  pvC;
  1584.     ULONG   cbPad   = 0;
  1585.  
  1586.     // a cluster size of 0 means don't use the virtual allocator.
  1587.  
  1588.     AssertSz(cbCluster != 0, "Cluster size is zero.");
  1589.  
  1590.     if (cb > 0x100000)
  1591.         return(0);
  1592.  
  1593.     if (cb % cbCluster)
  1594.         cbPad = (cbCluster - (cb % cbCluster));
  1595.  
  1596.     cbAlloc = sizeof(ULONG) + cb + cbPad + PAGE_SIZE - 1;
  1597.     cbAlloc -= cbAlloc % PAGE_SIZE;
  1598.     cbAlloc += PAGE_SIZE;
  1599.  
  1600.     pvR = VirtualAlloc(0, cbAlloc, MEM_RESERVE, PAGE_NOACCESS);
  1601.  
  1602.     if (pvR == 0)
  1603.         return(0);
  1604.  
  1605.     pvC = VirtualAlloc(pvR, cbAlloc - PAGE_SIZE, MEM_COMMIT, PAGE_READWRITE);
  1606.  
  1607.     if (pvC != pvR)
  1608.     {
  1609.         VirtualFree(pvR, 0, MEM_RELEASE);
  1610.         return(0);
  1611.     }
  1612.  
  1613.     *(ULONG *)pvC = cb;
  1614.  
  1615.     memset((BYTE *)pvC + sizeof(ULONG), 0xAD,
  1616.         (UINT) cbAlloc - cb - cbPad - sizeof(ULONG) - PAGE_SIZE);
  1617.  
  1618.     if (cbPad)
  1619.         memset((BYTE *)pvC + cbAlloc - PAGE_SIZE - cbPad, 0xBC,
  1620.             (UINT) cbPad);
  1621.  
  1622.     return((BYTE *)pvC + (cbAlloc - cb - cbPad - PAGE_SIZE));
  1623. }
  1624.  
  1625. void EXPORTDBG __cdecl VMFree(void *pv)
  1626. {
  1627.     VMFreeEx(pv, 1);
  1628. }
  1629.  
  1630. void EXPORTDBG __cdecl VMFreeEx(void *pv, ULONG cbCluster)
  1631. {
  1632.     VMValidatePvEx(pv, cbCluster);
  1633.  
  1634.     if (!VirtualFree(PvToVMBase(pv), 0, MEM_RELEASE))
  1635.         TrapSz2("VMFreeEx(pv=%08lX): VirtualFree failed (%08lX)",
  1636.             pv, GetLastError());
  1637. }
  1638.  
  1639. void * EXPORTDBG __cdecl VMRealloc(void *pv, ULONG cb)
  1640. {
  1641.     return VMReallocEx(pv, cb, 1);
  1642. }
  1643.  
  1644. void * EXPORTDBG __cdecl VMReallocEx(void *pv, ULONG cb, ULONG cbCluster)
  1645. {
  1646.     void *  pvNew = 0;
  1647.     ULONG   cbCopy;
  1648.  
  1649.     VMValidatePvEx(pv, cbCluster);
  1650.  
  1651.     cbCopy = *(ULONG *)PvToVMBase(pv);
  1652.     if (cbCopy > cb)
  1653.         cbCopy = cb;
  1654.  
  1655.     pvNew = VMAllocEx(cb, cbCluster);
  1656.  
  1657.     if (pvNew)
  1658.     {
  1659.         MemCopy(pvNew, pv, cbCopy);
  1660.         VMFreeEx(pv, cbCluster);
  1661.     }
  1662.  
  1663.     return(pvNew);
  1664. }
  1665.  
  1666. ULONG EXPORTDBG __cdecl VMGetSize(void *pv)
  1667. {
  1668.     return VMGetSizeEx(pv, 1);
  1669. }
  1670.  
  1671. ULONG EXPORTDBG __cdecl VMGetSizeEx(void *pv, ULONG cbCluster)
  1672. {
  1673.     return(*(ULONG *)PvToVMBase(pv));
  1674. }
  1675.  
  1676. #endif
  1677.  
  1678. /* Virtual Memory Support (WIN16) ------------------------------------------- */
  1679.  
  1680. #ifdef WIN16
  1681.  
  1682. #define PvToVMBase(pv)  ((void *)((ULONG)pv & 0xFFFF0000))
  1683.  
  1684. BOOL VMValidatePvEx(void *pv, ULONG cbCluster)
  1685. {
  1686.     void *  pvBase;
  1687.     BYTE *  pb;
  1688.  
  1689.     pvBase = PvToVMBase(pv);
  1690.  
  1691.     pb = (BYTE *)pvBase + sizeof(ULONG);
  1692.  
  1693.     while (pb < (BYTE *)pv) {
  1694.         if (*pb++ != 0xAD) {
  1695.             TrapSz1("VMValidatePvEx(pv=%08lX): Block leader has been overwritten", pv);
  1696.             return(FALSE);
  1697.         }
  1698.     }
  1699.  
  1700.     if (cbCluster != 1)
  1701.     {
  1702.         ULONG cb = *((ULONG *)pvBase);
  1703.         ULONG cbPad = 0;
  1704.  
  1705.         if (cb % cbCluster)
  1706.             cbPad = (cbCluster - (cb % cbCluster));
  1707.  
  1708.         if (cbPad)
  1709.         {
  1710.             BYTE *pbMac;
  1711.  
  1712.             pb = (BYTE *)pv + cb;
  1713.             pbMac = pb + cbPad;
  1714.  
  1715.             while (pb < pbMac)
  1716.             {
  1717.                 if (*pb++ != 0xBC)
  1718.                 {
  1719.                     TrapSz1("VMValidatePvEx(pv=%08lX): Block trailer has been "
  1720.                         "overwritten", pv);
  1721.                     return(FALSE);
  1722.                 }
  1723.             }
  1724.         }
  1725.     }
  1726.  
  1727.     return(TRUE);
  1728. }
  1729.  
  1730. BOOL VMValidatePv(void *pv)
  1731. {
  1732.     return VMValidatePvEx(pv, 1);
  1733. }
  1734.  
  1735. void * EXPORTDBG __cdecl VMAlloc(ULONG cb)
  1736. {
  1737.     return VMAllocEx(cb, 1);
  1738. }
  1739.  
  1740. void * EXPORTDBG __cdecl VMAllocEx(ULONG cb, ULONG cbCluster)
  1741. {
  1742.     HGLOBAL hGlobal;
  1743.     ULONG   cbAlloc;
  1744.     ULONG   cbAllocFromSys;
  1745.     void *  pvAlloc;
  1746.     ULONG   cbPad   = 0;
  1747.  
  1748.     if (cb > 0x10000 - sizeof(ULONG))
  1749.         return(0);
  1750.  
  1751.     if (cb % cbCluster)
  1752.         cbPad = (cbCluster - (cb % cbCluster));
  1753.  
  1754.     cbAlloc = sizeof(ULONG) + cb + cbPad;
  1755.  
  1756.     if (cbAlloc > 0x10000)
  1757.         return(0);
  1758.  
  1759. #ifdef SIMPLE_MAPI
  1760.     hGlobal = GlobalAlloc(GPTR | GMEM_SHARE, cbAlloc);
  1761. #else   
  1762.     hGlobal = GlobalAlloc(GPTR, cbAlloc);
  1763. #endif  
  1764.  
  1765.     if (hGlobal == 0)
  1766.         return(0);
  1767.  
  1768.     cbAllocFromSys = GlobalSize(hGlobal);
  1769.  
  1770.     Assert(cbAllocFromSys >= cbAlloc);
  1771.  
  1772.     cbAlloc = cbAllocFromSys;
  1773.  
  1774.     pvAlloc = GlobalLock(hGlobal);
  1775.  
  1776.     if (pvAlloc == 0) {
  1777.         GlobalFree(hGlobal);
  1778.         return(0);
  1779.     }
  1780.  
  1781.     Assert(((ULONG)pvAlloc & 0x0000FFFF) == 0);
  1782.  
  1783.     *(ULONG *)pvAlloc = cb;
  1784.  
  1785.     memset((BYTE *)pvAlloc + sizeof(ULONG), 0xAD,
  1786.         (size_t)(cbAlloc - cb - cbPad - sizeof(ULONG)));
  1787.  
  1788.     if (cbPad)
  1789.         memset((BYTE *)pvAlloc + cbAlloc - cbPad, 0xBC, (size_t) cbPad);
  1790.  
  1791.     return((BYTE *)pvAlloc + (cbAlloc - cb - cbPad));
  1792. }
  1793.  
  1794. void EXPORTDBG __cdecl VMFree(void *pv)
  1795. {
  1796.     VMFreeEx(pv, 1);
  1797. }
  1798.  
  1799. void EXPORTDBG __cdecl VMFreeEx(void *pv, ULONG cbCluster)
  1800. {
  1801.     if (VMValidatePvEx(pv, cbCluster))
  1802.     {
  1803.         HGLOBAL hGlobal;
  1804.         ULONG cb = *(ULONG *)PvToVMBase(pv);
  1805.  
  1806.         memset(pv, 0xFE, (size_t)cb);
  1807.  
  1808.         hGlobal = (HGLOBAL)((ULONG)pv >> 16);
  1809.         GlobalFree(hGlobal);
  1810.     }
  1811. }
  1812.  
  1813. void * EXPORTDBG __cdecl VMRealloc(void *pv, ULONG cb)
  1814. {
  1815.     return VMReallocEx(pv, cb, 1);
  1816. }
  1817.  
  1818. void * EXPORTDBG __cdecl VMReallocEx(void *pv, ULONG cb, ULONG cbCluster)
  1819. {
  1820.     void *  pvNew = 0;
  1821.     ULONG   cbCopy;
  1822.  
  1823.     if (VMValidatePvEx(pv, cbCluster)) {
  1824.         cbCopy = *(ULONG *)PvToVMBase(pv);
  1825.         if (cbCopy > cb)
  1826.             cbCopy = cb;
  1827.  
  1828.         pvNew = VMAllocEx(cb, cbCluster);
  1829.  
  1830.         if (pvNew) {
  1831.             MemCopy(pvNew, pv, (size_t)cbCopy);
  1832.             VMFreeEx(pv, cbCluster);
  1833.         }
  1834.     }
  1835.  
  1836.     return(pvNew);
  1837. }
  1838.  
  1839. ULONG EXPORTDBG __cdecl VMGetSize(void *pv)
  1840. {
  1841.     return VMGetSizeEx(pv, 1);
  1842. }
  1843.  
  1844. ULONG EXPORTDBG __cdecl VMGetSizeEx(void *pv, ULONG ulCluster)
  1845. {
  1846.     if (VMValidatePvEx(pv, ulCluster))
  1847.         return(*(ULONG *)PvToVMBase(pv));
  1848.  
  1849.     return(0);
  1850. }
  1851.  
  1852. #endif
  1853.  
  1854. /* Virtual Memory Support (Others) ------------------------------------------ */
  1855. /*
  1856.  *  The VM Allocators do not currently work on:
  1857.  *      ALPHA
  1858.  *      MAC
  1859.  */
  1860. #if defined(MAC) || defined(_ALPHA_)
  1861. #define VMAlloc(cb)             0
  1862. #define VMAllocEx(cb, ul)       0
  1863. #define VMRealloc(pv, cb)       0
  1864. #define VMReallocEx(pv, cb, ul) 0
  1865. #define VMFree(pv)
  1866. #define VMFreeEx(pv, ul)
  1867. #define VMGetSize(pv)           0
  1868. #define VMGetSizeEx(pv, ul)     0
  1869. #endif
  1870.  
  1871. /* PblkEnqueue / PblkDequeue ------------------------------------------------ */
  1872.  
  1873. void PblkEnqueue(PBLK pblk)
  1874. {
  1875.     pblk->pblkNext          = pblk->pdbgmem->pblkHead;
  1876.     pblk->pblkPrev          = 0;
  1877.     pblk->pdbgmem->pblkHead = pblk;
  1878.  
  1879.     if (pblk->pblkNext)
  1880.         pblk->pblkNext->pblkPrev = pblk;
  1881.  
  1882. }
  1883.  
  1884. void PblkDequeue(PBLK pblk)
  1885. {
  1886.     if (pblk->pblkNext)
  1887.         pblk->pblkNext->pblkPrev = pblk->pblkPrev;
  1888.  
  1889.     if (pblk->pblkPrev)
  1890.         pblk->pblkPrev->pblkNext = pblk->pblkNext;
  1891.     else
  1892.         pblk->pdbgmem->pblkHead  = pblk->pblkNext;
  1893. }
  1894.  
  1895. /* QueryInterface/AddRef/Release -------------------------------------------- */
  1896.  
  1897. STDMETHODIMP DBGMEM_QueryInterface(PDBGMEM pdbgmem, REFIID riid, LPVOID FAR* ppvObj)
  1898. {
  1899.     if (memcmp(riid, &DBGMEM_IID_IBaseMalloc, sizeof(IID)) == 0) {
  1900.         pdbgmem->pmalloc->lpVtbl->AddRef(pdbgmem->pmalloc);
  1901.         *ppvObj = pdbgmem->pmalloc;
  1902.         return(0);
  1903.     }
  1904.  
  1905.     if (memcmp(riid, &DBGMEM_IID_IMalloc, sizeof(IID)) == 0 ||
  1906.         memcmp(riid, &DBGMEM_IID_IUnknown, sizeof(IID)) == 0) {
  1907.         ++pdbgmem->cRef;
  1908.         *ppvObj = pdbgmem;
  1909.         return(0);
  1910.     }
  1911.  
  1912.     *ppvObj = NULL; /* OLE requires zeroing [out] parameter */
  1913.     return(ResultFromScode(E_NOINTERFACE));
  1914. }
  1915.  
  1916. STDMETHODIMP_(ULONG) DBGMEM_AddRef(PDBGMEM pdbgmem)
  1917. {
  1918.     ULONG cRef;
  1919.  
  1920.     DBGMEM_EnterCriticalSection(pdbgmem);
  1921.     cRef = ++pdbgmem->cRef;
  1922.     DBGMEM_LeaveCriticalSection(pdbgmem);
  1923.  
  1924.     return(cRef);
  1925. }
  1926.  
  1927. STDMETHODIMP_(ULONG) DBGMEM_Release(PDBGMEM pdbgmem)
  1928. {
  1929.     ULONG       cRef;
  1930.     LPMALLOC    pmalloc;
  1931.  
  1932.     DBGMEM_EnterCriticalSection(pdbgmem);
  1933.     cRef = --pdbgmem->cRef;
  1934.     DBGMEM_LeaveCriticalSection(pdbgmem);
  1935.  
  1936.     if (cRef == 0) {
  1937.         DBGMEM_CheckMemFn(pdbgmem, TRUE);
  1938.         pmalloc = pdbgmem->pmalloc;
  1939.         pdbgmem->lpVtbl = 0;
  1940.         #if defined(_WIN32) && defined(_X86_)
  1941.         DeleteCriticalSection(&pdbgmem->cs);
  1942.         #endif
  1943.         pmalloc->lpVtbl->Free(pmalloc, pdbgmem);
  1944.         pmalloc->lpVtbl->Release(pmalloc);
  1945.     }
  1946.  
  1947.     return(cRef);
  1948. }
  1949.  
  1950. /* IMalloc::Alloc ----------------------------------------------------------- */
  1951.  
  1952. STDMETHODIMP_(void FAR *) DBGMEM_Alloc(PDBGMEM pdbgmem, ULONG cb)
  1953. {
  1954.     PBLK    pblk;
  1955.     ULONG   cbAlloc;
  1956.     LPVOID  pvAlloc = 0;
  1957.     BYTE    bFill   = 0xFA;
  1958.  
  1959.     DBGMEM_EnterCriticalSection(pdbgmem);
  1960.  
  1961.     if (pdbgmem->fCheckOften)
  1962.         DBGMEM_CheckMemFn(pdbgmem, FALSE);
  1963.  
  1964.     cbAlloc = sizeof(BLK) + cb + pdbgmem->cbTail;
  1965.  
  1966.     if (pdbgmem->ulFailureAt != 0)
  1967.     {
  1968.         if (pdbgmem->ulFailureAt != pdbgmem->ulAllocAt)
  1969.             ++pdbgmem->ulAllocAt;
  1970.         else
  1971.             cbAlloc = 0;
  1972.     }
  1973.  
  1974.     if (cbAlloc < cb)
  1975.         pblk = 0;
  1976.     else if (pdbgmem->cbVirtual)
  1977.         pblk = VMAllocEx(cbAlloc, pdbgmem->cbVirtual);
  1978.     else
  1979.         pblk = (PBLK)pdbgmem->pmalloc->lpVtbl->Alloc(pdbgmem->pmalloc, cbAlloc);
  1980.  
  1981.     if (pblk) {
  1982.         pblk->pdbgmem       = pdbgmem;
  1983.         pblk->ulAllocNum    = ++pdbgmem->ulAllocNum;
  1984.         pblk->fUnleakable   = FALSE;
  1985.         pblk->pblktail      = (PBLKTAIL)((char *)pblk + sizeof(BLK) + cb);
  1986.  
  1987.         if (!pdbgmem->cbVirtual)
  1988.             *((PUABLK UNALIGNED * )
  1989.             &((struct _BLKTAIL UNALIGNED *) pblk->pblktail)->pblk) = pblk;
  1990.  
  1991.         PblkEnqueue(pblk);
  1992.  
  1993.         #if defined(_WIN32) && defined(_X86_)
  1994.         GetCallStack((DWORD *)pblk->pfnCallers, 0, NCALLERS);
  1995.         #endif
  1996.  
  1997.         if (pdbgmem->fCheckOften)
  1998.             DBGMEM_CheckMemFn(pdbgmem, FALSE);
  1999.  
  2000.         pvAlloc = PblkToPv(pblk);
  2001.  
  2002.         if (pdbgmem->fFillRandom)
  2003.             bFill = (BYTE)pblk->ulAllocNum;
  2004.  
  2005.         memset(pvAlloc, bFill, (size_t)cb);
  2006.  
  2007.         if (pdbgmem->cbExtra)
  2008.             memset(pblk->pblktail + 1, 0xAE, pdbgmem->cbExtra * sizeof(ULONG));
  2009.     }
  2010.  
  2011.     DBGMEM_LeaveCriticalSection(pdbgmem);
  2012.  
  2013.     return(pvAlloc);
  2014. }
  2015.  
  2016. /* IMalloc::Realloc --------------------------------------------------------- */
  2017.  
  2018. STDMETHODIMP_(void FAR *) DBGMEM_Realloc(PDBGMEM pdbgmem, void FAR* pv, ULONG cb)
  2019. {
  2020.     ULONG   cbAlloc;
  2021.     LPVOID  pvAlloc = 0;
  2022.     BYTE    bFill = 0xFA;
  2023.  
  2024.     DBGMEM_EnterCriticalSection(pdbgmem);
  2025.  
  2026.     if (pdbgmem->fCheckOften)
  2027.         DBGMEM_CheckMemFn(pdbgmem, FALSE);
  2028.  
  2029.     if (pv == 0) {
  2030.         TrapSz1("DBGMEM_Realloc(pv=NULL,cb=%ld): IMalloc::Realloc is being used allocate a new memory block.  Explicit use of IMalloc::Alloc is preferred.", cb);
  2031.         pvAlloc = DBGMEM_Alloc(pdbgmem, cb);
  2032.     } else if (cb == 0) {
  2033.         TrapSz1("DBGMEM_Realloc(pv=%08lX,cb=0): IMalloc::Realloc is being used to free a memory block.  Explicit use of IMalloc::Free is preferred.", pv);
  2034.         DBGMEM_Free(pdbgmem, pv);
  2035.         pvAlloc = 0;
  2036.     } else if (DBGMEM_ValidatePv(pdbgmem, pv, "DBGMEM_Realloc")) {
  2037.         PBLK    pblk    = PvToPblk(pv);
  2038.         ULONG   cbOld   = PblkClientSize(pblk);
  2039.         PBLK    pblkNew;
  2040.  
  2041.         PblkDequeue(pblk);
  2042.  
  2043.         cbAlloc = sizeof(BLK) + cb + pdbgmem->cbTail;
  2044.  
  2045.         if (pdbgmem->ulFailureAt != 0)
  2046.         {
  2047.             if (pdbgmem->ulFailureAt != pdbgmem->ulAllocAt)
  2048.                 ++pdbgmem->ulAllocAt;
  2049.             else
  2050.                 cbAlloc = 0;
  2051.         }
  2052.  
  2053.         if (cbAlloc < cb)
  2054.             pblkNew = 0;
  2055.         else if (pdbgmem->cbVirtual)
  2056.             pblkNew = (PBLK)VMReallocEx(pblk, cbAlloc, pdbgmem->cbVirtual);
  2057.         else
  2058.             pblkNew = (PBLK)pdbgmem->pmalloc->lpVtbl->Realloc(pdbgmem->pmalloc, pblk, cbAlloc);
  2059.  
  2060.         if (pblkNew == 0) {
  2061.             PblkEnqueue(pblk);
  2062.             pvAlloc = 0;
  2063.         } else {
  2064.             pblkNew->pblktail = (PBLKTAIL)((char *)pblkNew + sizeof(BLK) + cb);
  2065.  
  2066.             if (!pdbgmem->cbVirtual)
  2067.                 *((PUABLK UNALIGNED * )
  2068.                 &((struct _BLKTAIL UNALIGNED *) pblkNew->pblktail)->pblk) = pblkNew;
  2069.  
  2070.             PblkEnqueue(pblkNew);
  2071.  
  2072.             pvAlloc = PblkToPv(pblkNew);
  2073.  
  2074.             if (pdbgmem->fFillRandom)
  2075.                 bFill = (BYTE)pblkNew->ulAllocNum;
  2076.  
  2077.             if (cb > cbOld)
  2078.                 memset((char *)pvAlloc + cbOld, bFill, (size_t)(cb - cbOld));
  2079.  
  2080.             if (pdbgmem->cbExtra)
  2081.                 memset(pblkNew->pblktail + 1, 0xAE, pdbgmem->cbExtra * sizeof(ULONG));
  2082.         }
  2083.     }
  2084.  
  2085.     DBGMEM_LeaveCriticalSection(pdbgmem);
  2086.  
  2087.     return(pvAlloc);
  2088. }
  2089.  
  2090. /* IMalloc::Free ------------------------------------------------------------ */
  2091.  
  2092. STDMETHODIMP_(void) DBGMEM_Free(PDBGMEM pdbgmem, void FAR * pv)
  2093. {
  2094.     DBGMEM_EnterCriticalSection(pdbgmem);
  2095.  
  2096.     if (pdbgmem->fCheckOften)
  2097.         DBGMEM_CheckMemFn(pdbgmem, FALSE);
  2098.  
  2099.     if (pv && DBGMEM_ValidatePv(pdbgmem, pv, "DBGMEM_Free")) {
  2100.         PBLK pblk = PvToPblk(pv);
  2101.  
  2102.         PblkDequeue(pblk);
  2103.  
  2104.         memset(pblk, 0xDC, (size_t)PblkAllocSize(pblk));
  2105.  
  2106.         if (pdbgmem->cbVirtual)
  2107.             VMFreeEx(pblk, pdbgmem->cbVirtual);
  2108.         else
  2109.             pdbgmem->pmalloc->lpVtbl->Free(pdbgmem->pmalloc, pblk);
  2110.     }
  2111.  
  2112.     DBGMEM_LeaveCriticalSection(pdbgmem);
  2113. }
  2114.  
  2115. /* IMalloc::GetSize --------------------------------------------------------- */
  2116.  
  2117. STDMETHODIMP_(ULONG) DBGMEM_GetSize(PDBGMEM pdbgmem, void FAR * pv)
  2118. {
  2119.     ULONG ulResult = (ULONG)(-1);
  2120.  
  2121.     DBGMEM_EnterCriticalSection(pdbgmem);
  2122.  
  2123.     if (pv == 0)
  2124.         TrapSz("Although technically not an error, I bet you didn't really want to pass a NULL pointer to IMalloc::GetSize, did you?  I hope you can deal with a size of -1, because that's the offical answer.  Good luck.");
  2125.     else if (DBGMEM_ValidatePv(pdbgmem, pv, "DBGMEM_GetSize"))
  2126.         ulResult = PblkClientSize(PvToPblk(pv));
  2127.  
  2128.     DBGMEM_LeaveCriticalSection(pdbgmem);
  2129.  
  2130.     return(ulResult);
  2131. }
  2132.  
  2133. /* IMalloc::DidAlloc -------------------------------------------------------- */
  2134.  
  2135. STDMETHODIMP_(int) DBGMEM_DidAlloc(PDBGMEM pdbgmem, void FAR * pv)
  2136. {
  2137.     PBLK    pblk;
  2138.     char *  pszReason;
  2139.     int     iResult = 0;
  2140.  
  2141.     DBGMEM_EnterCriticalSection(pdbgmem);
  2142.  
  2143.     for (pblk = pdbgmem->pblkHead; pblk; pblk = pblk->pblkNext)
  2144.     {
  2145.         AssertSz2(DBGMEM_ValidatePblk(pdbgmem,pblk,&pszReason)==TRUE,
  2146.                  "Block header (pblk=%08lX) is invalid\n%s",
  2147.                  pblk, pszReason);
  2148.         if (PblkToPv(pblk) == pv) {
  2149.             iResult = 1;
  2150.             break;
  2151.         }
  2152.     }
  2153.  
  2154.     DBGMEM_LeaveCriticalSection(pdbgmem);
  2155.  
  2156.     return(iResult);
  2157. }
  2158.  
  2159. /* IMalloc::HeapMinimize ---------------------------------------------------- */
  2160.  
  2161. STDMETHODIMP_(void) DBGMEM_HeapMinimize(PDBGMEM pdbgmem)
  2162. {
  2163.     pdbgmem->pmalloc->lpVtbl->HeapMinimize(pdbgmem->pmalloc);
  2164. }
  2165.  
  2166. /* DBGMEM_ValidatePblk ------------------------------------------------------ */
  2167.  
  2168. BOOL DBGMEM_ValidatePblk(PDBGMEM pdbgmem, PBLK pblk, char ** pszReason)
  2169. {
  2170.     #if defined(WIN16) || (defined(_WIN32) && defined(_X86_))
  2171.     if (IsBadWritePtr(pblk, sizeof(BLK))) {
  2172.         *pszReason = "Block header cannot be written to";
  2173.         goto err;
  2174.     }
  2175.     #endif
  2176.  
  2177.     if (pblk->pdbgmem != pdbgmem) {
  2178.         *pszReason = "Block header does not have correct pointer back to allocator";
  2179.         goto err;
  2180.     }
  2181.  
  2182.     if (pblk->pblkNext) {
  2183.         #if defined(WIN16) || (defined(_WIN32) && defined(_X86_))
  2184.         if (IsBadWritePtr(pblk->pblkNext, sizeof(BLK))) {
  2185.             *pszReason = "Block header has invalid next link pointer";
  2186.             goto err;
  2187.         }
  2188.         #endif
  2189.  
  2190.         if (pblk->pblkNext->pblkPrev != pblk) {
  2191.             *pszReason = "Block header points to a next block which doesn't point back to it";
  2192.             goto err;
  2193.         }
  2194.     }
  2195.  
  2196.     if (pblk->pblkPrev) {
  2197.         #if defined(WIN16) || (defined(_WIN32) && defined(_X86_))
  2198.         if (IsBadWritePtr(pblk->pblkPrev, sizeof(BLK))) {
  2199.             *pszReason = "Block header has invalid prev link pointer";
  2200.             goto err;
  2201.         }
  2202.         #endif
  2203.  
  2204.         if (pblk->pblkPrev->pblkNext != pblk) {
  2205.             *pszReason = "Block header points to a prev block which doesn't point back to it";
  2206.             goto err;
  2207.         }
  2208.     } else if (pdbgmem->pblkHead != pblk) {
  2209.         *pszReason = "Block header has a zero prev link but the allocator doesn't believe it is the first block";
  2210.         goto err;
  2211.     }
  2212.  
  2213.     if (pblk->ulAllocNum > pdbgmem->ulAllocNum) {
  2214.         *pszReason = "Block header has an invalid internal allocation number";
  2215.         goto err;
  2216.     }
  2217.  
  2218.     if (!pdbgmem->cbVirtual) {
  2219.         #if defined(WIN16) || (defined(_WIN32) && defined(_X86_))
  2220.         if (IsBadWritePtr(pblk->pblktail, pdbgmem->cbTail)) {
  2221.             *pszReason = "Block header has invalid pblktail pointer";
  2222.             goto err;
  2223.         }
  2224.         #endif
  2225.  
  2226.         if (*((PUABLK UNALIGNED * )
  2227.             &((struct _BLKTAIL UNALIGNED *) pblk->pblktail)->pblk) != pblk) {
  2228.             *pszReason = "Block trailer does not point back to the block header";
  2229.             goto err;
  2230.         }
  2231.     }
  2232.  
  2233.     if (pdbgmem->cbExtra) {
  2234.         ULONG UNALIGNED * pul = (ULONG UNALIGNED *)(pblk->pblktail + 1);
  2235.         int n = pdbgmem->cbExtra;
  2236.         for (; --n >= 0; ++pul)
  2237.             if (*pul != 0xAEAEAEAE) {
  2238.                 *pszReason = "Block trailer spiddle-zone has been overwritten";
  2239.                 goto err;
  2240.             }
  2241.     }
  2242.  
  2243.     return(TRUE);
  2244.  
  2245. err:
  2246.     return(FALSE);
  2247. }
  2248.  
  2249. /* DBGMEM_ValidatePv -------------------------------------------------------- */
  2250.  
  2251. BOOL DBGMEM_ValidatePv(PDBGMEM pdbgmem, void * pv, char * pszFunc)
  2252. {
  2253.     char *  pszReason;
  2254.  
  2255.     if (DBGMEM_DidAlloc(pdbgmem, pv) == 0) {
  2256.         TrapSz3("DBGMEM_ValidatePv(subsys=%s,pv=%08lX) [via %s]\nDetected a memory block which was not allocated by this allocator",
  2257.             pdbgmem->szSubsys, pv, pszFunc);
  2258.         return(FALSE);
  2259.     }
  2260.  
  2261.     if (DBGMEM_ValidatePblk(pdbgmem,PvToPblk(pv),&pszReason))
  2262.         return(TRUE);
  2263.  
  2264.     TrapSz4("DBGMEM_ValidatePv(%s,pv=%08lX) [via %s]\n%s",
  2265.         pdbgmem->szSubsys, pv, pszFunc, pszReason);
  2266.  
  2267.     return(FALSE);
  2268. }
  2269.  
  2270. /* DBGMEM_ReportLeak -------------------------------------------------------- */
  2271.  
  2272. #if defined(_WIN32) && defined(_X86_)
  2273. void EXPORTDBG __cdecl DBGMEM_LeakHook(FARPROC pfn)
  2274. {
  2275.     /* Dummy function so that you can set a breakpoint with command   */
  2276.     /* "ln ecx;g", in order to get the debugger to print out the name */
  2277.     /* of the function which allocated the leaked memory block        */
  2278. }
  2279. #endif
  2280.  
  2281. void DBGMEM_ReportLeak(PDBGMEM pdbgmem, PBLK pblk)
  2282. {
  2283.     int i = 0;
  2284.  
  2285.     DebugTrace("%s Memory Leak: @%08lX, allocation #%ld, size %ld\n",
  2286.         pdbgmem->szSubsys, PblkToPv(pblk), pblk->ulAllocNum, PblkClientSize(pblk));
  2287.  
  2288.     #if defined(_WIN32) && defined(_X86_)
  2289.     for (i = 0; i < NCALLERS && pblk->pfnCallers[i] != 0; i++) {
  2290.         DebugTrace("[%d] %08lX ", i, pblk->pfnCallers[i]);
  2291.         DBGMEM_LeakHook(pblk->pfnCallers[i]);
  2292.     }
  2293.     DebugTrace("\n");
  2294.     #endif
  2295. }
  2296.  
  2297. /* DBGMEM_NoLeakDetectFn ---------------------------------------------------- */
  2298.  
  2299. void EXPORTDBG __cdecl DBGMEM_NoLeakDetectFn(void * pmalloc, void *pv)
  2300. {
  2301.     PDBGMEM pdbgmem = (PDBGMEM)pmalloc;
  2302.  
  2303.     DBGMEM_EnterCriticalSection(pdbgmem);
  2304.  
  2305.     if (pv == 0)
  2306.         pdbgmem->fUnleakable = TRUE;
  2307.     else if (DBGMEM_ValidatePv(pdbgmem, pv, "DBGMEM_NoLeakDetectFn"))
  2308.         PvToPblk(pv)->fUnleakable = TRUE;
  2309.  
  2310.     DBGMEM_LeaveCriticalSection(pdbgmem);
  2311. }
  2312.  
  2313. /* DBGMEM_SetFailureAtFn ---------------------------------------------------- */
  2314.  
  2315. void EXPORTDBG __cdecl DBGMEM_SetFailureAtFn(void * pmalloc, ULONG ulFailureAt)
  2316. {
  2317.     PDBGMEM pdbgmem = (PDBGMEM)pmalloc;
  2318.  
  2319.     DBGMEM_EnterCriticalSection(pdbgmem);
  2320.  
  2321.     pdbgmem->ulFailureAt = ulFailureAt;
  2322.  
  2323.     DBGMEM_LeaveCriticalSection(pdbgmem);
  2324. }
  2325.  
  2326. /* DBGMEM_CheckMemFn -------------------------------------------------------- */
  2327.  
  2328. void EXPORTDBG __cdecl DBGMEM_CheckMemFn(void * pmalloc, BOOL fReportOrphans)
  2329. {
  2330.     PDBGMEM pdbgmem = (PDBGMEM)pmalloc;
  2331.     PBLK    pblk;
  2332.     int     cLeaks = 0;
  2333.  
  2334.     DBGMEM_EnterCriticalSection(pdbgmem);
  2335.  
  2336.     for (pblk = pdbgmem->pblkHead; pblk; pblk = pblk->pblkNext) {
  2337.         if (!DBGMEM_ValidatePv(pdbgmem, PblkToPv(pblk), "DBGMEM_CheckMemFn"))
  2338.             break;
  2339.  
  2340.         if (fReportOrphans && !pdbgmem->fUnleakable && !pblk->fUnleakable) {
  2341.             DBGMEM_ReportLeak(pdbgmem, pblk);
  2342.             cLeaks += 1;
  2343.         }
  2344.     }
  2345.  
  2346.     #if defined(WIN16) || (defined(_WIN32) && defined(_X86_))
  2347.     if (fAssertLeaks == -1)
  2348.     {
  2349.         fAssertLeaks = GetPrivateProfileIntA(szSectionDebug, szKeyAssertLeaks,
  2350.             0, szDebugIni);
  2351.     }
  2352.     #endif
  2353.  
  2354.     if (cLeaks > 0)
  2355.     {
  2356.         #if defined(WIN16) || (defined(_WIN32) && defined(_X86_))
  2357.         if (fAssertLeaks)
  2358.         {
  2359.             TrapSz3("DBGMEM detected %d memory leak%s in subsystem %s",
  2360.                 cLeaks, cLeaks == 1 ? "" : "s", pdbgmem->szSubsys);
  2361.         }
  2362.         else
  2363.         {
  2364.             TraceSz3("DBGMEM detected %d memory leak%s in subsystem %s",
  2365.                 cLeaks, cLeaks == 1 ? "" : "s", pdbgmem->szSubsys);
  2366.         }
  2367.         #else
  2368.         TraceSz3("DBGMEM detected %d memory leak%s in subsystem %s",
  2369.             cLeaks, cLeaks == 1 ? "" : "s", pdbgmem->szSubsys);
  2370.         #endif
  2371.     }
  2372.  
  2373.     DBGMEM_LeaveCriticalSection(pdbgmem);
  2374. }
  2375.  
  2376. /* vtblDBGMEM --------------------------------------------------------------- */
  2377.  
  2378. DBGMEM_Vtbl BASED_DEBUG vtblDBGMEM =
  2379. {
  2380.     VTABLE_FILL
  2381.     DBGMEM_QueryInterface,
  2382.     DBGMEM_AddRef,
  2383.     DBGMEM_Release,
  2384.     DBGMEM_Alloc,
  2385.     DBGMEM_Realloc,
  2386.     DBGMEM_Free,
  2387.     DBGMEM_GetSize,
  2388.     DBGMEM_DidAlloc,
  2389.     DBGMEM_HeapMinimize
  2390. };
  2391.  
  2392. /* DBGMEM_EncapsulateFn ----------------------------------------------------- */
  2393.  
  2394. void * EXPORTDBG __cdecl DBGMEM_EncapsulateFn(void * pvmalloc, char *pszSubsys, BOOL fCheckOften)
  2395. {
  2396.     LPMALLOC    pmalloc = (LPMALLOC)pvmalloc;
  2397.     PDBGMEM     pdbgmem;
  2398.     LPMALLOC    pmallocBase;
  2399.     ULONG       cbVirtual = 0;
  2400.     BOOL        fFillRandom = FALSE;
  2401.     HRESULT     hr;
  2402.  
  2403.     hr = pmalloc->lpVtbl->QueryInterface(pmalloc, &DBGMEM_IID_IBaseMalloc, &pmallocBase);
  2404.     if (hr) {
  2405.         pmallocBase = pmalloc;
  2406.         pmallocBase->lpVtbl->AddRef(pmallocBase);
  2407.     }
  2408.  
  2409.     pdbgmem = (PDBGMEM)pmallocBase->lpVtbl->Alloc(pmallocBase, sizeof(DBGMEM));
  2410.  
  2411.     if (pdbgmem == 0) {
  2412.         TrapSz("DBGMEM: Failed trying to allocate memory for the first time!\n");
  2413.         return(pmallocBase);
  2414.     }
  2415.  
  2416.     #if defined(WIN16) || (defined(_WIN32) && defined(_X86_))
  2417.     cbVirtual = GetPrivateProfileIntA(szSectionDebug, szKeyUseVirtual, 0,
  2418.         szDebugIni);
  2419.  
  2420.     if (cbVirtual != 0 && cbVirtual != 1 && cbVirtual != 4)
  2421.         cbVirtual = 1;
  2422.  
  2423.     if (cbVirtual)
  2424.         DebugTrace("DBGMEM: Subsystem '%s' using virtual memory allocator -"
  2425.             " align %d.\n", pszSubsys, cbVirtual);
  2426.  
  2427.     if (!fCheckOften)
  2428.         fCheckOften = GetPrivateProfileIntA(szSectionDebug, szKeyCheckOften, 0,
  2429.             szDebugIni);
  2430.  
  2431.     fFillRandom = GetPrivateProfileIntA(szSectionDebug, szKeyFillRandom, 0,
  2432.         szDebugIni);
  2433.  
  2434.     #endif
  2435.  
  2436.     memset(pdbgmem, 0, sizeof(DBGMEM));
  2437.  
  2438.     pdbgmem->lpVtbl         = &vtblDBGMEM;
  2439.     pdbgmem->cRef           = 1;
  2440.     pdbgmem->pmalloc        = pmallocBase;
  2441.     pdbgmem->fCheckOften    = fCheckOften;
  2442.     pdbgmem->fUnleakable    = FALSE;
  2443.     pdbgmem->cbVirtual      = cbVirtual;
  2444.     pdbgmem->fFillRandom    = fFillRandom;
  2445.     pdbgmem->cbExtra        = 0;
  2446.     pdbgmem->ulAllocAt      = 1L;
  2447.     pdbgmem->ulFailureAt    = 0L;
  2448.  
  2449.     if (pdbgmem->cbVirtual)
  2450.         pdbgmem->cbTail     = 0;
  2451.     else
  2452.         pdbgmem->cbTail     = sizeof(BLKTAIL) + pdbgmem->cbExtra * sizeof(ULONG);
  2453.  
  2454.     lstrcpyn(pdbgmem->szSubsys, pszSubsys, sizeof(pdbgmem->szSubsys));
  2455.  
  2456.     #if defined(_WIN32) && defined(_X86_)
  2457.     InitializeCriticalSection(&pdbgmem->cs);
  2458.     #endif
  2459.  
  2460.     return(pdbgmem);
  2461. }
  2462.  
  2463. /* DBGMEM_ShutdownFn -------------------------------------------------------- */
  2464.  
  2465. void EXPORTDBG __cdecl DBGMEM_ShutdownFn(void *pvmalloc)
  2466. {
  2467.     LPMALLOC    pmalloc = (LPMALLOC)pvmalloc;
  2468.     PDBGMEM     pdbgmem = (PDBGMEM)pvmalloc;
  2469.     LPMALLOC    pmallocBase;
  2470.     HRESULT     hr;
  2471.  
  2472.     hr = pmalloc->lpVtbl->QueryInterface(pmalloc, &DBGMEM_IID_IBaseMalloc, &pmallocBase);
  2473.     if (hr == 0) {
  2474.         pmallocBase->lpVtbl->Release(pmallocBase);
  2475.         if (pdbgmem->cRef != 1) {
  2476.             TrapSz2("DBGMEM_Shutdown: Expected a cRef of 1; instead have %ld for %s",
  2477.                 pdbgmem->cRef, pdbgmem->szSubsys);
  2478.             pdbgmem->cRef = 1;
  2479.         }
  2480.     }
  2481.  
  2482.     pmalloc->lpVtbl->Release(pmalloc);
  2483. }
  2484.  
  2485. /* -------------------------------------------------------------------------- */
  2486.  
  2487. #endif
  2488.