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 / flatfile.ab / tid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-11  |  9.4 KB  |  386 lines

  1. /***********************************************************************
  2.  *
  3.  *  TID.C
  4.  *
  5.  *  Sample Address Book Template ID  object
  6.  *  This file contains the code for implementing a template ID object
  7.  *  that is associated with entries from this provider.  In particular
  8.  *  the MailUser object generated in ABUSER.C needs this TID object when
  9.  *  it's been copied into another provider (i.e. the PAB).
  10.  *
  11.  *  Copyright 1992-1995 Microsoft Corporation.  All Rights Reserved.
  12.  *
  13.  ***********************************************************************/
  14.  
  15.  
  16. #include "abp.h"
  17.  
  18. /*
  19.  *  Declaration of IMailUser object implementation
  20.  */
  21. #undef  INTERFACE
  22. #define INTERFACE   struct _TID
  23.  
  24. #undef  MAPIMETHOD_
  25. #define MAPIMETHOD_(type, method)   MAPIMETHOD_DECLARE(type, method, TID_)
  26.         MAPI_IUNKNOWN_METHODS(IMPL)
  27.         MAPI_IMAPIPROP_METHODS(IMPL)
  28. #undef  MAPIMETHOD_
  29. #define MAPIMETHOD_(type, method)   MAPIMETHOD_TYPEDEF(type, method, TID_)
  30.         MAPI_IUNKNOWN_METHODS(IMPL)
  31.         MAPI_IMAPIPROP_METHODS(IMPL)
  32. #undef  MAPIMETHOD_
  33. #define MAPIMETHOD_(type, method)   STDMETHOD_(type, method)
  34.  
  35. DECLARE_MAPI_INTERFACE(TID_)
  36. {
  37.     MAPI_IUNKNOWN_METHODS(IMPL)
  38.     MAPI_IMAPIPROP_METHODS(IMPL)
  39. };
  40.  
  41.  
  42. /*
  43.  *  The definition of the TID object
  44.  */
  45. typedef struct _TID {
  46.  
  47.     const TID_Vtbl * lpVtbl;
  48.  
  49.     SAB_Wrapped;
  50.     
  51.     /*
  52.      *  Private data
  53.      */
  54.     LPMAILUSER  lpABUser;
  55.  
  56. } TID, *LPTID;
  57.  
  58. /*
  59.  *  TID jump table is defined here...
  60.  */
  61.  
  62. static const TID_Vtbl vtblTID =
  63. {
  64.     (TID_QueryInterface_METHOD *)   ABU_QueryInterface,
  65.     (TID_AddRef_METHOD *)           WRAP_AddRef,
  66.     TID_Release,
  67.     (TID_GetLastError_METHOD *)     WRAP_GetLastError,
  68.     (TID_SaveChanges_METHOD *)      WRAP_SaveChanges,
  69.     (TID_GetProps_METHOD *)         WRAP_GetProps,
  70.     (TID_GetPropList_METHOD *)      WRAP_GetPropList,
  71.     TID_OpenProperty,
  72.     (TID_SetProps_METHOD *)         WRAP_SetProps,
  73.     (TID_DeleteProps_METHOD *)      WRAP_DeleteProps,
  74.     (TID_CopyTo_METHOD *)           WRAP_CopyTo,
  75.     (TID_CopyProps_METHOD *)        WRAP_CopyProps,
  76.     (TID_GetNamesFromIDs_METHOD *)  WRAP_GetNamesFromIDs,
  77.     (TID_GetIDsFromNames_METHOD *)  WRAP_GetIDsFromNames,
  78. };
  79.  
  80. /*************************************************************************
  81.  *
  82.  -  NewTID
  83.  -
  84.  *  Creates the TID object associated with a mail user.
  85.  *
  86.  *
  87.  */
  88. HRESULT
  89. HrNewTID(LPMAPIPROP *       lppMAPIPropNew,
  90.         ULONG               cbTemplateId,
  91.         LPENTRYID           lpTemplateId,
  92.         ULONG               ulTemplateFlags,
  93.         LPMAPIPROP          lpPropData,
  94.         LPABLOGON           lpABPLogon,
  95.         LPCIID              lpInterface,
  96.         HINSTANCE           hLibrary,
  97.         LPALLOCATEBUFFER    lpAllocBuff,
  98.         LPALLOCATEMORE      lpAllocMore,
  99.         LPFREEBUFFER        lpFreeBuff,
  100.         LPMALLOC            lpMalloc )
  101. {
  102.     LPTID lpTID = NULL;
  103.     SCODE sc;
  104.     HRESULT hr = hrSuccess;
  105.     LPMAILUSER lpABUser = NULL;
  106.     ULONG ulObjType;
  107.  
  108.     /*
  109.      *  Create the user object corresponding to the template id
  110.      */
  111.     hr = HrNewSampUser( &lpABUser,
  112.                         &ulObjType,
  113.                         cbTemplateId,
  114.                         lpTemplateId,
  115.                         lpABPLogon,
  116.                         lpInterface,
  117.                         hLibrary,
  118.                         lpAllocBuff,
  119.                         lpAllocMore,
  120.                         lpFreeBuff,
  121.                         lpMalloc);
  122.     if (HR_FAILED(hr))
  123.     {
  124.         goto err;
  125.     }
  126.  
  127.     Assert(ulObjType == MAPI_MAILUSER);
  128.     /*
  129.      *  Allocate space for the TID structure
  130.      */
  131.     sc = lpAllocBuff(sizeof(TID), (LPVOID *) &lpTID);
  132.     if (FAILED(sc))
  133.     {
  134.         hr = ResultFromScode(sc);
  135.         goto err;
  136.     }
  137.  
  138.     /*
  139.      *  Initialize the TID structure
  140.      */
  141.  
  142.     lpTID->lpVtbl = &vtblTID;
  143.     lpTID->lcInit = 1;
  144.     lpTID->hResult = hrSuccess;
  145.     lpTID->idsLastError = 0;
  146.     lpTID->hLibrary = hLibrary;
  147.     lpTID->lpAllocBuff = lpAllocBuff;
  148.     lpTID->lpAllocMore = lpAllocMore;
  149.     lpTID->lpFreeBuff = lpFreeBuff;
  150.     lpTID->lpMalloc = lpMalloc;
  151.     lpTID->lpABLogon = lpABPLogon;
  152.     lpTID->lpPropData = lpPropData;
  153.     lpTID->lpABUser = (LPMAILUSER) lpABUser;
  154.  
  155.  
  156.     if (ulTemplateFlags & FILL_ENTRY)
  157.     {
  158.         ULONG ulCount;
  159.         LPSPropValue lpspv = NULL;
  160.  
  161.         /*
  162.          *  Copy all the properties from my object to the propdata
  163.          */
  164.         hr = lpABUser->lpVtbl->GetProps(lpABUser,
  165.             NULL,
  166.             0,      /* ansi */
  167.             &ulCount,
  168.             &lpspv);
  169.  
  170.         if (HR_FAILED(hr))
  171.             goto err;
  172.  
  173.         hr = lpPropData->lpVtbl->SetProps(lpPropData,
  174.             ulCount,
  175.             lpspv,
  176.             NULL);
  177.  
  178.         lpFreeBuff(lpspv);
  179.  
  180.         if (HR_FAILED(hr))
  181.             goto err;
  182.     }
  183.  
  184.     /*
  185.      *  AddRef lpPropData so we can use it after we return
  186.      */
  187.  
  188.     (void)lpPropData->lpVtbl->AddRef(lpPropData);
  189.  
  190.     InitializeCriticalSection(&lpTID->cs);
  191.  
  192.     /*  We must AddRef the lpABPLogon object since we will be using it
  193.      */
  194.     lpABPLogon->lpVtbl->AddRef(lpABPLogon);
  195.  
  196.     *lppMAPIPropNew = (LPVOID) lpTID;
  197.  
  198. out:
  199.  
  200.     DebugTraceResult(HrNewTID, hr);
  201.     return hr;
  202.  
  203. err:
  204.  
  205.     if (lpABUser)
  206.         lpABUser->lpVtbl->Release(lpABUser);
  207.     
  208.     lpFreeBuff(lpTID);
  209.  
  210.     goto out;
  211. }
  212.  
  213. /*
  214.  -  TID_Release
  215.  -
  216.  *  Releases the TID object.  The main difference between this
  217.  *  Release and WRAP_Release() is that it also has to release the
  218.  *  lpABUser object.
  219.  *
  220.  *  Note the Release on the lpPropData.  This required because of
  221.  *  WRAP's implementation of IUnknown (which this object reuses).
  222.  */
  223.  
  224. STDMETHODIMP_(ULONG) TID_Release(LPTID lpTID)
  225. {
  226.     LONG lcInit;
  227.  
  228.     /*
  229.      * Check to see if it's large enough to hold this object
  230.      */
  231.     if (IsBadReadPtr(lpTID, sizeof(TID)))
  232.     {
  233.         /*
  234.          *  I'm not looking at an object that I expect to be.
  235.          */
  236.         return 1;
  237.     }
  238.  
  239.     /*
  240.      *  Check to see that it's TIDs vtbl
  241.      */
  242.     if (lpTID->lpVtbl != &vtblTID)
  243.     {
  244.         /*
  245.          *  It's big enough but it's got the wrong vtbl.
  246.          */
  247.         return 1;
  248.     }
  249.  
  250.     /*
  251.      *  Release the mapi property object
  252.      */
  253.     lpTID->lpPropData->lpVtbl->Release(
  254.         lpTID->lpPropData);
  255.  
  256.     EnterCriticalSection(&lpTID->cs);
  257.     lcInit = --lpTID->lcInit;
  258.     LeaveCriticalSection(&lpTID->cs);
  259.  
  260.     if (lcInit == 0)
  261.     {
  262.         /*
  263.          *  Release the ABUser object
  264.          */
  265.         lpTID->lpABUser->lpVtbl->Release(
  266.             lpTID->lpABUser);
  267.  
  268.         /*  
  269.          *  Release our reference to the ABLogon object.
  270.          */
  271.         if (lpTID->lpABLogon)
  272.         {
  273.             lpTID->lpABLogon->lpVtbl->Release(lpTID->lpABLogon);
  274.             lpTID->lpABLogon = NULL;
  275.         }
  276.  
  277.         /*
  278.          *  Clean up the critical section
  279.          */
  280.         DeleteCriticalSection(&lpTID->cs);
  281.  
  282.         /*
  283.          * Need to free the object
  284.          */
  285.         lpTID->lpFreeBuff(lpTID);
  286.         return 0;
  287.     }
  288.  
  289.     return lcInit;
  290. }
  291.  
  292. /*
  293.  -  TID_OpenProperty
  294.  -
  295.  *  Satisfies the object that are needed to support the "Options" details pane
  296.  *  associated with the MailUser object from ABUSER.C.
  297.  *
  298.  *  Note:  We are masking error strings that might be possible to get from the
  299.  *  lpABUser object.  Since (for the most part) the only errors that can be returned
  300.  *  from this object are resource failure types, it wouldn't be of much use to the
  301.  *  user.
  302.  */
  303.  
  304. STDMETHODIMP
  305. TID_OpenProperty(LPTID lpTID,
  306.     ULONG ulPropTag,
  307.     LPCIID lpiid,
  308.     ULONG ulInterfaceOptions,
  309.     ULONG ulFlags,
  310.     LPUNKNOWN * lppUnk)
  311. {
  312.     HRESULT hResult;
  313.  
  314.     /*
  315.      *  Check to see if it's large enough to hold this object
  316.      */
  317.     if (IsBadReadPtr(lpTID, sizeof(TID)))
  318.     {
  319.         /*
  320.          *  No vtbl found
  321.          */
  322.         return ResultFromScode(E_INVALIDARG);
  323.     }
  324.  
  325.     /*
  326.      *  Check to see that it's TIDs vtbl
  327.      */
  328.     if (lpTID->lpVtbl != &vtblTID)
  329.     {
  330.         /*
  331.          *  It's big enough but it's got the wrong vtbl.
  332.          */
  333.         return ResultFromScode(E_INVALIDARG);
  334.     }
  335.  
  336.     if ( ulInterfaceOptions & ~MAPI_UNICODE )
  337.     {
  338.         DebugTraceArg( TID_OpenProperty, "unknown flags" );
  339.         return ResultFromScode( MAPI_E_UNKNOWN_FLAGS );
  340.     }
  341.     
  342.     if ( ulInterfaceOptions & MAPI_UNICODE )
  343.     {
  344.         DebugTraceArg( TID_OpenProperty, "bad character width" );
  345.         return ResultFromScode( MAPI_E_BAD_CHARWIDTH );
  346.     }
  347.     /*
  348.      *  Don't want to check any other parameters here.
  349.      *  Calls down to wrapped objects will do this for
  350.      *  me.
  351.      */
  352.  
  353.     switch (ulPropTag)
  354.     {
  355.         case PR_LISTBOX_TABLE:
  356.         case PR_DDLISTBOX_TABLE:
  357.         case PR_COMBOBOX_TABLE:
  358.         {
  359.             hResult = lpTID->lpABUser->lpVtbl->OpenProperty(
  360.                 lpTID->lpABUser,
  361.                 ulPropTag,
  362.                 lpiid,
  363.                 ulInterfaceOptions,
  364.                 ulFlags,
  365.                 lppUnk);
  366.             break;
  367.  
  368.         }
  369.  
  370.         default:
  371.         {
  372.             hResult = lpTID->lpPropData->lpVtbl->OpenProperty(
  373.                 lpTID->lpPropData,
  374.                 ulPropTag,
  375.                 lpiid,
  376.                 ulInterfaceOptions,
  377.                 ulFlags,
  378.                 lppUnk);
  379.             break;
  380.         }
  381.     }
  382.  
  383.     DebugTraceResult(TID_OpenProperty, hResult);
  384.     return hResult;
  385. }
  386.