Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically create a shortcut using Win32

I need to programmatically create a shortcut using C++.

How can I do this using Win32 SDK?

What API function can be used for this purpose?

like image 238
sergiom Avatar asked Oct 11 '10 14:10

sergiom


People also ask

How do I create a shortcut key in C++?

According to MSDN, to create a shortcut to a non-file object, set the SetIDList to the LPITEMIDLIST of the object instead of setting the SetPath() in case of file base object and all other things remain the same.

How do you create shortcuts in Windows?

Click the Windows key, and then browse to the Office program for which you want to create a desktop shortcut. Right-click the program name or tile, and then select Open file location. Right-click the program name, and then click Send To > Desktop (Create shortcut). A shortcut for the program appears on your desktop.


1 Answers

Try Windows Shell Links. This page also contains a C++ example. Descriptive Snippet:

Using Shell Links

This section contains examples that demonstrate how to create and resolve shortcuts from within a Win32-based application. This section assumes you are familiar with Win32, C++, and OLE COM programming.

EDIT: Adding the code sample in case the link dies (and MSDN links do die often.)

// CreateLink - Uses the Shell's IShellLink and IPersistFile interfaces  //              to create and store a shortcut to the specified object.  // // Returns the result of calling the member functions of the interfaces.  // // Parameters: // lpszPathObj  - Address of a buffer that contains the path of the object, //                including the file name. // lpszPathLink - Address of a buffer that contains the path where the  //                Shell link is to be stored, including the file name. // lpszDesc     - Address of a buffer that contains a description of the  //                Shell link, stored in the Comment field of the link //                properties.  #include "stdafx.h" #include "windows.h" #include "winnls.h" #include "shobjidl.h" #include "objbase.h" #include "objidl.h" #include "shlguid.h"  HRESULT CreateLink(LPCWSTR lpszPathObj, LPCSTR lpszPathLink, LPCWSTR lpszDesc)  {      HRESULT hres;      IShellLink* psl;       // Get a pointer to the IShellLink interface. It is assumed that CoInitialize     // has already been called.     hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);      if (SUCCEEDED(hres))      {          IPersistFile* ppf;           // Set the path to the shortcut target and add the description.          psl->SetPath(lpszPathObj);          psl->SetDescription(lpszDesc);           // Query IShellLink for the IPersistFile interface, used for saving the          // shortcut in persistent storage.          hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);           if (SUCCEEDED(hres))          {              WCHAR wsz[MAX_PATH];               // Ensure that the string is Unicode.              MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);               // Save the link by calling IPersistFile::Save.              hres = ppf->Save(wsz, TRUE);              ppf->Release();          }          psl->Release();      }      return hres;  
like image 199
Paul Sasik Avatar answered Sep 21 '22 09:09

Paul Sasik