home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / mts4.cab / Account.VC_GetReceipt.cpp < prev    next >
C/C++ Source or Header  |  1997-11-14  |  5KB  |  183 lines

  1. // Filename: GetReceipt.cpp
  2. //
  3. // Description: Implementation of CGetReceipt
  4. //
  5. // This file is provided as part of the Microsoft Transaction Server Samples
  6. //
  7. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
  8. // WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
  9. // INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
  10. // OF MERCHANTABILITY AND/OR FITNESS FOR A  PARTICULAR 
  11. // PURPOSE.
  12. //
  13. // Copyright (C) 1997 Microsoft Corporation, All rights reserved
  14.  
  15. #include "stdafx.h"
  16. #include "Account.h"
  17. #include "GetReceipt.h"
  18. #include "UpdateReceipt.h"
  19.  
  20. #include <mtx.h>
  21. #include <mtxspm.h>
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. //
  25.  
  26. STDMETHODIMP CGetReceipt::InterfaceSupportsErrorInfo(REFIID riid)
  27. {
  28.     static const IID* arr[] = 
  29.     {
  30.         &IID_IGetReceipt,
  31.     };
  32.     
  33.     for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  34.     {
  35.         if (InlineIsEqualGUID(*arr[i],riid))
  36.             return S_OK;
  37.     }
  38.     return S_FALSE;
  39. }
  40.  
  41. //    GetNextReceipt: return the next receipt number
  42. //
  43. //    returns: S_OK or E_FAIL
  44.  
  45. STDMETHODIMP CGetReceipt::GetNextReceipt (OUT long* plReceiptNo) {
  46.     
  47.     HRESULT hr = S_OK;
  48.     
  49.     IObjectContext* pObjectContext = NULL;
  50.     
  51.     ISharedPropertyGroupManager* spmMgr = NULL;
  52.     ISharedPropertyGroup* spmGroup = NULL;
  53.     ISharedProperty* spmPropNextReceipt = NULL;
  54.     ISharedProperty* spmPropMaxNum = NULL;
  55.     
  56.     IUpdateReceipt* pObjUpdateReceipt = NULL;
  57.  
  58.     CComVariant vNextReceipt;
  59.  
  60.     long lErrFlag = 0;
  61.     TCHAR* pErrMsg = NULL;
  62.  
  63.  
  64.     try {
  65.         
  66.         // First of all, get the object context
  67.         THROW_ERR ( GetObjectContext(&pObjectContext), "GetObjectContext" );
  68.  
  69.         // Create the SharedPropertyGroupManager
  70.         THROW_ERR ( pObjectContext->CreateInstance (CLSID_SharedPropertyGroupManager, 
  71.             IID_ISharedPropertyGroupManager, (void**)&spmMgr), "CreateInstance(CLSID_SharedPropertyGroupManager)" );
  72.         
  73.         // Create the SharedPropertyGroup
  74.         LONG lIsolationMode = LockMethod;
  75.         LONG lReleaseMode = Process;
  76.         VARIANT_BOOL bExists = VARIANT_FALSE;
  77.         THROW_ERR ( spmMgr->CreatePropertyGroup (L"Receipt", &lIsolationMode, &lReleaseMode, &bExists, &spmGroup),
  78.             "CreatePropertyGroup" );
  79.         
  80.         // Create the two shared properties
  81.         THROW_ERR ( spmGroup->CreateProperty (L"Next", &bExists, &spmPropNextReceipt), "CreateProperty(Next)" );
  82.         THROW_ERR ( spmGroup->CreateProperty (L"MaxNum", &bExists, &spmPropMaxNum), "CreateProperty(MaxNum)" );
  83.         
  84.         // Obtain their current values
  85.         CComVariant vMaxNum;
  86.         THROW_ERR ( spmPropNextReceipt->get_Value (&vNextReceipt), "spmPropNextReceipt->get_Value" );
  87.         THROW_ERR ( spmPropMaxNum->get_Value (&vMaxNum), "spmPropMaxNum->get_Value" );
  88.         
  89.         // Does MaxNum require an update?
  90.         if (vNextReceipt.lVal >= vMaxNum.lVal) {
  91.             
  92.             // Create an UpdateReceipt object
  93.             THROW_ERR( pObjectContext->CreateInstance (CLSID_CUpdateReceipt, IID_IUpdateReceipt,
  94.                 (void**)&pObjUpdateReceipt), "CreateInstance(CLSID_CUpdateReceipt)" );
  95.             
  96.             // Update NextReceipt
  97.             RETHROW_ERR ( pObjUpdateReceipt->Update (&vNextReceipt.lVal) );
  98.  
  99.             // Update NextReceipt shared property
  100.             THROW_ERR ( spmPropNextReceipt->put_Value (vNextReceipt), "spmPropNextReceipt->put_Value" );
  101.             
  102.             // Update MaxNum shared property
  103.             vMaxNum.lVal = vNextReceipt.lVal + 100;
  104.             THROW_ERR ( spmPropMaxNum->put_Value (vMaxNum), "spmPropMaxNum->put_Value" );
  105.         }
  106.  
  107.         // Increment NextReceipt shared property
  108.         vNextReceipt.lVal ++;
  109.         THROW_ERR ( spmPropNextReceipt->put_Value (vNextReceipt), "spmPropNextReceipt->put_Value" );
  110.         *plReceiptNo = vNextReceipt.lVal;
  111.         hr = S_OK;
  112.  
  113.         // We're finished and happy
  114.         pObjectContext->SetComplete();
  115.         
  116.     } catch (HRESULT hr) {
  117.         
  118.         //
  119.         //    ErrorInfo is saved here because the following ADO cleanup code 
  120.         //    may clear it.
  121.         //
  122.         IErrorInfo * pErrorInfo = NULL;
  123.         GetErrorInfo(NULL, &pErrorInfo);
  124.  
  125.         // Fill in error information
  126.         switch (lErrFlag) {
  127.         
  128.         // Unknown error occurred in this object
  129.         case (0):
  130.             TCHAR szErr [512];
  131.             wsprintf (szErr, _T("Error 0x%x from CGetReceipt calling %s."), hr, pErrMsg);
  132.             pErrMsg = szErr;
  133.             // Fall through
  134.         
  135.         // An application error occurred in this object
  136.         case (1):
  137.             //
  138.             //    we are going to put our own error in TLS, so if there is one there, clear it
  139.             //
  140.             if (pErrorInfo)
  141.                 pErrorInfo -> Release();
  142.             
  143.             AtlReportError( CLSID_CGetReceipt, pErrMsg, IID_IGetReceipt, hr);
  144.             break;
  145.  
  146.         case (2):    // An error occurred in a called object
  147.             //
  148.             //    put the error back in TLS
  149.             //
  150.             SetErrorInfo(NULL, pErrorInfo);
  151.             break;
  152.         
  153.         // Will never reach here
  154.         default:
  155.             break;
  156.         }
  157.  
  158.         // Indicate our unhappiness
  159.         if (pObjectContext)
  160.             pObjectContext->SetAbort();
  161.     }
  162.     
  163.     // Resource cleanup
  164.     if (pObjectContext)
  165.         pObjectContext->Release();
  166.     
  167.     if (spmMgr)
  168.         spmMgr->Release();
  169.     
  170.     if (spmGroup)
  171.         spmGroup->Release();
  172.     
  173.     if (spmPropNextReceipt)
  174.         spmPropNextReceipt->Release();
  175.     
  176.     if (spmPropMaxNum)
  177.         spmPropMaxNum->Release();
  178.     
  179.     if (pObjUpdateReceipt)
  180.         pObjUpdateReceipt->Release();
  181.     
  182.     return hr;
  183. }