Click here to Skip to main content
15,860,943 members
Articles / Desktop Programming / MFC
Article

Utility for creating Link(shortcut)...

Rate me:
Please Sign up or sign in to vote.
3.37/5 (11 votes)
11 May 20042 min read 65.5K   1K   20   9
"Shortcut" is a utility to create link on Start menu or Start->Program or Desktop.

Sample Image - UtilShortcuts.jpg

Introduction

“Shortcut” is a small utility to create Links on Start Menu, Start->Program Menu or on the Desktop. Most of the time, the only type of application that touches the Start menu is an installation program. You may want to add items to the Start menu if you think it will help users too. In few cases, you might want to add some functionality to the Start menu similar to the Documents submenu. The Start menu is not much more than a special view of a few special purpose directories. All the items in the Start menu should be links or directories. The shell builds the Start menu by reading the entries in the All Users\Start Menu and <User Name>\Start Menu directories. Any files that show up in these directories automatically appear in the Start menu.

Source Code

IShellLink encapsulates everything about a link. The physical manifestation of an IShellLink is a .lnk file. IShellLink exposes interfaces that let you do everything from create the link and command line to resolve the link back to the actual item. From an active IShellLink object, you can call QueryInterface() to get an IPersistFile instance. This IPersistFile instance will allow you to save the link to disk.

Add the following statement of code in OnInitDialog().

CoInitialize(NULL) ;  // To initialize the COM library on the current thread

Call CreateLink(int nFolder) to create the link in a specified location. nFolder can be any one of the following values:

  • CSIDL_STARTMENU - To add a menu item in Start menu.
  • CSIDL_PROGRAMS - To add a menu item in Start->Program menu.
  • CSIDL_DESKTOP - To add a link on Desktop.
void CShortcutDlg::CreateLink(int nFolder)
{
    UpdateData(TRUE) ;
    //Check for empty strings ...
    if(m_strItem.IsEmpty() || m_strFile.IsEmpty())
    {
        AfxMessageBox("Please,enter the Menu Item and File name correctly.");
        return;
    }

    //File system directory that contains the directories for the
    //common program groups that appear on the Start menu for all users.
    LPITEMIDLIST pidl;

    // Get a pointer to an item ID list that
    // represents the path of a special folder
    HRESULT hr = SHGetSpecialFolderLocation(NULL, nFolder, &pidl);

    // Convert the item ID list's binary
    // representation into a file system path
    char szPath[_MAX_PATH];
    BOOL f = SHGetPathFromIDList(pidl, szPath);

    // Allocate a pointer to an IMalloc interface
    LPMALLOC pMalloc;

    // Get the address of our task allocator's IMalloc interface
    hr = SHGetMalloc(&pMalloc);

    // Free the item ID list allocated by SHGetSpecialFolderLocation
    pMalloc->Free(pidl);

    // Free our task allocator
    pMalloc->Release();

    CString szLinkName = m_strItem;
    szLinkName+= _T(".lnk");

    CString m_szCurrentDirectory = szPath ;
    // "D:\\Documents and Settings\\Administrator\\Start Menu" ;
          
    CString szTemp = szLinkName;
    szLinkName.Format( "%s\\%s", m_szCurrentDirectory, szTemp );

    HRESULT hres = NULL;
    IShellLink* psl = NULL;

    // Get a pointer to the IShellLink interface. 
    hres = CoCreateInstance(CLSID_ShellLink, NULL, 
            CLSCTX_INPROC_SERVER, IID_IShellLink, 
            reinterpret_cast<void**>(&psl));

    if (SUCCEEDED(hres)) 
    {
        IPersistFile* ppf = NULL; 
        // Set the path to the shortcut target
        psl->SetPath(m_strFile); 

        // Query IShellLink for the IPersistFile interface for saving 
        //the shortcut in persistent storage. 
        hres = psl->QueryInterface(IID_IPersistFile, 
                 reinterpret_cast<void**>(&ppf)); 

        if (SUCCEEDED(hres))
        { 
            WCHAR wsz[MAX_PATH]; 

            // Ensure that the string is ANSI. 
            MultiByteToWideChar(CP_ACP, 0, szLinkName, -1, 
                wsz, MAX_PATH); 

            // Save the link by calling IPersistFile::Save. 
            hres = ppf->Save(wsz, TRUE); 
            ppf->Release(); 
        } 
        psl->Release(); 
    } 
    UpdateData(FALSE) ;
}

How to Use

Using the “Shortcut” utility is very simple. Just enter the Name of the link and select the file to be used for the link. Go for creating the link on Start menu or Start->Program or Desktop.

References

  1. MSDN
  2. Windows Shell Programming by Scott Seely

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
United States United States
Hi Friends.,

I am DILLIP working as a Software Engineer in South Korea .

My Hobbies:

Listening to Music, Net Browsing , watching TV..

Comments and Discussions

 
GeneralShortcut creation and MUI Pin
msacarny10-Oct-07 8:55
msacarny10-Oct-07 8:55 
Generalnice, thanks! Pin
arman-na26-Aug-06 10:08
arman-na26-Aug-06 10:08 
Generalthank's a lot ! Pin
ubentz14-Jun-06 5:17
ubentz14-Jun-06 5:17 
Generalshortcut to dial up connection Pin
Diego Serrano18-Apr-06 10:44
Diego Serrano18-Apr-06 10:44 
GeneralThanks and a question Pin
Golf-HotelDelta26-Mar-06 6:55
Golf-HotelDelta26-Mar-06 6:55 
AnswerRe: Thanks and a question (How to remove a shortcut) Pin
Dr. GUI26-Sep-08 1:53
Dr. GUI26-Sep-08 1:53 
GeneralThird time's a charm Pin
thenarc22-Jan-06 7:24
thenarc22-Jan-06 7:24 
GeneralThat's good Pin
pavlina14-Sep-05 21:03
pavlina14-Sep-05 21:03 
GeneralThanks! Pin
warrenblackwell27-Jul-05 19:40
warrenblackwell27-Jul-05 19:40 

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.