//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// "SHLProps"-CODE-PACKAGE NATIVE SUPPORT (API) — "Get"-FUNCTIONS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// NativeLib DLL (for DotNetHelpers) | C+- (SysRoutine) | Author: Sergei Y.Kitáev
#include "Precompiled.h"
#include "SharedIncludes.h"
#include "partition\SHLProps.h"
//==================================================================================================
static const int strbuff_initial_capacity = 64;
//==================================================================================================
/*
static PWSTR Hyphen()
{
   PWSTR pValue = $SZ::DuplicateNS($, L"-" );
   return pValue ? NL_RESET_ERROR( pValue ) : NL_FAIL_LAST_RT_ERROR();
}
*/
//==================================================================================================
NL_API
PWSTR WINAPI SHLProps_GetPath($CI_TAG, HANDLE instancePtr ) $needs_free nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   WCHAR szValue[ MAX_PATH ];
   HRESULT hResult = HND.PSLInterface ->
      GetPath( $ARRAY_N_COUNT_OF(szValue), nullptr, SLGP_RAWPATH );
   if ( FAILED( hResult ) )
      return (PWSTR)SHLProps::CantExtractSLProperty($, L"Path", hResult );
   PWSTR pValue = $SZ::DuplicateNS($, szValue );
   return pValue ? NL_RESET_ERROR( pValue ) : NL_FAIL_LAST_RT_ERROR();
}

//--------------------------------------------------------------------------------------------------
NL_API
PWSTR WINAPI SHLProps_GetArguments($CI_TAG, HANDLE instancePtr ) $needs_free nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   $StrBuffAuto sb;
   if ( !sb.ReAllocateNS($TCI, strbuff_initial_capacity ) )
      NL_FAIL_LAST_RT_ERROR();
   $LOOP
   {
      sb.SetLastChar($, 1 );
      HRESULT hResult = HND.PSLInterface -> GetArguments( sb.P($), sb.Capacity($) );
      if ( FAILED( hResult ) )
         return (PWSTR)SHLProps::CantExtractSLProperty($, L"Arguments", hResult );
      if ( sb.LastChar($) )
         return NL_RESET_ERROR( sb.TakePointerOwnership($) );
      if ( !sb.TwiceEnlargeNS($TCI) )
         NL_FAIL_LAST_RT_ERROR();
   }
}

//--------------------------------------------------------------------------------------------------
NL_API
PWSTR WINAPI SHLProps_GetDescription($CI_TAG, HANDLE instancePtr ) $needs_free nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   WCHAR szValue[ INFOTIPSIZE ];
   HRESULT hResult = HND.PSLInterface -> GetDescription( $ARRAY_N_COUNT_OF(szValue) );
   if ( FAILED( hResult ) )
      return (PWSTR)SHLProps::CantExtractSLProperty($, L"Description", hResult );
   PWSTR pValue = $SZ::DuplicateNS($, szValue );
   return pValue ? NL_RESET_ERROR( pValue ) : NL_FAIL_LAST_RT_ERROR();
}

//--------------------------------------------------------------------------------------------------
NL_API
BOOL WINAPI SHLProps_GetHotkey($CI_TAG, HANDLE instancePtr, PUSHORT valuePtr $out ) nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   $TCI_VERIFY_POINTER_ARG( valuePtr );
   *valuePtr = 0;
   HRESULT hResult = HND.PSLInterface -> GetHotkey( valuePtr );
   return SUCCEEDED( hResult ) ? NL_SUCCEED() :
      SHLProps::CantExtractSLProperty($, L"Hotkey", hResult );
}

//--------------------------------------------------------------------------------------------------
NL_API
PWSTR WINAPI SHLProps_GetDisplayName($CI_TAG, HANDLE instancePtr ) $needs_free nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();

   PIDLIST_ABSOLUTE pIDList = nullptr;
   $TASKMEM_AUTO( pIDList );
   HRESULT hResult = HND.PSLInterface -> GetIDList( &pIDList );
   if ( FAILED( hResult ) )
      return (PWSTR)SHLProps::CantExtractSLProperty($, L"IDList", hResult );
   if ( hResult == S_FALSE )
   {
      PWSTR pEmpty = $SZ::DuplicateNS($, L"" );
      return pEmpty ? NL_RESET_ERROR( pEmpty ) : NL_FAIL_LAST_RT_ERROR();
   }

   IShellFolder *pDesktopFolder = nullptr;
   $COM_AUTO( pDesktopFolder );
   if ( FAILED( hResult = ::SHGetDesktopFolder( &pDesktopFolder ) ) )
      return (PWSTR)NL_FAIL_COM( hResult );

   STRRET strRet = {};
   $TASKMEM_AUTO( strRet.pOleStr );
   if ( FAILED( hResult = pDesktopFolder -> GetDisplayNameOf(
        pIDList, SHGDN_FORPARSING, &strRet ) ) )
      return (PWSTR)SHLProps::CantExtractSLProperty($, L"DisplayName", hResult );

   PWSTR pValue = $SZ::DuplicateNS($, strRet.pOleStr );
   return pValue ? NL_RESET_ERROR( pValue ) : NL_FAIL_LAST_RT_ERROR();
}

//--------------------------------------------------------------------------------------------------
NL_API
BOOL WINAPI SHLProps_GetShowCmd($CI_TAG, HANDLE instancePtr, PINT valuePtr $out ) nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   $TCI_VERIFY_POINTER_ARG( valuePtr );
   *valuePtr = 0;
   HRESULT hResult = HND.PSLInterface -> GetShowCmd( valuePtr );
   return SUCCEEDED( hResult ) ? NL_SUCCEED() :
      SHLProps::CantExtractSLProperty($, L"ShowCmd", hResult );
}

//--------------------------------------------------------------------------------------------------
NL_API
PWSTR WINAPI SHLProps_GetWorkingDirectory($CI_TAG, HANDLE instancePtr ) $needs_free nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   $StrBuffAuto sb;
   if ( !sb.ReAllocateNS($TCI, strbuff_initial_capacity ) )
      NL_FAIL_LAST_RT_ERROR();
   $LOOP
   {
      sb.SetLastChar($, 1 );
      HRESULT hResult = HND.PSLInterface -> GetArguments( sb.P($), sb.Capacity($) );
      if ( FAILED( hResult ) )
         return (PWSTR)SHLProps::CantExtractSLProperty($, L"WorkingDirectory", hResult );
      if ( sb.LastChar($) )
         return NL_RESET_ERROR( sb.TakePointerOwnership($) );
      if ( !sb.TwiceEnlargeNS($TCI) )
         NL_FAIL_LAST_RT_ERROR();
   }
}

//==================================================================================================
NL_API
PWSTR WINAPI SHLProps_GetIconLocation($CI_TAG, HANDLE instancePtr, PINT indexPtr $out )
                                               $needs_free nl_last_error
{
   NL_SHLP_API_ENTRY_CONST();
   $TCI_VERIFY_POINTER_ARG( indexPtr );
   WCHAR szPath[ INFOTIPSIZE ];
   *indexPtr = 0;
   HRESULT hResult = HND.PSLInterface -> GetIconLocation( $ARRAY_N_COUNT_OF(szPath), indexPtr );
   if ( FAILED( hResult ) )
      return (PWSTR)SHLProps::CantExtractSLProperty($, L"IconLocation", hResult );
   PWSTR pValue = $SZ::DuplicateNS($, szPath );
   return pValue ? NL_RESET_ERROR( pValue ) : NL_FAIL_LAST_RT_ERROR();
}

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