获取快捷方式的文件路径

#include <shlobj.h>

bool ReadShortcut(LPWSTR lpwLnkFile, LPSTR lpDescFile)
{
    bool bReturn = true;
    IShellLink *pShellLink;

    if(bReturn)
    {
        bReturn = (CoInitialize(NULL) == S_OK);
        if(bReturn)
        {
            bReturn = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                    IID_IShellLink, (void **)&pShellLink) >= 0;
            if(bReturn)
            {
                IPersistFile *ppf;
                bReturn = pShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf) >= 0;
                if(bReturn)
                {
                    bReturn = ppf->Load(lpwLnkFile, TRUE) >= 0;
                    if(bReturn)
                    {
                        pShellLink->GetPath(lpDescFile, MAX_PATH, NULL, 0);
                    }
                    ppf->Release ();
                }
                pShellLink->Release ();
            }
            CoUninitialize();
        }
    }
    return bReturn;
}

// 测试代码:
char szBuf[MAX_PATH];
ReadShortcut(L"C:\\GLWorld.lnk", szBuf);
MessageBox(0, szBuf, "", 0);
View Code

 

posted @ 2013-10-29 10:59  陳さん様  阅读(242)  评论(0编辑  收藏  举报