How to find out if ".lnk" file (windows shortcut) is linking to ".exe" file

Started by
12 comments, last by Colin Jeanne 16 years, 9 months ago
Does anybody know how to find out if .lnk file is linking to an .exe (Application file) For example: Calculator.lnk is actually linking to an Application file in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories, and the application is called Calculator.exe I am programming using Win32 and C++. Any help would be appreciated. Thank you.
Advertisement
all .lnk files link to an .exe. it's what they are: shortcuts to .exe files

at least according to this...
http://filext.com/file-extension/LNK

Do you mean to ask: how do i find out what EXE a LNK file links to?

-me
Not all shortcuts are to executables. You can have shortcuts to folders, documents, shell objects, etc. If you want to find what a given link refers to you can use IShellLink::Resolve(). It's not pretty though. Q179904 gives an example of how to use IShellLink.
It's not too bad of a process to resolve a link:

IShellLink *psl;HRESULT hr = CoCreateInstance(__uuidof(IShellLink), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl));if (SUCCEEDED(hr)){   IPersistFile *ppf;   hr = psl->QueryInterface(IID_PPV_ARGS(&ppf));   if (SUCCEEDED(hr))   {      hr = ppf->Load(pszPathToLnkFile, STGM_READ);      if (SUCCEEDED(hr))      {         hr = psl->Resolve(NULL, SLR_NOLINKINFO | SLR_NO_UI | SLR_NOUPDATE | SLR_NOSEARCH | SLR_NOTRACK);         if (SUCCEEDED(hr))         {            WCHAR szPath[MAX_PATH];            hr = psl->GetPath(szPath, ARRAYSIZE(szPath), NULL, 0);            if (SUCCEEDED(hr))            {               // Do whatever with the path            }         }      }      ppf->Release();   }   psl->Release();}
Colin,

Thank you for your sample code.

I have a question about the code, though, where is it in there actually trying to to detect whether a '.lnk' file is linking to a '.exe' file or some other file extension?

Or do you mean to find the path where the .lnk file is linking and in the "// Do whatever with the path" section, after we figure out the path, we have to figure out of the last 3 letter of the file whether it is 'exe' or something else?
The part where it says "//Do whatever with the path" is where you determine whether the target is an EXE or something. I recommend using PathIsExe().
There's an error when I use "IID_PPV_ARGS" , it says: error C3861: 'IID_PPV_ARGS': identifier not found

I tried including "objbase.h", but it still doesn't work. Do you have any idea what file I need to include to use "IID_PPV_ARGS"?

Thanks.
You need to have the latest Platform SDK. Clicky.
I have installed the latest Windows SDK, but I still get the same error message.

error C3861: 'IID_PPV_ARGS': identifier not found
It should be in objbase.h. If not there is a newer SDK than you have and if you still cant find it, here it is.

This topic is closed to new replies.

Advertisement