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 / docfile.ms / mspinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-11  |  6.9 KB  |  264 lines

  1. /*
  2.  *  M S P I N I T . C
  3.  *
  4.  *  Initialize the MAPI Sample Message Store Provider.
  5.  *
  6.  *  Copyright 1992-1995 Microsoft Corporation.  All Rights Reserved.
  7.  */
  8.  
  9. #include "msp.h"
  10.  
  11. CHAR szFolderTemplate[]         = "*.fld";
  12. CHAR szMessageTemplate[]        = "*.msg";
  13. CHAR szPropertyFileName[]       = "folder.prp";
  14. CHAR szHierarchyFileName[]      = "hierarch.tbl";
  15. CHAR szContentsFileName[]       = "contents.tbl";
  16. CHAR szOutgoingFileName[]       = "outgoing.tbl";
  17.  
  18. #define MSP_CheckParameters(pobj, intf, method, arglist)        \
  19.         OBJ_CheckParameters(pobj, intf, method, sizeof(MSP), &vtblMSP, arglist)
  20.  
  21. MSP_Vtbl vtblMSP =
  22. {
  23.     (MSP_QueryInterface_METHOD *)   OBJ_QueryInterface,
  24.     (MSP_AddRef_METHOD *)           OBJ_AddRef,
  25.     MSP_Release,
  26.     MSP_Shutdown,
  27.     MSP_Logon,
  28.     MSP_SpoolerLogon,
  29.     MSP_CompareStoreIDs
  30. };
  31.  
  32. /*
  33.  *  Exported functions
  34.  */
  35.  
  36. /*
  37.  *  MSProviderInit
  38.  *
  39.  *  Purpose:
  40.  *      Message Store Provider initialization and version handshake
  41.  *      with MAPI.  Called once for each MAPI Session that uses this
  42.  *      store provider DLL on this process.  Passes back an init
  43.  *      object (LPMSPROVIDER) used for further access to this provider.
  44.  *
  45.  *  Arguments:
  46.  *      ulFlags         Reserved for future use.  Ignored.
  47.  *      ulMAPIVersion   Version of Message Store SPI used by MAPI.
  48.  *      lpulMDBVersion  [out] Version of SPI supported by the provider.
  49.  *      ppmsp   [out] MS Provider object for further access.
  50.  *
  51.  *  Returns:
  52.  *      HRESULT
  53.  *
  54.  *  Side effects:
  55.  *      None.
  56.  *
  57.  *  Errors:
  58.  *      MAPI_E_VERSION              Require a higher version of MAPI
  59.  *      MAPI_E_NOT_ENOUGH_MEMORY    Insufficient memory
  60.  *      Any errors from ScInitMSInstance()
  61.  */
  62. STDINITMETHODIMP
  63. MSProviderInit(HINSTANCE hInstance, LPMALLOC pmalloc,
  64.     LPALLOCATEBUFFER pfnAllocBuf, LPALLOCATEMORE pfnAllocMore,
  65.     LPFREEBUFFER pfnFreeBuf, ULONG ulFlags, ULONG ulMAPIVersion,
  66.     ULONG * pulMDBVersion, LPMSPROVIDER * ppmsp)
  67. {
  68.     SCODE sc = S_OK;
  69.     PMSP pmsp = NULL;
  70.  
  71.     AssertSz(pmalloc, "Bad pmalloc");
  72.     AssertSz(pfnAllocBuf, "Bad pfnAllocBuf");
  73.     AssertSz(pfnAllocMore, "Bad pfnAllocMore");
  74.     AssertSz(pfnFreeBuf, "Bad pfnFreeBuf");
  75.     NFAssertSz(!ulFlags, "Unknown flags, bug in MAPI DLL");
  76.     AssertSz(pulMDBVersion, "Bad pulMDBVersion");
  77.     AssertSz(ppmsp, "Bad ppmsp");
  78.  
  79.     /* This provider requires MAPI to be at least the version
  80.      * of the SPI defined at the time this provider was compiled.
  81.      */
  82.     if (ulMAPIVersion < CURRENT_SPI_VERSION)
  83.     {
  84.         sc = MAPI_E_VERSION;
  85.         goto exit;
  86.     }
  87.  
  88.     /* Initialize the per-instance global data */
  89.     sc = ScInitMSInstance(pmalloc);
  90.     if (sc != S_OK)
  91.         goto exit;
  92.  
  93.     /* Allocate and initialize the MSPROVIDER object. */
  94.     sc = ScAllocZ(sizeof(MSP), (PPV) &pmsp);
  95.     if (sc != S_OK)
  96.     {
  97.         DeinitMSInstance();
  98.         goto exit;
  99.     }
  100.  
  101.     OBJ_Initialize(pmsp, &vtblMSP, OT_MSPROVIDER, 0, &pmsp->cs);
  102.  
  103.     pmsp->hInst = hInstance;
  104.     pmsp->lmr.lpAllocBuf = pfnAllocBuf;
  105.     pmsp->lmr.lpAllocMore = pfnAllocMore;
  106.     pmsp->lmr.lpFreeBuf = pfnFreeBuf;
  107.  
  108.     InitializeCriticalSection(&pmsp->cs);
  109.  
  110.     /* Pass back [out] parameters. */
  111.     *pulMDBVersion = CURRENT_SPI_VERSION;
  112.     *ppmsp = (LPMSPROVIDER) pmsp;
  113.  
  114. exit:
  115.     DebugTraceSc(MSProviderInit, sc);
  116.     return ResultFromScode(ScCheckSc(sc, IMSProvider_Init));
  117. }
  118.  
  119. /*
  120.  -  MSP_Release
  121.  -
  122.  */
  123.  
  124. STDMETHODIMP_(ULONG) MSP_Release(PMSP pmsp)
  125. {
  126.     LONG cRef;
  127.  
  128.     MSP_EnterCriticalSection(pmsp);
  129.  
  130.     cRef = --pmsp->cRef;
  131.  
  132.     AssertSz2(cRef >= 0, "MSP_Release(pmsp=%08lX): Bogus cRef (%08lX)",
  133.         pmsp, cRef);
  134.  
  135.     AssertSz(cRef > 0 || pmsp->pobjHead == NULL, "There are still valid logons");
  136.  
  137.     MSP_LeaveCriticalSection(pmsp);
  138.  
  139.     if (cRef == 0)
  140.     {
  141.         DeleteCriticalSection(&pmsp->cs);
  142.         FreeNull(pmsp);
  143.         DeinitMSInstance();
  144.     }
  145.  
  146.     return (cRef);
  147. }
  148.  
  149. /*
  150.  *  MSP_Shutdown
  151.  *
  152.  *  Purpose:
  153.  *      Allow MAPI to specify flags related to the Release().
  154.  *
  155.  *  Arguments:
  156.  *      pulFlags        Reserved for future use.  Ignored.
  157.  *
  158.  *  Returns:
  159.  *      HRESULT
  160.  *
  161.  *  Side effects:
  162.  *      None.
  163.  *
  164.  *  Errors:
  165.  *      None.
  166.  */
  167. STDMETHODIMP
  168. MSP_Shutdown(PMSP pmsp, ULONG * pulFlags)
  169. {
  170.     MSP_CheckParameters(
  171.             pmsp, 
  172.             IMSProvider, 
  173.             Shutdown,
  174.             (pmsp,
  175.             pulFlags));
  176.  
  177.     MSP_EnterCriticalSection(pmsp);
  178.  
  179.     /* MAPI says it will never call Release with valid logons, */
  180.     /* let's make sure.                                        */
  181.  
  182.     AssertSz(pmsp->pobjHead == NULL, "There are still valid logons");
  183.  
  184. #ifdef DEBUG
  185.     pmsp->fInvalid = TRUE;
  186. #endif
  187.  
  188.     MSP_LeaveCriticalSection(pmsp);
  189.  
  190.     return 0;
  191. }
  192.  
  193. /*
  194.  *  MSP_CompareStoreIDs
  195.  *
  196.  *  Purpose:
  197.  *      Compare the EntryIDs of two message stores.
  198.  *
  199.  *  Arguments:
  200.  *      pmsp                MSPROVIDER object returned by MSProviderInit.
  201.  *      lcbEntryID1         Size of first EntryID.
  202.  *      lpEntryID1          First EntryID to compare.
  203.  *      lcbEntryID2         Size of second EntryID.
  204.  *      lpEntryID2          Second EntryID to compare.
  205.  *      ulFlags             Flags.  Reserved for future use.
  206.  *      pulResult           Address in which to place the result of
  207.  *                          the comparison (TRUE or FALSE).
  208.  *
  209.  *  Returns:
  210.  *      HRESULT
  211.  *
  212.  *  Side effects:
  213.  *      None.
  214.  *
  215.  *  Errors:
  216.  *      None.
  217.  */
  218. STDMETHODIMP
  219. MSP_CompareStoreIDs(PMSP pmsp, ULONG lcbEntryID1, LPENTRYID lpEntryID1,
  220.     ULONG lcbEntryID2, LPENTRYID lpEntryID2, ULONG ulFlags,
  221.     ULONG *pulResult)
  222. {
  223.     PEID peid1 = (PEID) lpEntryID1;
  224.     PEID peid2 = (PEID) lpEntryID2;
  225.  
  226.     MSP_CheckParameters(
  227.             pmsp, 
  228.             IMSProvider,
  229.             CompareStoreIDs,
  230.             (pmsp,
  231.             lcbEntryID1, 
  232.             lpEntryID1,
  233.             lcbEntryID2, 
  234.             lpEntryID2, 
  235.             ulFlags,
  236.             pulResult));
  237.  
  238.     AssertSz(!pmsp->fInvalid,
  239.         "This MSProvider has already been shutdown");
  240.  
  241.     /* The Sample Store's method of deriving and comparing   */
  242.     /* store EntryIDs has the quirky behavior that if any    */
  243.     /* OTHER store's EntryIDs are binary-comparable, this    */
  244.     /* function will return a successful match, without ever */
  245.     /* knowing that the EntryIDs were not Sample Store       */
  246.     /* EntryIDs.  A subsequent logon would, of course, fail. */
  247.  
  248.     /* Case-insensitive string compare on part, binary on rest. */
  249.     if (    lcbEntryID1 >= offsetof(EID, szPath) + sizeof(TCHAR)
  250.         &&  lcbEntryID1 == lcbEntryID2
  251.         &&  memcmp(peid1, peid2, offsetof(EID, szPath)) == 0
  252.         &&  peid1->bVersion == SMPMS_VERSION
  253.         &&  lstrcmpi(peid1->szPath, peid2->szPath) == 0)
  254.     {
  255.         *pulResult = TRUE;
  256.     }
  257.     else
  258.     {
  259.         *pulResult = FALSE;
  260.     }
  261.  
  262.     return hrSuccess;
  263. }
  264.