Creating a desktop shortcut programatically

Posted on 2002-07-22
Last Modified: 2013-12-03
How do I create a desktop shortcut (.lnk) programatically? either fron an application (c++) or from the shell (command line).
Question by:aarone
LVL 23

Expert Comment

by:Roshan Davis
ID: 7169192
Find "Using Shell Links in Windows 95" in MSDN, You can find a GOOD Sample ...

Good Luck
LVL 86

Accepted Solution

jkr earned 200 total points
ID: 7169207
See ("Shell Links") - it comes with sample code:

// 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 containing the path of the object.
// lpszPathLink - address of a buffer containing the path where the
//                Shell link is to be stored.
// lpszDesc     - address of a buffer containing the description of the
//                Shell link.

HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc)
   HRESULT hres;
   IShellLink* psl;

   // Get a pointer to the IShellLink interface.
   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.

       // Query IShellLink for the IPersistFile interface 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);
           // TODO: Check return value from MultiByteWideChar to ensure

           // Save the link by calling IPersistFile::Save.
           hres = ppf->Save(wsz, TRUE);
   return hres;
From: jhance  Date: 07/04/2002 12:44PM PST  
To make a shortcut anywhere, use the IShellLink COM interface exported by the Windows Shell.

By the way, you may want to get a copy of Dino Esposito's book, Windows Shell Programming...

Anyway, a nice sample from that book:

 LPTSTR pszTarget;
 LPTSTR pszDesc;
 WORD wHotKey;
 LPTSTR pszIconPath;
 WORD wIconIndex;

 WCHAR wszLinkFile[MAX_PATH] = {0];
IShellLink* pShellLink = NULL;
IPersistFile* pPF = NULL;

if(lpss == NULL){
 return E_FAIL;

HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<LPVOID*>(&pShellLink));

 return hr;

pShellLink->SetIconLocation(lpss->pszIconPath, lpss->wIconIndex);

hr = pShellLink->QueryInterface(IID_IPersistFile, reinterpret_cast<LPVOID*>(&pPF));

 return hr;

MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szLnkFile, 01, wszLinkFile, MAX_PATH);

hr = pPF->Save(wszLnkFile, TRUE);

return hr;


Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Event ID 10010 3 59
Create an excel spreadsheet that reads and inserts data 5 60
Should CArray be used for a list of pointers in C++? 19 102
Best book to learn C++ 4 78
This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now