// RuntimeForStatic base library | C+- (SysRoutine) | Author: Sergei Y.Kitáev
#include "Precompiled.h"
#include "SharedIncludes.h"
namespace RuntimeForStatic {
//==================================================================================================

$DynText $Win32Error::GetErrorMessage( DWORD dwError ) $requires_rt_init $tenacious
{
   $DynText errorMessage; bool isMIDNotFoundError;
   GetErrorMessage($, dwError, errorMessage, isMIDNotFoundError );
   return errorMessage;
}
bool $Win32Error::GetErrorMessage($A, DWORD dwError, $DynText& errorMessage $out ) $last_error
{
   bool isMIDNotFoundError;
   return GetErrorMessage($ci, dwError, errorMessage, isMIDNotFoundError );
}
bool $Win32Error::GetErrorMessage($A, DWORD dwError, $DynText& errorMessage $out,
                                      bool& isMIDNotFoundError $out ) $last_error
{
   $$VERIFY_DISPOSABLE_ARG( errorMessage );
   errorMessage.Reset($ci);
   isMIDNotFoundError = false;
   PWSTR pMessageText = HelpersRT::GetSystemMessage($, dwError );
   $AUTO_FREE( pMessageText );
   if ( !pMessageText )
   {
      if ( ::GetLastError() == ERROR_MR_MID_NOT_FOUND )
         isMIDNotFoundError = true;
      return ::GetLastError() ?
         $SET_FROM_SYSTEM_LE() :
         $LE::SetError( $StaticMessages::MemoryAllocationFailure() );
   }
   int l = $SZ::Length($, pMessageText ), i = l - 1;
   while ( i>=0 && $Char::IsSpace( pMessageText[i] ) ) i--;
   if ( i < l - 1 ) pMessageText[ l = i + 1 ] = L'\0';
   bool success = errorMessage.AssignNS($, pMessageText );
   return success ? $SUCCEED_LE() :
      $LE::SetError( $StaticMessages::MemoryAllocationFailure() );
}

$DynText $Win32Error::GetLastErrorMessage() $requires_rt_init $tenacious
{
   return GetErrorMessage( ::GetLastError() );
}

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

$DynText $Win32Error::GetErrorMessageOrNull($A, DWORD dwError, $DynText& errorMessage $out,
                                                bool& isMIDNotFoundError $out ) $null_on_success
{
   $$VERIFY_DISPOSABLE_ARG( errorMessage );
   errorMessage.Reset($ci);
   isMIDNotFoundError = false;
   PWSTR pMessageText = HelpersRT::GetSystemMessage($, dwError );
   if ( !pMessageText )
   {
      DWORD error = ::GetLastError();
      if ( error == ERROR_MR_MID_NOT_FOUND )
         isMIDNotFoundError = true;
      return error && (pMessageText = HelpersRT::GetSystemMessage($, dwError )) ?
         $DynText($, pMessageText, true ) :
         $DynText::MemoryAllocationFailure_Message();
   }
   int l = $SZ::Length($, pMessageText ), i = l - 1;
   while ( i>=0 && $Char::IsSpace( pMessageText[i] ) ) i--;
   if ( i < l - 1 ) pMessageText[ l = i + 1 ] = L'\0';
   PWSTR pMessageText2 = HelpersRT::DuplicateStr($, pMessageText, l );
   $Memory::SysLocalFree($, pMessageText );
   if ( !pMessageText2 )
      return $DynText::MemoryAllocationFailure_Message();
   errorMessage.SetPointer($, pMessageText2, false );
   return $DynText::CreateNull();
}

$DynText $Win32Error::GetLastErrorMessageOrNull() $null_on_success
{
   $DynText errorMessage; bool isMIDNotFoundError;
   return GetErrorMessageOrNull($, ::GetLastError(), errorMessage, isMIDNotFoundError );
}

//--------------------------------------------------------------------------------------------------
/*
bool $Win32Error::IsUnknown(DWORD dwError)
{
   return dwError == UnknownError;
}

bool $Win32Error::IsLastSysErrorUnknown()
{
   return IsUnknown( ::GetLastError() );
}
*/
//--------------------------------------------------------------------------------------------------

bool $Win32Error::SetLastSysError(DWORD dwError) $ret_false
{
   ::SetLastError( dwError ); return false;
}
/*
bool $Win32Error::SetLastSysErrorUnknown() $ret_false
{
   return SetLastSysError( UnknownError );
}
*/
bool $Win32Error::ResetLastSysError() $ret_true
{
   ::SetLastError( 0 ); return true;
}

//==================================================================================================
} // RuntimeForStatic
