Utility to Create a Link (Shortcut)

Introduction

"Shortcut" is a small utility to create links on the 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 they will help users too. In a 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 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 an Menu item in Start Menu.
  • CSIDL_PROGRAMS—To add an Menu item in Start->Program Menu.
  • CSIDL_DESKTOP—To add a link on the 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 Shortcut

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 the Start Menu, Start->Program, or Desktop.

References

  1. MSDN: www.msdn.com
  2. Windows Shell Programming by Scott Seely


About the Author

Dillip Kumar Kara

I'm working as a Software Engineer at Princeton, New Jersey, USA.

Downloads

Comments

  • Shortcut creation utility

    Posted by Harish_KJ on 11/03/2008 08:20am

    Good job :) it does all I was looking for

    Reply
  • run-time error

    Posted by Bios1 on 06/14/2004 10:33am

    After compiling, it cannot run at Windows 2000, as most MFC projects, due to other libraries. It should run at Windows 9xx.

    • Re:run-time error

      Posted by newnick on 06/21/2004 04:07am

      Hi.,
       Can you elaborate little more about the problem you are facing .?
      
      Cheers,
      Dillip

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds