EarthWeb

Developer.com
CodeGuru Sites
Visual C++/MFC
C# and.NET
Visual Basic

Discussion Boards
Books on.NET
Book Reviews
Newsletters (subscribe)
Newsletters (archived)

Article Sections
C++
algorithms & formulas
c++ & mfc
date & time
string
COM-based Technologies
atl & wtl
com & activex
com+
shell programming
Controls
button control
combobox
edit control
imagelist control
listbox control
listview control
menu
other controls
property sheet
rich edit control
static control
status bar
toolbar
treeview control
Data
database
miscellaneous
Frameworks
ui & printing frameworks
Graphics & Multimedia
bitmaps & palettes
directx
gdi
multimedia
opengl
Internet & Networking
ie programming
internet protocols
isapi
network protocols
Miscellaneous
miscellaneous
Visual Studio
debugging
add-ins & macros
editor tips
Windows Programming
ce
clipboard
dll
file & folder
help systems
printing
win32
system
Windows & Dialogs
console
dialog
docking window
doc/view
splitter
Interact
Newsletters
Guestbook
Recommend Us!
About Us

[Internet Jobs]
   

Getting a Shortcut (.lnk) - Target and Arguments


This article was contributed by Firo.

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;
	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:

Add Comment





EarthWeb is a service of INT Media Group, Incorporated.
Copyright 2001 INT Media Group, Incorporated. All Rights Reserved.
Feedback,   Advertising Info,   Legal NoticesLicensing, Reprints, & Permissions,   Privacy Policy.
http://www.internet.com/