Archive

Posts Tagged ‘ShGetFileInfo’

Is a path shared?

November 14, 2007 Leave a comment

ShGetFileInfo helps us in finding out whether a given path is shared or not.

A wrapper function is given below…

bool IsPathShared( LPCTSTR lpctszPath_i )
{
   SHFILEINFO shFileInfo = { 0 };
   return ( SHGetFileInfo( lpctszPath_i, 
                           0, 
                           &shFileInfo, 
                           sizeof( shFileInfo ), 
                           SHGFI_ATTRIBUTES ) && ( shFileInfo.dwAttributes & SFGAO_SHARE ) == SFGAO_SHARE );
}// End IsPathShared

The key to this is the flag SFGAO_SHARE and SHGFI_ATTRIBUTES.

Cool isn’t it? 8)

Is a given file shortcut to some other file?

November 14, 2007 Leave a comment

ShGetFileInfo is the API that helps in finding out whether a file is a shortcut to some other file.

Here is wrapper function…

bool IsShortcut( LPCTSTR lpctszPath_i )
{
    SHFILEINFO shFileInfo = { 0 };
    return (( SHGetFileInfo( lpctszPath_i,
                             0,
                             &shFileInfo,
                             sizeof( shFileInfo ),
                             SHGFI_ATTRIBUTES )) && ( shFileInfo.dwAttributes & SFGAO_LINK ) == SFGAO_LINK );
}// End IsShortcut

Cool isn’t it? 8)

How to resolve a shortcut!

September 12, 2007 Leave a comment

Ever wondered how to resolve a shortcut! There is a hidden helper function if you are using MFC, it’s called AfxResolveShortcut. I customized it a bit, so that we can use it independently without MFC, and here is the finished product. 😉

BOOL ResolveShortcut( HWND hWnd_i,
                      LPCTSTR lpctszFileIn_i,
                      LPTSTR  lptszFileOut_o,
                      const int nPathLength_i )
{
    // Object for resolving link
    IShellLink* psl = NULL;
    *lptszFileOut_o = 0;   // assume failure            

    if (!hWnd_i)
        return FALSE;            

    SHFILEINFO info;
    if (( SHGetFileInfo(lpctszFileIn_i,
                        0,
                        &info,
                        sizeof(info),
                        SHGFI_ATTRIBUTES) == 0) || !(info.dwAttributes & SFGAO_LINK))
    {
        return FALSE;
    }            

    CoInitialize( 0 );            

    // Create instance
    HRESULT hCreateRes = CoCreateInstance( CLSID_ShellLink,
                                           0,
                                           CLSCTX_INPROC_SERVER,
                                           IID_IShellLink,
                                           reinterpret_cast<lpvoid *>( &psl ));
    if ( FAILED( hCreateRes ) || psl == NULL )
    {
        return FALSE;
    }            

    IPersistFile *ppf = NULL;
    if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf)))
    {
        USES_CONVERSION; // T2COLE needs this
        if ( ppf != NULL && SUCCEEDED( ppf->Load( T2COLE( lpctszFileIn_i ), STGM_READ)) )
        {
            /* Resolve the link, this may post UI to find the link */
            if ( SUCCEEDED(psl->Resolve( hWnd_i,
                 			    SLR_ANY_MATCH)))
            {
                psl->GetPath(lptszFileOut_o, nPathLength_i, NULL, 0);
                ppf->Release();
                psl->Release();
                CoUninitialize();
                return TRUE;
            }
        }           

        if (ppf != NULL)
            ppf->Release();
    }           

    psl->Release();
    CoUninitialize();
    return FALSE;           

}// End ResolveShortcut           

//Lets test the above code...
int main()
{
  TCHAR chBuffer[MAX_PATH] = { 0 };
  ResolveShortcut( hSomeWindow,
                   _T( "C:\\shortcut to msdev.lnk" ), // Make sure the shortcut is there
                   chBuffer,
                   MAX_PATH );
  MessageBox( hSomeWindow, chBuffer, _T( "Hurray shortcut resolved" ), MB_OK | MB_ICONINFORMATION );
  return 0;
}// End main

Type of Exe(Console, Windows, MS-DOS application).

May 28, 2007 3 comments

How to find out the type of an executable file, i.e. whether it’s a windows application or a console application or an MS-DOS application…

void GetFileType( LPCTSTR lpctszFilePath_i, CString& csFileType_o )
{
    // Get exe file type
    const DWORD dwRetVal = SHGetFileInfo( lpctszFilePath_i,
                                          FILE_ATTRIBUTE_NORMAL,
                                          0,
                                          0,
                                          SHGFI_EXETYPE );
    if( dwRetVal )
    {
        /*
           READ THIS!! dwRetVal is interpreted as follows...

           LOWORD = NE or PE and HIWORD = 3.0, 3.5, or 4.0  Windows application
           LOWORD = MZ and HIWORD = 0  MS-DOS .exe, .com, or .bat file
           LOWORD = PE and HIWORD = 0  Win32 console application
        */

        const WORD wLowWord =  LOWORD( dwRetVal );
        const WORD wHiWord = HIWORD( dwRetVal );
        const WORD wPEWord = MAKEWORD( 'P', 'E' );
        const WORD wMZWord = MAKEWORD( 'M', 'Z' );
        const WORD wNEWord = MAKEWORD( 'N', 'E' );

        // Read above comments to understand what's happening
        if( wLowWord == wPEWord || wLowWord == wNEWord )
        {
            if( wHiWord == 0 )
            {
                csFileType_o = _T( "Win32 Console Application" );
            }
            else
            {
                csFileType_o = _T( "Windows application" );
            }
        }
        else if( wLowWord == wMZWord && wHiWord == 0 )
        {
            csFileType_o = _T( "MS-DOS .exe, .com or .bat file" );
        }
        else
        {
            csFileType_o = _T( "Unknown file type" );
        }// End if
    }// End if
}// End GetFileType
%d bloggers like this: