Sign in to follow this  

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

This topic is 3830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I have installed the latest Windows SDK, but I still get the same error message.

error C3861: 'IID_PPV_ARGS': identifier not found

Share this post


Link to post
Share on other sites
Quote:
Original post by Colin Jeanne
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().


Heh, nice disclaimer that documentation has:

Quote:

Note This function is available through Microsoft Windows XP Service Pack 2 (SP2) and Windows Server 2003. It might be altered or unavailable in subsequent versions of Windows.


Enough to make me hesitant to use it. Did Microsoft stop caring about backwards compatibility?

Share this post


Link to post
Share on other sites
I ended up adding the IID_PPV_ARGS macro to make the code to work.

I have a question on this line of code:

hr = ppf->Load(pszPathToLnkFile, STGM_READ);



I need LPCOLESTR as the data type for pszPathToLnkFile, however the data I have is std::string.
Do you know how to convert from std::string to LPCOLESTR? I tried googling it, but couldn't find anything very helpful.

Thank you in advance.

Share this post


Link to post
Share on other sites
An LPCOLESTR is the same as LPCWSTR. LPCWSTR is the same as const WCHAR *. Basically, std::string wont cut it since it uses char internally. Instead, use std::basic_string<WCHAR> and then you can use the .c_str() method.

Share this post


Link to post
Share on other sites

This topic is 3830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this