//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// "SHLProps"-CODE-PACKAGE 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\SHLProps.h"
//==================================================================================================
NL_API
HANDLE WINAPI SHLProps_CreateInstance($CI_TAG) $null_on_failure $needs_finalize nl_last_error
{
   NL_API_ENTRY_VERIFY();

   OSVERSIONINFOEX osvi = {};
   osvi.dwOSVersionInfoSize = sizeof(osvi);
   DWORDLONG conditionalMask = 0;
   VER_SET_CONDITION( conditionalMask, VER_MAJORVERSION, VER_GREATER_EQUAL );
   if ( !::VerifyVersionInfo( &osvi, VER_MAJORVERSION, conditionalMask ) )
      return NL_FAIL_LAST_SYSTEM();

   auto pInstance = $memory::Allocate<SHLPropsHnd>($TCI);
   if ( !pInstance )
      return NL_FAIL_LAST_RT_ERROR();
   $ap<SHLPropsHnd> apInstance($, pInstance );

   return pInstance->Initialize() ?
      NL_RESET_ERROR( apInstance.TakePointerOwnership($) ) :
      nullptr;
}

//--------------------------------------------------------------------------------------------------
NL_API
VOID WINAPI SHLProps_Release($CI_TAG, HANDLE instancePtr $optional )
{
   NL_API_ENTRY_VERIFY_0();
   if ( instancePtr )
      ((SHLPropsHnd*)instancePtr) -> Finalize();
}

//==================================================================================================
NL_API
BOOL WINAPI SHLProps_Load($CI_TAG, HANDLE instancePtr, PCWSTR filePath,
                                   NLShellLinkSpecialProps* specialPropsPtr $out ) nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   $TCI_VERIFY_POINTER_ARG( filePath );

   $memory::ClearIfNotNull( specialPropsPtr );
   HRESULT hResult; SHELL_LINK_DATA_FLAGS dataFlags;
   if ( FAILED( hResult = HND.PPersistFileInterface -> Load( filePath, STGM_READ )) ||
        FAILED( hResult = HND.PSLDataListInterface -> GetFlags( (PDWORD)&dataFlags )) )
      return NLFailure::SetCOMError( hResult );
   if ( specialPropsPtr )
      specialPropsPtr -> RunAsAdministrator = $BOOL( dataFlags & SLDF_RUNAS_USER );

   return NL_SUCCEED();
}

//--------------------------------------------------------------------------------------------------
NL_API
BOOL WINAPI SHLProps_Save($CI_TAG, HANDLE instancePtr, PCWSTR filePath ) nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   return HND.SaveProperties($TCI, filePath, nullptr );
}
NL_API
BOOL WINAPI SHLProps_SaveEx($CI_TAG, HANDLE instancePtr, PCWSTR filePath,
                                     CONST NLShellLinkSpecialProps* specialPropsPtr $optional )
                                     nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   return HND.SaveProperties($TCI, filePath, specialPropsPtr );
}

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