windows read the target of shortcut file in c++

前端 未结 2 1132
無奈伤痛
無奈伤痛 2020-12-17 02:46

How to read the target of shortcut file on windows. Tried using boost::read_symlink which throws exception saying \"the file or directory is not a reparse point\" message.

2条回答
  •  臣服心动
    2020-12-17 02:59

    Here's more compact version of David's code, with ATL (included with Visual Studio).

    #define CHECK( hr ) { const HRESULT __hr = ( hr ); if( FAILED( __hr ) ) return __hr; }
    
    HRESULT resolveShortcutTarget( HWND wnd, const CString& lnk, CString& target )
    {
        // Get a pointer to the IShellLink interface. It is assumed that CoInitialize has already been called. 
        CComPtr psl;
        CHECK( psl.CoCreateInstance( CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER ) );
    
        // Get a pointer to the IPersistFile interface. 
        CComPtr ppf;
        CHECK( psl->QueryInterface( IID_PPV_ARGS( &ppf ) ) );
    
        // Load the shortcut. 
        CHECK( ppf->Load( lnk, STGM_READ ) );
    
        // Resolve the link. 
        CHECK( psl->Resolve( wnd, 0 ) );
    
        // Get the path to the link target. 
        const HRESULT hr = psl->GetPath( target.GetBufferSetLength( MAX_PATH ), MAX_PATH, nullptr, 0 );
        target.ReleaseBuffer();
        return hr;
    }
    

提交回复
热议问题