Last updated:
31. October 2001

User Interface Programming

(Start main menu) Home ­ Articles ­ Book ­ Resources (End main menu)

resolveName.cpp: Listing 27


(Start sub-menu)

Table of Contents

Cover

Sample Chapter

Changelog


 

Feedback…

My blog »

(End sub-menu)

Programming Industrial Strength Windows (cover)

/*
 * $Header: /Book/resolveName.cpp 11    11.07.01 15:20 Oslph312 $
 */
 
#include "precomp.h"
#include "os.h"
#include "utils.h"
#include "trace.h"
 
#include "AutoComReference.h"
 
 
#define FILE_NOT_FOUND   MAKE_HRESULT( 1, FACILITY_WIN32, ERROR_FILE_NOT_FOUND )
#define DEVICE_NOT_READY MAKE_HRESULT( 1, FACILITY_WIN32, ERROR_NOT_READY      )
 
/**
 * If pszSrc refers to a link, pszDst will be the file that
 * the link refers to. If pszSrc does not refer to a link,
 * pszDst will be equal to pszSrc. It is OK if these point
 * to the same buffer. pszDst should have a length of at least
 * MAX_PATH + 1 characters.
 */
bool resolveName( LPTSTR pszDst, LPCTSTR pszSrc ) {
 
   assert( isGoodStringPtr( pszDst ) );
   assert( isGoodStringPtr( pszSrc ) );
 
   PATHNAME szFullPathName = { 0 };
   LPTSTR pszFilePart = 0;
   const DWORD dwChars = GetFullPathName(
      pszSrc, dim( szFullPathName ), szFullPathName, &pszFilePart );
   if ( 0 < dwChars ) {
      pszSrc = szFullPathName;
   }
 
   if ( pszDst != pszSrc ) {
      _tcscpy( pszDst, pszSrc ); // assume failure, use org file name
   }
 
   bool isShortcut = false;
   try {
      // Get a pointer to the IShellLink interface.
      AutoComReference< IShellLink >
         psl( CLSID_ShellLink, IID_IShellLink );
      AutoComReference< IPersistFile >
         ppf( IID_IPersistFile, psl );
      PATHNAMEW wsz = { 0 };
 
#ifdef UNICODE
      _tcscpy( wsz, pszSrc );
#else
      multiByteToWideChar( pszSrc, wsz );
#endif
 
      // Load the shortcut. Fails if non-existens or non-link.
      // Succeeds if empty file, so we check for this below.
      HRESULT hres = ppf->Load( wsz, STGM_READ );
      if ( !SUCCEEDED( hres ) ) {
         // TODO: May be 0x80070015, Device is not ready. Handle this!
         assert( E_FAIL == hres || FILE_NOT_FOUND == hres || DEVICE_NOT_READY == hres );
      } else {
         // Resolve the link.
         hres = psl->Resolve( HWND_DESKTOP, SLR_ANY_MATCH );
         if ( SUCCEEDED( hres ) ) {
            // Get the path to the link target.
            PATHNAME szGotPath = { 0 };
            WIN32_FIND_DATA wfd = { 0 };
            psl->GetPath( szGotPath, MAX_PATH, &wfd, SLGP_SHORTPATH );
            // Happens if file not found.
            if ( 0 != szGotPath[ 0 ] ) {
               isShortcut = true;
               _tcscpy( pszDst, szGotPath );
            }
         }
      }
   }
   catch ( const ComException& x ) {
      trace( _T( "resolveName: ComException: %s\n" ), x.what() );
   }
 
   return isShortcut;
}
 
// end of file

(Start bottom menu)

TopHomeArticlesBookResources
Win­dows De­vel­oper Maga­zineR&D BooksCMP Books
Amazon.comAmazon.co.ukContact Petter Hesselberg

(End bottom menu)

Chapter 7 Listings

createNewFile.cpp

getLongPathName.cpp

resolveName.cpp

activateOldInstance.cpp

ArgumentList.h

ArgumentList.cpp

init.cpp