Getting Shortcuts (.lnk) - Target and Arguments
Posted
by Firo Firo
on February 13th, 1999
I saw Michael Taupitz's idea of creating Links (shortcuts) and I realized that there are many times when we stumble across a link in command prompt mode and we have no idea what this .lnk file points to.
Here's a function I wrote which returns the Target and Arguments of a .lnk file in a CString. Hope you find it helpful.
CString GetShortcutTarget(const CString LinkFileName)
{
HRESULT hres;
CString Link, Temp = LinkFileName;
Temp.MakeLower();
if (Temp.Find(".lnk")==-1) //Check if the name ends with .lnk
Link = LinkFileName + ".lnk"; //if not, append it
else
Link = LinkFileName;
CString Info;
Info.Empty();
IShellLink* psl;
//Create the ShellLink object
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*) &psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
//Bind the ShellLink object to the Persistent File
hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);
if (SUCCEEDED(hres))
{
WORD wsz[MAX_PATH];
//Get a UNICODE wide string wsz from the Link path
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, Link, -1, wsz, MAX_PATH);
//Read the link into the persistent file
hres = ppf->Load(wsz, 0);
if (SUCCEEDED(hres))
{
//Read the target information from the link object
//UNC paths are supported (SLGP_UNCPRIORITY)
psl->GetPath(Temp.GetBuffer(1024), 1024, NULL, SLGP_UNCPRIORITY);
Temp.ReleaseBuffer();
Info = Temp;
//Read the arguments from the link object
psl->GetArguments(Temp.GetBuffer(1024), 1024);
Temp.ReleaseBuffer();
Info += " " + Temp;
}
}
}
psl->Release();
//Return the Target and the Argument as a CString
return Info;
}
Date Posted: 13/02/99

Comments
How about a DLL to do it?
Posted by Legacy on 05/04/2001 12:00amOriginally posted by: Daniel Foote
Hello all.
Just wondering... I use a compiler known as MinGW, as VC++ is a little expensive, but found a lot of the information on this site very useful.
However, in the case of creating and resolving links, my compiler doesn't do well. It has problems with COM. So far my attempts to create a .LNK resolver have been unsuccessful.
What I was wondering was if anyone could possibly make these functions into a DLL which could be used. I know it's possible, and it'd be great to use one.
If you can help, or have done, please inform me at d_foote@spyring.com. It'll eventually be used in a freeware project of mine, whenever it gets released.
Thankyou,
ReplyDaniel.
Trouble with links to MS system EXEs
Posted by Legacy on 03/18/2001 12:00amOriginally posted by: Steve Carpenter
I have an app that uses essentially the same code as the IShellLink example provided here. And it works great all the time ----- except when I try to use a link from IE, or one of the other Desktop System deities.
My app allows the user to dragdrop a file icon (from desktop or Windows Explorer) to the client area of an MDI, thus creating a child windowm, or onto an existing child window client area, allowing the creation of file groups that can be used as optional startup sets.
I noticed right away that IE would not dragdrop (would only display the barred circle). This was an immediate yank on my chokechain. But, I created a shortcut on the desktop and was able to drag that.
The killer was that GetPath can't deliver the file object (IEXPLORER.EXE) as it does so well on common files. Oddly, after a week or two, it began to succeed in this also. Then last week I had a HD crash and had to format and reinstall Windows. Then I noticed the old reluctance had reappeared.
Here's a clue: I made a shortcut to IE that that would run IE when clicked, but when the accel I chose (ctrl+alt+e) was used, the thing opened MyComputer! (I subsequently learned that was because of an empty string.) Then I went on line and got the latest IE patch, and now the keyboard shortcut works, and I can drag onto the bare client of my app (in the interim, I could only drop to child window clients).
But no way can I get GetPath() to return the base file path of IEXPLORER.EXE. GetPath() returns an error of 1, which Borland's func OLEError(int errnum) reports as "Incorrect Function", but it works fine for every other link but these few.
Any clues?
Much obliged in advance,
JSC
ReplyHow to run shortcut from my app, just like ordinary .EXE file?
Posted by Legacy on 12/15/1999 12:00amOriginally posted by: Slava Herasimau
I need to run cmd.exe in initially minimized mode. It helps avoid blinking of creen when you run different files.
Any aideas?
ReplyCoInitialize needed?
Posted by Legacy on 07/18/1999 12:00amOriginally posted by: John McManus
Replycall to ppf->Load(wsz, 0); alwayse return -21... something something
Posted by Legacy on 07/13/1999 12:00amOriginally posted by: Eforg
call to ppf->Load(wsz, 0); alwayse return -21... something something!
What is causing that?
Eforg
ReplyOther Link properties
Posted by Legacy on 02/22/1999 12:00amOriginally posted by: Eric Crahen
This is good place to look other link properties like working dir, target file etc. But what exactly dictates where, as in co-ordinates, on the desktop your icons are?
- Eric
ReplyGetting a Shortcut (.lnk) - Target and Arguments
Posted by Legacy on 02/22/1999 12:00amOriginally posted by: Mario Mercea
CoCreateInstance returns error -2147221008 and it exists from GetShortcutTarget ...
ReplyWhat can cause this error ?
ppf->Release()?
Posted by Legacy on 02/14/1999 12:00amOriginally posted by: Anatoly Ivasyuk
There should be a call to ppf->Release() at the end of the the second if(SUCCEEDED(hres)) block, otherwise you will cause a leak.
Reply