Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / ATL
Article

CShortcut (Shell links to special folders)

Rate me:
Please Sign up or sign in to vote.
4.89/5 (45 votes)
13 May 20044 min read 173.2K   4.5K   90   33
This class enables you to build shortcuts to any File/Folder (like the Startmenu, SendTo-contextmenu...)

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</P>

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</P>

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


Written By
Web Developer
Germany Germany
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 | ;-)

Comments and Discussions

 
Questionwhy can't Resolve the IE shortcut? Pin
Member 423260024-Nov-08 0:45
Member 423260024-Nov-08 0:45 
GeneralAccessibility Options Shortcut Pin
Dabara4-Aug-08 21:15
Dabara4-Aug-08 21:15 
QuestionResolveLink?????? Pin
locoone28-Jun-07 17:49
locoone28-Jun-07 17:49 
GeneralGood submission but it is important to point out... Pin
sricard23-Oct-06 6:58
sricard23-Oct-06 6:58 
QuestionHow to create the office style shortcuts! Pin
Funfound21-Aug-06 0:20
Funfound21-Aug-06 0:20 
GeneralC++ Char help Pin
EIFNOBODY19-Jul-05 7:19
EIFNOBODY19-Jul-05 7:19 
GeneralRe: C++ Char help Pin
kcselvaraj31-Mar-06 23:55
kcselvaraj31-Mar-06 23:55 
GeneralC++ Char help Pin
Anonymous19-Jul-05 7:13
Anonymous19-Jul-05 7:13 
GeneralRe: C++ Char help Pin
Anonymous19-Jul-05 7:16
Anonymous19-Jul-05 7:16 
GeneralCreate subfolder Pin
Andromeda Shun19-Apr-05 4:31
Andromeda Shun19-Apr-05 4:31 
GeneralRe: Create subfolder Pin
Andromeda Shun19-Apr-05 4:43
Andromeda Shun19-Apr-05 4:43 
QuestionCreate shortcut in subfolder? Pin
Andromeda Shun18-Apr-05 4:55
Andromeda Shun18-Apr-05 4:55 
AnswerRe: Create shortcut in subfolder? Pin
Andromeda Shun18-Apr-05 23:53
Andromeda Shun18-Apr-05 23:53 
GeneralCompiler error Pin
Andromeda Shun18-Apr-05 3:10
Andromeda Shun18-Apr-05 3:10 
GeneralRe: Compiler error Pin
rootdial5-Jan-06 22:42
rootdial5-Jan-06 22:42 
GeneralStart In dir Pin
6-Sep-04 9:22
suss6-Sep-04 9:22 
GeneralRe: Start In dir Pin
Sinople23-May-05 3:47
Sinople23-May-05 3:47 
GeneralDelete Shortcut with any keyword Pin
Member 11853294-Sep-04 8:10
Member 11853294-Sep-04 8:10 
QuestionNice, and how to create a new folder? Pin
bendo11-Jul-04 1:19
bendo11-Jul-04 1:19 
AnswerRe: Nice, and how to create a new folder? Pin
Avinesh B20-Jul-04 11:00
Avinesh B20-Jul-04 11:00 
Generalthanks, regular codes Pin
simonchen.net25-May-04 6:25
simonchen.net25-May-04 6:25 
GeneralGood solution! Pin
spawn__19-May-04 8:43
spawn__19-May-04 8:43 
GeneralRe: Good solution! Pin
Thomas Latuske19-May-04 8:43
Thomas Latuske19-May-04 8:43 
Thank you!Blush | :O

best regards
Thomas
GeneralVery good !! Pin
WREY14-May-04 12:22
WREY14-May-04 12:22 
GeneralRe: Very good !! Pin
Thomas Latuske14-May-04 19:42
Thomas Latuske14-May-04 19:42 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.