Andu
November 28th, 2006, 04:17 AM
Is there a way to access a file in a .lnk subdirectory ? Right now, I see it as a regular file (that pops a window when executed), but I need to at least retrieve the full path inside the file and I'm having a hard time finding documentation on the subject.
Thanks in advance
riscutiavlad
November 28th, 2006, 05:08 AM
Try reading this: http://mediasrv.ns.ac.yu/extra/fileformat/windows/lnk/shortcut.pdf
Andu
November 28th, 2006, 07:21 AM
Thanks, exactly what I needed.
Mike Harnad
November 28th, 2006, 11:10 AM
You need to resolve the shortcut (.lnk) to proceed further. Below is an MFC code snippet I used to do this. You should be able to adapt it to your needs.
//
// This method resolves Shell shortcuts (.lnk files).
//
// Input: szLnkFile - the name of the shortcut file.
//
// Returns: actual file system path, or, "" if no path available.
//
CString CFileDlg::SHResolveShortcut(CString szLnkFile)
{
WCHAR wszLnkFile[MAX_PATH] = {0}; // wide-char lnk file
IShellLink* pShellLink = NULL; // shell link interface ptr
IPersistFile* pPF = NULL; // persist file interface
CString lnkFile = szLnkFile; // adjusted lnk name
// add the suffix, if necessary.
lnkFile.MakeUpper();
if (lnkFile.Find(".LNK") == -1)
lnkFile+= ".lnk";
// create the appropriate COM server.
HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink,
reinterpret_cast<LPVOID*>(&pShellLink));
if(FAILED(hr))
return "";
// get the IPersistFile interface to load the LNK file.
hr = pShellLink->QueryInterface(IID_IPersistFile, reinterpret_cast<LPVOID*>(&pPF));
if(FAILED(hr))
{
pShellLink->Release();
return "";
}
// load the shortcut (.lnk) file.
MultiByteToWideChar(CP_ACP, 0, lnkFile, -1, wszLnkFile, MAX_PATH);
hr = pPF->Load(wszLnkFile, STGM_READ);
if(FAILED(hr))
{
pPF->Release();
pShellLink->Release();
return "";
}
// resolve the link.
hr = pShellLink->Resolve(NULL, SLR_ANY_MATCH | SLR_NO_UI);
if(FAILED(hr))
{
pPF->Release();
pShellLink->Release();
return "";
}
// get the true path name.
TCHAR szPath[MAX_PATH] = {0};
WIN32_FIND_DATA wfd;
pShellLink->GetPath(szPath, MAX_PATH, &wfd, SLGP_SHORTPATH);
// cleanup.
pPF->Release();
pShellLink->Release();
CString path = szPath;
return path;
}