As usual, comments are welcomed!

HResult.h

#pragma once

 

namespace Microsoft { namespace MCS { namespace PartnerISV {

 

using namespace System;

 

public ref class HResult {

   HRESULT _hResult;

   String ^_description;

public:

   HResult(HRESULT hResult);

   HResult(bool severity, UInt16 Severity, UInt16 code);

   static HResult ^ FromWin32Error(Int32 win32Error);

   property Int32 Value {Int32 get();}

   property bool Severity {bool get();}

   property UInt16 Facility {UInt16 get();}

   property UInt16 Code {UInt16 get();}

   property String ^ Description {String ^ get();}

   virtual String ^ ToString() override;

};

 

}}}

 

HResult.cpp

#include "StdAfx.h"

#include "HResult.h"

 

namespace Microsoft { namespace MCS { namespace PartnerISV {

HResult::HResult(HRESULT hResult)

        :_hResult(hResult) {

}

 

HResult::HResult(bool severity, UInt16 facility, UInt16 code)

        :_hResult(MAKE_HRESULT(severity, facility, code)) {

}

 

HResult ^ HResult::FromWin32Error(Int32 win32Error) {

   return gcnew HResult(HRESULT_FROM_WIN32(win32Error));

}

 

Int32 HResult::Value::get() {

   return _hResult;

}

 

bool HResult::Severity::get() {

   return HRESULT_SEVERITY(_hResult);

}

 

UInt16 HResult::Facility::get() {

   return static_cast<UInt16>(HRESULT_FACILITY(_hResult));

}

 

UInt16 HResult::Code::get() {

   return static_cast<UInt16>(HRESULT_CODE(_hResult));

}

 

String ^ HResult::Description::get() {

   if (!_description) {

      wchar_t * message = NULL;

      try {  

         if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, _hResult, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(& message), 0, NULL))

            _description = gcnew String(message);

         else

            _description = String::Format( "Cannot get description for HRESULT equal to {0}", _hResult);

      } finally {

         LocalFree(message);

      }

   }

   return _description;

}

 

String ^ HResult::ToString() {

   return Description;

}

 

}}}