//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// LNK-CONVERTER UTILITY NATIVE SUPPORT (API) EXPORTED FROM "NativeLib.dll":
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// NativeLib DLL (for DotNetHelpers) | C+- (SysRoutine) | Author: Sergei Y.Kitáev
#include "Precompiled.h"
#include "SharedIncludes.h"
#include "partition\LnkConverter.h"
//==================================================================================================
NL_API
BOOL WINAPI LnkConverter_ScreenCorruptionTest($CI_TAG) nl_last_error
{
   NL_API_ENTRY_VERIFY(); // NL-initialization checking; reset NL-last-error;
/*
// $TCI_INVALID_OPERATION();
   return NL_FAIL( $TEXT(
      L"Test of “screen corruption” is not implemented at current time.",
      L"Тест “повреждения экрана” в настоящее время не реализован." ));
*/
   int cxScreen = ::GetSystemMetrics( SM_CXSCREEN );
   int cyScreen = ::GetSystemMetrics( SM_CYSCREEN );
   if ( !cxScreen || !cyScreen )
      return NLFailure::SetSystemError($, $TEXT(
         L"Can not obtain main screen size(s).",
         L"Не удаётся получить размер(ы) основного экрана." ));

   HDC hScreen = ::GetDC( nullptr );
   if ( !hScreen )
      return NL_FAIL_LAST_SYSTEM();

   HANDLE hCurrentThread = ::GetCurrentThread();
   int savedPriority = ::GetThreadPriority( hCurrentThread );
   if ( savedPriority == THREAD_PRIORITY_ERROR_RETURN ||
        !::SetThreadPriority( hCurrentThread, THREAD_PRIORITY_ABOVE_NORMAL ) )
      return NL_FAIL_LAST_SYSTEM();
   $ON_COMPLETE( priorityCompleter ) $last_error
   {
      return ::SetThreadPriority( hCurrentThread, savedPriority ) ?
         true : $SET_FROM_SYSTEM_LE();
   }
   $END_ON_COMPLETE( priorityCompleter );

   bool refreshAttempt = false;
   bool success =
   $WITH_FINALLY_BOOL
   {
      if ( !LnkConverter::PerformDrawing( hScreen, cxScreen, cyScreen ) ||
           !LnkConverter::ReleaseScreenDC( hScreen ) )
         return NL_FAIL_LAST_RT_ERROR();

      ::Sleep( 2000 );

      if ( !LnkConverter::RefreshScreen( refreshAttempt ) ||
           !priorityCompleter.Complete() )
         return NL_FAIL_LAST_RT_ERROR();

      return true;
   }
   $FINALIZATION
   {
      LnkConverter::ReleaseScreenDC( hScreen );
      LnkConverter::RefreshScreen( refreshAttempt );
   }
   $END_FINALLY;

   return success ? NL_SUCCEED() : FALSE;
}

//--------------------------------------------------------------------------------------------------
//==================================================================================================
