//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#if WIN32
using System;
using System.ComponentModel;
#if LEGACY
using System.Security.Permissions;
#endif // LEGACY
using System.Runtime.InteropServices;

namespace CommonHelpers {

//**************************************************************************************************
/// <summary>
/// </summary>
#if LEGACY
[PermissionSet(SecurityAction.Demand,Unrestricted=true)]
#endif // LEGACY
public static partial class HCOMError {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static string HRToMessage(int errorCode)
{
   return HRToMessage( errorCode, HIntPtr.MinusOneValue );
}

public static string HRToMessage(int errorCode,[HOptional] IntPtr errorInfo)
{
   Exception e = Marshal.GetExceptionForHR( errorCode, errorInfo );
   return e != null ? e.Message :
      "Undefined COM-error.";
}

//==================================================================================================

/// <exception cref="ArgumentNullException" />
public static string ComposeOpFailedErrorMessage(string functionName)
{
   return ComposeOpFailedErrorMessage( functionName, null );
}

/// <exception cref="ArgumentNullException" />
public static string ComposeOpFailedErrorMessage
(
   string functionName, [HOptional] string remarkMessage
)
{
   HArgChecking.VerifyNotNull( functionName );
   return HString.FormatAsText(
      "COM-function \"{0}\" has failed.{1}", functionName,
      remarkMessage!=null ? string.Format("\n(0)",remarkMessage) : "" );
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="ArgumentNullException" />
public static string ComposeOpFailedErrorMessageWithReturnCodeToken
(
   string functionName, string returnCodeToken
)
{
   HArgChecking.VerifyNotNulls( functionName, returnCodeToken );
   return ComposeOpFailedErrorMessage( functionName,
      string.Format("COM Return Code = {0}",returnCodeToken) );
}

//==================================================================================================

static HCOMError()
{
   CommonHelpers_Core.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HCOMError
//**************************************************************************************************

} // CommonHelpers
#endif // WIN32
