HOWTO: Change the Icon of a Shortcut Through IShellLink

Last reviewed: January 26, 1998
Article ID: Q179904
The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK)

SUMMARY

This article describes how to create a shortcut and change the icon that is displayed for the shortcut.

MORE INFORMATION

IShellLink provides methods for obtaining and setting the icon for a shortcut. The steps for changing the icon for a shortcut are as follows:

  1. Obtain the IPersistFile interface from the IShellLink using QueryInterface with IID_IPersistFile.

  2. Call IShellLink::SetIconLocation with the file containing the icon (in this case the file is either a .dll or .exe) and the index of the icon.

  3. Call IPersistFile::Save to update the shortcut.

Sample Code

Following is sample code that creates a shortcut and sets the shortcut's icon to an icon contained in shell32.dll:

   /*PARAMETERS

   fname_to_create_link  = (e.g.) "c:\\mytextfile.txt"
   lnk_fname = (e.g.) "yourname.lnk"

   */

   void CreateLinkThenChangeIcon(LPTSTR fname_to_create_link,
                                 LPTSTR lnk_fname)
   {
   HRESULT hres;
   IShellLink *psl = NULL;
   IPersistFile *pPf = NULL;
   
   WORD wsz[256];
   TCHAR buf[256];
   int id;
   LPITEMIDLIST pidl;
   
   hres = CreateInstance(  CLSID_ShellLink,
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_IShellLink,
                           (LPVOID*)&psl);
   
   if(FAILED(hres))
      goto cleanup;
   
   hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&pPf);
   
   if(FAILED(hres))
      goto cleanup;
   
   hres = psl->SetPath(fname_to_create_link);
   
   if(FAILED(hres))
      goto cleanup;
   
   //place the shortcut on the desktop
   
   SHGetSpecialFolderLocation(hwnd, CSIDL_DESKTOP, &pidl);
   
   SHGetPathFromIDList(pidl, buf);
   
   lstrcat(buf,"\\");
   lstrcat(buf,lnk_fname);
   
   MultiByteToWideChar(CP_ACP, 0, buf, -1, wsz, MAX_PATH);
   
   hres = pPf->Save(wsz, TRUE);
   
   if(FAILED(hres))
      goto cleanup;
   
   GetSystemDirectory(buf, 256);
   
   lstrcat(buf,"\\shell32.dll");
   
   hres = psl->SetIconLocation(buf, 1);
   
   if(FAILED(hres))
      goto cleanup;
   
   hres = psl->GetIconLocation(buf, 256, &id);
   
   if(FAILED(hres))
      goto cleanup;
   
   pPf->Save(wsz, TRUE);
   
   
   cleanup:
   
   if(pPf)
      pPf->Release();
   
   if(psl)
      psl->Release();
   
   }
Keywords          : UsrShell
Version           : WINNT:
Platform          : winnt
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 26, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.