CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> COM-based Technologies >> Shell Programming


Creating File Links
Rating: none

Michael Taupitz (view profile)
February 7, 1999

For a new Project I needed the function, to create Links to a File (in Startmenu and Autostart). But that is the Point to take a look on the MFC-Classes. But there is not so many Info about. The Old-Version (for Win 3.11) with DDE was nice and runs fine. But that is not the way to write it under WinNT 4.0.

Okay, the right book at the right time, and every problem can be solved...
(continued)



The Solution I found under a small chapter about IShellLink. But not much Info, and so i tried it out.

At First is the boring COM -

	CoInitialize(NULL);
	hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
	IID_IShellLink, (LPVOID*) &psl);

If it was successful, you can set Path, Description and so on. But to create the link in real (on Harddisk) we need IPersistFile

	hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);

and save the File...

	hres = ppf->Save(in WideChar>, TRUE);

Thats all.

So, if anybody need it complete:

// Creating a Link with IShellLink
// strPathObj  = Object with Path
// strPathLink = Path where the Link will appear
// strDesc = Link-Decription (the text under the Icon)
// CoInitialize(NULL) wird vorausgesetzt !!

HRESULT MyMainClass::CreateLink(const CString strPathObj,const CString
strPathLink, const CString strDesc)
{
HRESULT hres;
IShellLink* psl;

	CString strMyPath = strPathLink;

	hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*) &psl);
	if (SUCCEEDED(hres))
	{
		IPersistFile* ppf;

		psl->SetPath(strPathObj);
		psl->SetDescription(strDesc);

		hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);

		if (SUCCEEDED(hres))
		{
			strMyPath += ".lnk";  // Important !!!

			WORD wsz[MAX_PATH];
			MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, strMyPath, -1, wsz,
MAX_PATH);

			hres = ppf->Save(wsz, TRUE);
			if (hres != S_OK )
				MessageBox(NULL, "IPersistFile->Save() Error", "Error", MB_OK);

			ppf->Release();
		}
		psl->Release();
	}
	return hres;
}
Okay, lets take a look on the Function above : What you need to make the link is only to know where is the File for the Link, where you want to create the Link, and what a Name should appear under the Icon. The Path where comes in the Link must be added with the Name for the Link ( under the Icon) and needs (its Important) a .lnk extension.
C:\\.lnk

Now call the Function :
FileLink([Source-Path with File],
	 [Destination-Path with an allowed Name and .lnk as Extension]
         (like C:\[Path]\[Name you want].lnk ]);

And if you would like to try a complete Sample you can download FileLinkDemo.zip (only Win NT4.0 tested) (19KB)

Date Posted: 10/5/98

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Creating PIF's - Legacy CodeGuru (10/16/2000)
Library problem - Legacy CodeGuru (10/05/2000)
Problems with handles! -> Memory overflow! - Legacy CodeGuru (03/29/2000)
How to indicate position on the desktop when creating a shortcut! - Legacy CodeGuru (03/26/2000)
creating internet shortcut - Legacy CodeGuru (12/22/1999)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES