Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CShortcut (Shell links to special folders)

0.00/5 (No votes)
13 May 2004 1  
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

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