Last updated:
31. October 2001
(Start sub-menu)
(End sub-menu)
/* * $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)
Top •
Home
• Articles
• Book
• Resources
Windows Developer Magazine
• R&D Books
• CMP Books
Amazon.com
• Amazon.co.uk
• Contact Petter Hesselberg
(End bottom menu)
resolveName.cpp