Getting Shortcuts (.lnk) - Target and Arguments

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Environment: VC6.0 SP2

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;
	if (Temp.Find(".lnk")==-1)           //Check if the name ends with .lnk
		Link = LinkFileName + ".lnk";   //if not, append it
		Link = LinkFileName;

	CString Info;
	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);
				Info = Temp;

				//Read the arguments from the link object
				psl->GetArguments(Temp.GetBuffer(1024), 1024);
				Info += " " + Temp;
	//Return the Target and the Argument as a CString
	return Info;

Date Posted: 13/02/99


  • How about a DLL to do it?

    Posted by Legacy on 05/04/2001 12:00am

    Originally 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 It'll eventually be used in a freeware project of mine, whenever it gets released.


  • Trouble with links to MS system EXEs

    Posted by Legacy on 03/18/2001 12:00am

    Originally 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,


  • How to run shortcut from my app, just like ordinary .EXE file?

    Posted by Legacy on 12/15/1999 12:00am

    Originally 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?

  • CoInitialize needed?

    Posted by Legacy on 07/18/1999 12:00am

    Originally posted by: John McManus

       I was having trouble with Firo's implementation of
    GetShortcutTarget. Specifically, the CoCreateInstance call
    was failing. Being new to COM, the registry, and VisualC++,
    I despaired of finding a solution.
    However, just 3 minutes after sending an email to Firo
    asking for help, I found a snippet of code under another
    topic that called CoInitialize. That rang a bell, because
    Firo's article said something about the need to initialize
    the OLE libraries--"What if there's some sort of COM
    initialization that must be done, too?"
    I don't know why it works, but it does!!
    FWIW, I was working in a Console app with MFC support,
    just a few lines to test the function. Maybe a Windows app
    would be different?

    CString GetShortcutTarget(const CString LinkFileName)

    HRESULT hres;

    // Code I added.
    hres = CoInitialize(NULL);
    if( !SUCCEEDED(hres))
    return "Failed to CoInitialize\n";

    [remainder of Firo's excellent function snipped]

    Thanks, Firo! And all the other contributors to the thread!


  • call to ppf->Load(wsz, 0); alwayse return -21... something something

    Posted by Legacy on 07/13/1999 12:00am

    Originally posted by: Eforg

    call to ppf->Load(wsz, 0); alwayse return -21... something something!

    What is causing that?


  • Other Link properties

    Posted by Legacy on 02/22/1999 12:00am

    Originally 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

  • Getting a Shortcut (.lnk) - Target and Arguments

    Posted by Legacy on 02/22/1999 12:00am

    Originally posted by: Mario Mercea

    CoCreateInstance returns error -2147221008 and it exists from GetShortcutTarget ...
    What can cause this error ?

  • ppf->Release()?

    Posted by Legacy on 02/14/1999 12:00am

    Originally 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.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date