Click here to Skip to main content
Click here to Skip to main content

CShortcut (Shell links to special folders)

By , 13 May 2004
 
Prize winner in Competition "MFC/C++ Apr 2004"

Sample Image - CShortcut.gif

Introduction

This class enables you to build Shell links (shortcuts) easily like you see everywhere in Windows (e.g.: on the Desktop, in the Context menu's "SendTo"-entry, in the Startmenu...). You are also able to give a description or command-line arguments to the link. It is really a snap to make shortcuts with this class. Have a look at the demo project to see how easy it is!

Some words on the Demo project

  • The Icon will change each time a shortcut is written and the programm is running. The Icons are leeched from the shell32.dll.
  • The name of the link that is written by this demo is always "Test Link" no matter in which folder the link is created.
  • When the Program starts, it detects if there is a shortcut. If there is one, the text
    beside the option button will be changed and the textcolor will also be changed to Red. If no, the text will be blue. The same occurs when a link is written or deleted.
  • If you create a link in the "SendTo" menu, you can test it like this:
    • create a shortcut in the SendTo context menu
    • open the explorer, right-click on a file and choose the SendTo menu entry
    • now another menu pop's up and you should choose "Test Link"
    • the CShortcut Demo starts and a Messagebox shows you the path and the filename of the file you "SendTo".

SendTo Messagebox Img

Using the code

1. add the files Shortcut.h and Shortcut.cpp to your project and Include the header

#include "Shortcut.h"

2. create a variable of class CShortcut. ie:

m_pShortcut // a pointer to CShortcut

3.1. Call the function SetCmdArguments in case you need command-line arguments

SetCmdArguments(CString sArg)

the CString is/are the argument(s) you need to call with your file

3.2 Call the function CreateShortCut to create a shortcut (the function name says it ;-)

CreateShortCut(CString LnkTarget,
               CString LnkName,
               UINT SpecialFolder,
               CString LnkDescription,
               CString IconLocation,
               UINT IconIndex)
  • LnkTarget the File/Folder the link belongs to
  • LnkName the name of the ShortCut
  • SpecialFolder where to put the shortcut to (see below or for more folders see
    MSDN --> SHGetSpecialFolderLocation)
  • LnkDescription an application can use it to store any text information and can
    retrieve it with "IShellLink::GetDescription"
  • IconLocation path to the file where the icon is located that should be used. Can be
    an empty string
  • IconIndex the index of the icon in the file

    Definitions for SpecialFolder's:

Definition

Description

CSIDL_SENDTO

SendTo Menu/Folder

CSIDL_DESKTOP

Desktop for current User

CSIDL_COMMON_DESKTOP

Desktop for all Users

CSIDL_STARTUP

Autostart for current User

CSIDL_COMMON_STARTUP

Autostart for all Users

CSIDL_STARTMENU

Start-menu for current User

CSIDL_COMMON_STARTMENU

Start-menu for all Users

CSIDL_PROGRAMS

Programs-menu for current User

and many more...

4. You want to delete a Shortcut? O.K. read on. Just use DeleteShortCut

DeleteShortCut(CString LnkName, UINT SpecialFolder)
  • LnkName the name of the Shortcut
  • SpecialFolder again a define for a special folder (see CreateShortCut)

5. You want to detect a shortcut? Then use isLinkAvailable

isLinkAvailable(CString LnkName, UINT SpecialFolder)
  • LnkName the name of the Shortcut
  • SpecialFolder again a define for a special folder (see CreateShortcut)

6. Now you can resolve and retrieve the description of a shortcut, use:

ResolveLink(CString LnkName,
            UINT SpecialFolder,
            HWND hwnd,
            CString &LnkPath,
            CString &LnkDescription)
  • LnkName the name of the ShortCut
  • SpecialFolder the location of the shortcut
  • hwnd The handle of the parent window for any message boxes that may be displayed by the shell.
  • &LnkPath reference to a string that receives the path to the object
  • &LnkDescription reference to a string that receives the description of the link

7. There are also two private helper routines that are used internal:

  • GetSpecialFolder This routine is a helper that finds the path to the special folder
  • ShortToLongPathName This routine is a helper that builds a long path from 8+3 one. I know that there exists an function called GetLongPathName but I'm using half of my time an "old" NT4 system and there is this function not available!

Now you are ready to build links to everything on your computer. >B-}

Credits

I would like to thank the programmer who made this class possible due to his or her good
IShellLink implementation! I really don't remember where I found it. Sorry! And Michael Dunn for his great article Introduction to COM - What It Is and How to Use It

The reason for this:

I was in the need to give the user of a little application of mine an option to send files
through the right-click "SendTo" menu to my Program. After searching for a few hours I've had enough material and information to bring up this class (my first contact with COM Technology).

You can put executables not only in the "SendTo" menu but also folders which can be useful to search and collect some files and Zip them up later (for example). You see there are many useful things todo with links. Another possibility is to link something to the control panel. Or you can...

History

