Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating a desktop shortcut programatically

Posted on 2002-07-22
2
Medium Priority
?
452 Views
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).
0
Comment
Question by:aarone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
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
0
 
LVL 86

Accepted Solution

by:
jkr earned 800 total points
ID: 7169207
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/Shell/programmersguide/shell_int/shell_int_programming/shortcuts/shortcut.asp ("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.
       psl->SetPath(lpszPathObj);
       psl->SetDescription(lpszDesc);

       // 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
                    success.

           // Save the link by calling IPersistFile::Save.
           hres = ppf->Save(wsz, TRUE);
           ppf->Release();
       }
       psl->Release();
   }
   return hres;
}
 
Comment
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:

struct SHORTCUTSTRUCT
{
 LPTSTR pszTarget;
 LPTSTR pszDesc;
 WORD wHotKey;
 LPTSTR pszIconPath;
 WORD wIconIndex;
};
typedef SHORTCUTSTRUCT* LPSHORTCUTSTRUCT;

HRESULT SHCreateShortcutEx(LPCTSTR szLnkFile, LPSHORTCUTSTRUCT lpss)
{
 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));

if(FAILED(hr)){
 return hr;
}

pShellLink->SetPath(lpss->pszTarget);
pShellLink->SetDescription(lpss->pszDesc);
pShellLink->SetHotKey(lpss->wHotKey);
pShellLink->SetIconLocation(lpss->pszIconPath, lpss->wIconIndex);

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

if(FAILED(hr)){
 pShellLink->Release();
 return hr;
}

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

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

pPF->Release();
pShellLink->Release();
return hr;

}  
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

610 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