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

CShortcut (Shell links to special folders)

, 13 May 2004
Rate this:
Please Sign up or sign in to vote.
This class enables you to build shortcuts to any File/Folder (like the Startmenu, SendTo-contextmenu...)
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 Wink | ;-)

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
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? PinmemberMember 423260024-Nov-08 0:45 
GeneralAccessibility Options Shortcut PinmemberDabara4-Aug-08 21:15 
QuestionResolveLink?????? Pinmemberlocoone28-Jun-07 17:49 
GeneralGood submission but it is important to point out... Pinmembersricard23-Oct-06 6:58 
QuestionHow to create the office style shortcuts! PinmemberFunfound21-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 help PinmemberEIFNOBODY19-Jul-05 7:19 
GeneralRe: C++ Char help Pinmemberkcselvaraj31-Mar-06 23:55 
GeneralC++ Char help PinsussAnonymous19-Jul-05 7:13 
GeneralRe: C++ Char help PinsussAnonymous19-Jul-05 7:16 
GeneralCreate subfolder PinmemberAndromeda Shun19-Apr-05 4:31 
GeneralRe: Create subfolder PinmemberAndromeda Shun19-Apr-05 4:43 
QuestionCreate shortcut in subfolder? PinmemberAndromeda Shun18-Apr-05 4:55 
AnswerRe: Create shortcut in subfolder? PinmemberAndromeda Shun18-Apr-05 23:53 
GeneralCompiler error PinmemberAndromeda Shun18-Apr-05 3:10 
GeneralRe: Compiler error Pinmemberrootdial5-Jan-06 22:42 
GeneralStart In dir PinmemberAlex Carter6-Sep-04 9:22 
GeneralRe: Start In dir PinmemberSinople23-May-05 3:47 
GeneralDelete Shortcut with any keyword PinmemberRyan Bohm4-Sep-04 8:10 
QuestionNice, and how to create a new folder? Pinsussbendo11-Jul-04 1:19 
AnswerRe: Nice, and how to create a new folder? PinsussAvinesh Bangar20-Jul-04 11:00 
Generalthanks, regular codes Pinmembercxy25-May-04 6:25 
GeneralGood solution! Pinmemberspawn__19-May-04 8:43 
GeneralRe: Good solution! PinmemberThomas Latuske19-May-04 8:43 
GeneralVery good !! PinmemberWREY14-May-04 12:22 
GeneralRe: Very good !! PinmemberThomas Latuske14-May-04 19:42 
GeneralResolveShortcut PinmemberThomas Latuske11-May-04 7:42 
GeneralRe: ResolveShortcut PinmemberOzzyJMalmsteen13-May-04 16:37 
GeneralOne more method PinmemberIgor Vigdorchik9-May-04 15:38 
GeneralRe: One more method PinmemberThomas Latuske10-May-04 6:08 
GeneralVery nice! PinmemberHans Dietrich9-May-04 14:26 
GeneralRe: Very nice! PinmemberMarkus Hlacer9-May-04 23:10 
GeneralRe: Very nice! PinmemberThomas Latuske10-May-04 6:17 
GeneralRe: Very nice! PinmemberThomas Latuske14-May-04 19:20 

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

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

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