home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / errcpp.h < prev    next >
C/C++ Source or Header  |  1998-04-25  |  4KB  |  98 lines

  1. // ErrCpp.h---------------------------------------------------------------------
  2. // Defines a helper class for mapi errors.
  3. //
  4. // Copyright 1986 - 1998 Microsoft Corporation.  All Rights Reserved.
  5. // -----------------------------------------------------------------------------
  6.  
  7. #ifndef _ERRCPP_H_
  8. #define _ERRCPP_H_
  9.  
  10. #include "EdkUtCpp.H"
  11.  
  12. // -----------------------------------------------------------------------------
  13. // This class is intended as a replacement for the HRESULT type defined by MAPI.
  14. // It has two advantages over MAPI's version.
  15. //   1) You can report the error code as a readable string using the Msg() 
  16. //      member function.
  17. //   2) When in debug mode you can view this object and see the return code
  18. //      as a readable string by viewing the contents of the m_sMsg member object.
  19. //
  20. // You can use this exactly like you use HRESULT.  In other words you can assign
  21. // an HRESULT return value directly to it.  You can test it using FAILED.  You
  22. // can assign it back to a type HRESULT or return it from a function whose return
  23. // type is HRESULT.  You can even assign it to a const char* or pass it to a 
  24. // function that takes a const char* and you will get the readable string version
  25. // of the HRESULT.
  26. // -----------------------------------------------------------------------------
  27.  
  28. class CHRESULT
  29. {
  30. public:
  31.     // CONSTRUCTOR: defaults to NOERROR or uses the value you pass in.
  32.     CHRESULT( HRESULT hr = NOERROR)
  33.     {
  34.         m_hr = hr;
  35.         #if DEBUG
  36.             m_sMsg = m_idlMAPIErr.Find( m_hr);
  37.         #endif
  38.     }
  39.     
  40.     // Returns TRUE if the HRESULT failed.
  41.     BOOL bFailed() {return( FAILED( m_hr));}
  42.  
  43.     // Returns the defined constant for the error as a string constant. Usage example:
  44.     //   CHRESULT hr = MAPI_E_NOT_FOUND;
  45.     //   char chBuf[500];
  46.     //   sprintf( chBuf, "Error: %s occurred", hr.Msg());
  47.     const LPSTR Msg()
  48.     {
  49.         #if DEBUG
  50.             return( m_sMsg.GetBuffer(0));
  51.         #else
  52.             return( m_idlMAPIErr.Find( m_hr));
  53.         #endif
  54.     }
  55.     
  56.     // Returns the defined constant for the error as a string. Usage example:
  57.     //   CHRESULT hr = MAPI_E_NOT_FOUND;
  58.     //   const char* pErrMsg = hr;    // returns "MAPI_E_NOT_FOUND".
  59.     operator LPSTR()                {return( Msg());}                   
  60.  
  61.     // Returns the HRESULT. Usage example:
  62.     //   CHRESULT hr = MAPI_E_NOT_FOUND;
  63.     //   HRESULT mapiHR = hr;
  64.     operator HRESULT()              {return( m_hr);}                    
  65.  
  66.     // Assigns an HRESULT to CHRESULT. Usage example:
  67.     //   CHRESULT hr;       // Defaults to SUCCESS_SUCCESS
  68.     //   hr = imapiProp->GetProps( ...
  69.     const CHRESULT& operator=( HRESULT hr)
  70.     {
  71.         m_hr = hr;
  72.         #if DEBUG
  73.             m_sMsg = m_idlMAPIErr.Find( m_hr);
  74.         #endif
  75.         return( *this);
  76.     }
  77.  
  78. protected:
  79.     static CIDList  m_idlMAPIErr;   // Static ID list of MAPI Errors.
  80.     HRESULT         m_hr;           // Contains the actual HRESULT.
  81.     
  82.     #if DEBUG
  83.     CString         m_sMsg;         // In the dubugger look at this to see the 
  84.     #endif                          // current HRESULT as a readable string.
  85. };
  86.  
  87. // -----------------------------------------------------------------------------
  88. #ifdef _GLOBAL_OBJECTS_
  89. #pragma message("Declaration of global objects for: " __FILE__)
  90.  
  91. #include "ErrCppD.h"
  92. CIDList CHRESULT::m_idlMAPIErr( MAPIErrors, ARRAY_CNT( MAPIErrors));
  93.  
  94. #endif //_GLOBAL_OBJECTS_
  95. // -----------------------------------------------------------------------------
  96.  
  97. #endif // _ERRCPP_H_
  98.