12.05.2004 - routine to resolve ShellLinks added

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Thomas Latuske
Web Developer
Germany Germany
Member
My name is Thomas, I'm born on January the 11th in
1970, right now I'm working in the Quality department
of a big Pipe mill as a Technician.
My hobbies are my girl friend, my car, RC-Planes and
Computers. I begun with VC++ some time ago and now
Programming is like a drug to me (I'm still a
beginner). I want to learn it all in a blink of an
eye Wink | ;-) but i know that this is not possible. It's
real fun for me and I do small Programms for my own
use.
O.K. enough written..... I need my Time to debug
everything that crosses my way! Wink | ;-)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionwhy can't Resolve the IE shortcut?memberMember 423260024 Nov '08 - 0:45 
Other Question: When I Create a ie shortcut, don't use ShellExecuteEx open it?
GeneralAccessibility Options ShortcutmemberDabara4 Aug '08 - 21:15 
Hi
 
How can I create Accessibility Options Shortcut using ShellProgramming.
 
dabara

QuestionResolveLink??????memberlocoone28 Jun '07 - 17:49 

CString linkpath;
CString LnkDescription;
if (m_shortcut->isLinkAvailable(ff.GetFileTitle(), CSIDL_RECENT))
{
MessageBox("available");
m_shortcut->ResolveLink(ff.GetFileTitle(), CSIDL_RECENT, m_hWnd, linkpath, LnkDescription);
MessageBox(linkpath);
}

MessageBox("available") goes off
MessageBox(linkpath) is blank
anyone know why?
GeneralGood submission but it is important to point out...membersricard23 Oct '06 - 6:58 
In my travels through the internet searching for an answer of how to create a sendto shortcut, this is pretty much the standard of how people suggest to do it. What they do not point out is that if you are trying to submit a large number of files from windows explorer to your application, this method simply is not good enough. I feel that it should be pointed out that the files are posted to the application as an argument, and thus are limited to 256 characters.
 
Just some information to think about when implementing the sendto shortcut. I am currently in search of a more robust solution to creating a sendto shortcut and desktop shortcut that allows an unlimited amount of files.
 
The following article provides some good information, but I am a VB.NET programmer, so this will consume a lot of my time doing the translation. If anyone has a solution, please let me know!
 
http://www.codeproject.com/shell/ShellExtGuide6.asp
 
Seth
QuestionHow to create the office style shortcuts!memberFunfound21 Aug '06 - 0:20 
About the shortcut of microsoft word 2003,
we can't see the target exe file path.
 
why and how?
 
thx!

GeneralC++ Char helpmemberEIFNOBODY19 Jul '05 - 7:19 
Hi. I am kinda new to c++ and i want to make a program that asks for a password and if u enter "test" its gonna say correct password or else it says "wrong password!"
heres what i came up with -- but it doesnt work:
 
#include iostream.h
int main () {
char a[4];
cout << "Enter password:" << endl;
cin >> a;
if (a == "test")
{
cout << "Correct password!" << endl;
}
else
{
cout << "Incorrect password!" << endl;
}
system("PAUSE");
return 0;
}
 

if u enter "test" in there it says wrong password. any1 got any suggestions of how to fix it? if so plz reply id be greateful!

GeneralRe: C++ Char helpmemberkcselvaraj31 Mar '06 - 23:55 
try to use strcmp()or char by char comparision
 
i.e
 
if(a[0]=='t' && a[1]=='e' && a[2]=='s' && a[3]=='t')
or
if (strcmp(a,"test")==0)
 
Note: use the size of array one greater than number of character reading . bcz. Last char for '\0'.Roll eyes | :rolleyes:
 
K.C.S
GeneralC++ Char helpsussAnonymous19 Jul '05 - 7:13 
Hi. I am kinda new to c++ and i want to make a program that asks for a password and if u enter "test" its gonna say correct password or else it says "wrong password!"
heres what i came up with -- but it doesnt work:
 
#include
int main () {
char a[4];
cout << "Enter password:" << endl;
cin >> a;
if (a == "test")
{
cout << "Correct password!" << endl;
}
else
{
cout << "Incorrect password!" << endl;
}
system("PAUSE");
return 0;
}
 

if u enter "test" in there it says wrong password. any1 got any suggestions of how to fix it? if so plz reply id be greateful! Big Grin | :-D
GeneralRe: C++ Char helpsussAnonymous19 Jul '05 - 7:16 
im sorry, its me again. first line of the program == #include
GeneralCreate subfoldermemberAndromeda Shun19 Apr '05 - 4:31 
Hi!
 
Just one small addition (my code might not be very good, though): You should check if the given folder exists and if not create it. I've done it like that:
 
// your code
if(!GetSpecialFolder(SpecialFolder, sSpecialFolder))
return FALSE;
sSpecialFolder += LnkName + "." + "lnk";
 
// I added this section:
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
_splitpath(sSpecialFolder, drive, dir, fname, ext );
CString strLinkFolder = CString(drive) + dir; // get the link folder
 
char cOldDir[_MAX_PATH];
bool bExistsDir = false;
if (strLinkFolder.Right(1)=="\\")
strLinkFolder=strLinkFolder.Left(strLinkFolder.GetLength()-1);
::GetCurrentDirectory(_MAX_PATH,cOldDir);
if(SetCurrentDirectory(strLinkFolder)) // try to change to the link folder
bExistsDir = true; // if this works the folder exists
SetCurrentDirectory(cOldDir);
if (!bExistsDir) // otherwise it must be created
CreateDirectory(strLinkFolder, NULL);
// end of added section
 
// again your code
if(LnkTarget == "_this")

 
I would appreciate comments and improvements for this code.
 
CU, Andromeda Shun

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 14 May 2004
Article Copyright 2004 by Thomas Latuske
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid