★ wanayoo — archive 1999 http://codeguru.developer.com/shell/filelinks.shtmlNouvelle recherche | Portail wanayoo
EarthWeb
Developer.com Click Here!
Click Here!
Find:
 

EXPERT SEARCH
SEARCH THE IT WEB -----
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Contest
mfc codeguru
submission guidelines
 
Book
thinking in c++
stl programmer's guide
reviews
 
Interact
forums
newsgroups
guest book
jobs
jokes
open faq
what's new
 
Other
about
links
newsletters
privacy
tools
 

-----
Visual C++ by E-mail:

Get the weekly e-mail highlights on
Visual C++!
-----



EarthWeb Sites:
IT Management
Networking & Telecommunications
IT Jobs
Software Development
Hardware & Systems
Commerce
Community

EarthWeb Worldwide

   

Creating File Links


This article was contributed by Michael Taupitz.

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

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

Comments:

Add Comment

   

Give them a visit Click Here!

Click Here!

INSTANT EXPERTS!
Savvy IT Pros report on SupportSource. Take a look inside the largest vault of multi-vendor computing specs ever assembled. Click here for an In-depth Report,.

For tons of Windows technology resources, check out the Windows Programming directory.

Searching the Web for tech topics just got better with
TechCrawler where all results come only from popular technical sites.

WINNER!!!
Congratulations to James Hole, of New Mexico! The grand prize winner of EarthWeb's Flatten the Obstacles Sweepstakes and now a proud Hummer(R) owner.

Give them a visit Click Here!

Click Here!

Click Here!

Click Here!

Home About Us Search Subscribe Advertising Info Contact Us FAQs
Use of this site is subject to certain Terms & Conditions.
Earthweb's statement regarding our users' right to privacy.
Copyright ©1996-2000 EarthWeb Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited.