Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.0 KiB

  1. #ifndef _CHRESULT_HXX_
  2. #define _CHRESULT_HXX_
  3. class CHResult
  4. {
  5. public:
  6. ~CHResult()
  7. {
  8. Initialize();
  9. }
  10. CHResult()
  11. {
  12. _hr = (HRESULT) E_FAIL;
  13. _bstrMessage = NULL;
  14. _pszFile = NULL;
  15. _ulLine = (ULONG) 0;
  16. }
  17. CHResult( CHResult &chr )
  18. {
  19. _hr = chr._hr;
  20. _ulLine = chr._ulLine;
  21. _bstrMessage = SysAllocString( chr._bstrMessage );
  22. _pszFile = new CHAR[ strlen(chr._pszFile) + sizeof(CHAR) ];
  23. if( NULL != _pszFile )
  24. memcpy( _pszFile, chr._pszFile, strlen(chr._pszFile) + sizeof(CHAR) );
  25. }
  26. CHResult( HRESULT hr, const LPOLESTR pwszMessage, const LPSTR pszFile, ULONG ulLine )
  27. {
  28. _hr = hr;
  29. _bstrMessage = SysAllocString( pwszMessage );
  30. _pszFile = new CHAR[strlen(pszFile) + sizeof(CHAR)];
  31. if( NULL != _pszFile )
  32. memcpy( _pszFile, pszFile, strlen(pszFile) + sizeof(CHAR) );
  33. _ulLine = ulLine;
  34. }
  35. CHResult( HRESULT hr, LPOLESTR pwszMessage )
  36. {
  37. this->CHResult::CHResult( hr, pwszMessage, NULL, 0 );
  38. }
  39. public:
  40. operator HRESULT()
  41. {
  42. return( _hr );
  43. }
  44. operator LPOLESTR()
  45. {
  46. return( _bstrMessage );
  47. }
  48. HRESULT operator =(HRESULT hr)
  49. {
  50. _hr = hr;
  51. SysFreeString( _bstrMessage );
  52. _bstrMessage = NULL;
  53. return(_hr);
  54. }
  55. public:
  56. HRESULT GetLastError()
  57. {
  58. Initialize();
  59. _hr = HRESULT_FROM_WIN32(::GetLastError());
  60. return( _hr );
  61. }
  62. LPSTR GetFile()
  63. {
  64. return( _pszFile );
  65. }
  66. ULONG GetLine()
  67. {
  68. return( _ulLine );
  69. }
  70. private:
  71. void Initialize()
  72. {
  73. _hr = (HRESULT) E_FAIL;
  74. _ulLine = 0;
  75. SysFreeString( _bstrMessage );
  76. _bstrMessage = NULL;
  77. CoTaskMemFree( _pszFile );
  78. _pszFile = NULL;
  79. }
  80. private:
  81. HRESULT _hr;
  82. BSTR _bstrMessage;
  83. LPSTR _pszFile;
  84. ULONG _ulLine;
  85. };
  86. #define CHRESULT(hr,pwszMessage) CHResult( hr, pwszMessage, __FILE__, __LINE__ )
  87. #endif