Code.GeekInterview.com
 
Code Samples C++
 

Create shortcuts on Desktop and Start Menu


Code ResourceAuthor: Mehedi Shams Rony  

Difficulty Level:

Published: 6th Nov 2006   Read: 5387 times  

Filed in: C++
Add Comment


 

 

Sponsored Links


 

 

This code is written in Microsoft Visual C++. Compile and use the executable file in your other projects.

For simplicity and ease of use from any other programs coded in any other language, I have provided the provision of using a text file that holds the paths. Create a text file named "Paths.txt" in C: drive that holds the following two entries: C:My Program.exe y The first line is file name to be run when the shortcut is double-clicked. The second entry indicates that a desktop shortcut is also needed. If not needed, simply omit the second line.

 


Sample Code
  1.  
  2. // ShortCut.cpp : Defines the entry point for the application.
  3. // Coded by: Mehedi Shams Rony, mehedishams@yahoo.com
  4. // This program creates shortcut for a program on desktop and on Start Menu -> Programs.
  5. // For simplicity and ease of use from any other programs coded in any other language,
  6. // I have provided the provision of using a text file that holds the paths.
  7. // Assuemd hereby, there is a file named "Paths.txt" in C: drive that holds the following
  8. // two entries:
  9. // C:My Program.exe
  10. // y
  11. // The first line is file name to be run when the shortcut is double-clicked.
  12. // The second entry indicates that a desktop shortcut is also needed. If not needed, simply
  13. // omit the second line.
  14. // This program is only a prototype that creates only one shortcut. If there are more than
  15. // one shortcut needed, please modify the code providing enough fgets, and enough CreateLink
  16. // functions.
  17.  
  18. #include "stdafx.h"
  19. #include <windows.h>
  20. #include <string.h>
  21. #include <shlobj.h>
  22. #include <stdio.h>
  23.  
  24. void CreateLink(int, char *, char *);
  25.  
  26. int APIENTRY WinMain(HINSTANCE hInstance,
  27.                      HINSTANCE hPrevInstance,
  28.                      LPSTR     lpCmdLine,
  29.                      int       nCmdShow)
  30. {
  31.         char ProgramPath[_MAX_PATH-1];
  32.         char DeskTopShortcutNeeded[2];
  33.  
  34.         // Obtaining shortcut names and target paths from the text file.
  35.         FILE *fp = fopen("C:Paths.txt", "r");
  36.        
  37.         // Assumed shortcut for only one file is needed.
  38.         fgets(ProgramPath, _MAX_PATH-1, fp);
  39.        
  40.         // ToDo: add code here to get paths to other files.
  41.  
  42.         fscanf(fp, "%s", DeskTopShortcutNeeded);
  43.         DeleteFile("C:Paths.txt");
  44.         fclose(fp);
  45.  
  46.         if (ProgramPath[strlen(ProgramPath)-1] == 'n')
  47.                 ProgramPath[strlen(ProgramPath)-1] = NULL;
  48.  
  49.         char LinkName[_MAX_PATH-1] = "My Program";
  50.         CreateLink(CSIDL_COMMON_PROGRAMS, LinkName, ProgramPath);
  51.  
  52.         // ToDo: add code here to make link to other files if more than one shortcut is needed.
  53.        
  54.         if (!strcmp(DeskTopShortcutNeeded, "y"))        // Create the link on the desktop if required.
  55.         {
  56.                 strcpy(LinkName, "My Program");
  57.                 CreateLink(CSIDL_COMMON_DESKTOPDIRECTORY, LinkName, ProgramPath);
  58.         }
  59.         return 0;
  60. }
  61.  
  62. void CreateLink(int SpecialFolderName, char *LinkName, char *FilePath)
  63. {
  64.         CoInitialize(NULL);     // Initialize the COM library on the current thread.
  65.  
  66.         // File system directory that contains the directories for the
  67.     // common program groups that appear on the Start menu for all users.
  68.     LPITEMIDLIST pidl;
  69.  
  70.     // Get a pointer to an item ID list that represents the path of a special folder.
  71.         HRESULT hr = SHGetSpecialFolderLocation(NULL, SpecialFolderName, &pidl);
  72.  
  73.     // Convert the item ID list's binary representation into a file system path.
  74.     char SpecialFolderPath[_MAX_PATH-1];
  75.  
  76.         // SpecialFolderPath is now Start Menu -> Programs path.
  77.     BOOL f = SHGetPathFromIDList(pidl, SpecialFolderPath);
  78.  
  79.     // Allocate a pointer to an IMalloc interface
  80.     LPMALLOC pMalloc;
  81.  
  82.     // Get the address of our task allocator's IMalloc interface
  83.     hr = SHGetMalloc(&pMalloc);
  84.  
  85.     // Free the item ID list allocated by SHGetSpecialFolderLocation
  86.     pMalloc->Free(pidl);
  87.  
  88.     // Free our task allocator
  89.     pMalloc->Release();
  90.    
  91.         if (SpecialFolderName == CSIDL_COMMON_PROGRAMS)
  92.         {
  93.                 char MyDirectory[_MAX_PATH-1];
  94.  
  95.                 // SpecialFolderPath = Start Menu -> Programs or Desktop path.
  96.                 strcpy(MyDirectory, SpecialFolderPath);
  97.  
  98.                 // MyDirectory = Start Menu -> Programs -> My Program or Desktop Path -> My Program
  99.                 strcat(MyDirectory, "My Program");
  100.  
  101.                 // Create Directory as Start Menu -> Programs -> My Program or Desktop Path -> My Program
  102.                 CreateDirectory(MyDirectory, NULL);
  103.  
  104.                 char szTemp[_MAX_PATH-1];
  105.                 strcpy(szTemp, MyDirectory);
  106.                 strcat(szTemp, "");
  107.                 strcat(szTemp, LinkName );
  108.                 strcpy(LinkName, szTemp);
  109.                 strcat(LinkName, ".lnk");       // LinkName should have .lnk extension."
  110.         }
  111.         else
  112.         {
  113.                 char szTemp[_MAX_PATH-1];
  114.                 strcpy(szTemp, SpecialFolderPath);
  115.                 strcat(szTemp, "");
  116.                 strcat(szTemp, LinkName );
  117.                 strcpy(LinkName, szTemp);
  118.                 strcat(LinkName, ".lnk");       // LinkName should have .lnk extension."
  119.         }
  120.  
  121.     HRESULT hres = NULL;
  122.     IShellLink* psl = NULL;
  123.        
  124.     // Get a pointer to the IShellLink interface.
  125.     hres = CoCreateInstance(CLSID_ShellLink,
  126.                                                         NULL,
  127.                                                         CLSCTX_INPROC_SERVER,
  128.                                                         IID_IShellLink,
  129.                                                         reinterpret_cast<void**>(&psl));
  130.  
  131.     if (SUCCEEDED(hres))
  132.     {
  133.         IPersistFile* ppf = NULL;
  134.         psl->SetPath(FilePath); // Set the path to the shortcut target
  135.  
  136.                 char buf[_MAX_PATH-1];
  137.                 ::GetCurrentDirectory(_MAX_PATH, buf);
  138.                 strcat(buf,"IconDLL.dll");
  139.                 psl->SetIconLocation(buf, 0);
  140.  
  141.             // Query IShellLink for the IPersistFile interface for saving
  142.         // the shortcut in persistent storage.
  143.         hres = psl->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&ppf));
  144.  
  145.         if (SUCCEEDED(hres))
  146.         {
  147.             WCHAR WideCharacterBuffer[_MAX_PATH-1];
  148.  
  149.             // Ensure that the string is ANSI.
  150.             MultiByteToWideChar(CP_ACP, 0, LinkName, -1, WideCharacterBuffer, MAX_PATH);
  151.  
  152.             // Save the link by calling IPersistFile::Save.
  153.             hres = ppf->Save(WideCharacterBuffer, TRUE);
  154.             ppf->Release();
  155.         }
  156.         psl->Release();
  157.     }
  158. }
  159.  
Copyright GeekInterview.com


Next Article: Finding loop in a link list


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. Priya36657
2. chowsys14951
3. bipin_vaylu13576
4. raghu.mohindru8717
5. Subashini7234
6. vimal.fire6976
7. rachittyagi6626
8. venki_madesh6204
9. prabha6191
10. vipin gupta5905

Active Coders

Refined Tags