How Do I Create A Desktop Shortcut For My Application?

A Member? Please Login  
type your username and password to login
Date: October 14, 2008, 04:10 AM
249609 members and 148268 Topics
Latest Member: mgbek
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Programming  |  How Do I Create A Desktop Shortcut For My Application?
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: How Do I Create A Desktop Shortcut For My Application?  (Read 1080 views)
@tomX (m)
How Do I Create A Desktop Shortcut For My Application?
« on: October 19, 2007, 06:55 PM »

Hi Gurus'
Does anyone here know how to programaticaly create a desktop shortcut for an executable file?
I'll prefer a C++ example but VB will do just fine.
Thanks Y'all
Smart K. (m)
Re: How Do I Create A Desktop Shortcut For My Application?
« #1 on: October 23, 2007, 06:46 PM »

its beter to do that in the installer like Inno Setup, Installshield, etc.
@tomX (m)
Re: How Do I Create A Desktop Shortcut For My Application?
« #2 on: October 30, 2007, 03:24 PM »

Hi @Smart K,
thanks for the insigth. I'm quite familiar with and use some of this installer but there are instances when you may want to be able to do this programaticaly (say you were to build your very own installer for instance).
The shell api in C++ is supposed to be good for this (see codes bellow) but I'm having a heck of time getting it to work right. (Makes me feel like a newbie all over again).


#include <objbase.h>
#include <shlguid.h>
,

void CShellUtils::CreateShortcut(const CString& ExePath, const CString& LinkFilename, const CString& WorkingDirectory, const CString& Description, int nFolder)
{
   // Must have called CoInitalize before this function is called!

    IShellLink* psl;
   const CString PathLink = GetSpecialFolderLocation(nFolder) + "\\" + LinkFilename;
   
    // Get a pointer to the IShellLink interface.
    HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                           IID_IShellLink, (PVOID *) &psl);
   
    if (SUCCEEDED(hres))
   {
        IPersistFile* ppf;
      
        // Set the path to the shortcut target and add the
        // description.
        psl->SetPath((LPCSTR) ExePath);
      psl->SetWorkingDirectory((LPCSTR) WorkingDirectory);
        psl->SetDescription((LPCSTR) Description);
      
      // Query IShellLink for the IPersistFile interface for saving the
      // shortcut in persistent storage.
        hres = psl->QueryInterface(IID_IPersistFile, (PVOID *) &ppf);
      
        if (SUCCEEDED(hres))
      {
            WORD wsz[MAX_PATH];
         
            // Ensure that the string is ANSI.
            MultiByteToWideChar(CP_ACP, 0, (LPCSTR) PathLink, -1, wsz, MAX_PATH);
         
            // Save the link by calling IPersistFile::Save.
            hres = ppf->Save(wsz, TRUE);
            ppf->Release();
        }
        psl->Release();
    }
}
Smart K. (m)
Re: How Do I Create A Desktop Shortcut For My Application?
« #3 on: October 30, 2007, 06:11 PM »

TRY THIS: (from CodeProject)

/**********************************************************************
* Function, : CreateShortcut
* Parameters, : lpszFileName - string that specifies a valid file name
*          lpszDesc - string that specifies a description for a
                             shortcut
*          lpszShortcutPath - string that specifies a path and
                                     file name of a shortcut
* Returns, : S_OK on success, error code on failure
* Description, : Creates a Shell link object (shortcut)
**********************************************************************/
HRESULT CreateShortcut(/*in*/ LPCTSTR lpszFileName,
                    /*in*/ LPCTSTR lpszDesc,
                    /*in*/ LPCTSTR lpszShortcutPath)
{
    HRESULT hRes = E_FAIL;
    DWORD dwRet = 0;
    CComPtr<IShellLink> ipShellLink;
        // buffer that receives the null-terminated string
        // for the drive and path
    TCHAR szPath[MAX_PATH];   
        // buffer that receives the address of the final
        //file name component in the path
    LPTSTR lpszFilePart;   
    WCHAR wszTemp[MAX_PATH];
       
    // Retrieve the full path and file name of a specified file
    dwRet = GetFullPathName(lpszFileName,
                       sizeof(szPath) / sizeof(TCHAR),
                       szPath, &lpszFilePart);
    if (!dwRet)                                       
        return hRes;

    // Get a pointer to the IShellLink interface
    hRes = CoCreateInstance(CLSID_ShellLink,
                            NULL,
                            CLSCTX_INPROC_SERVER,
                            IID_IShellLink,
                            (void**)&ipShellLink);

    if (SUCCEEDED(hRes))
    {
        // Get a pointer to the IPersistFile interface
        CComQIPtr<IPersistFile> ipPersistFile(ipShellLink);

        // Set the path to the shortcut target and add the description
        hRes = ipShellLink->SetPath(szPath);
        if (FAILED(hRes))
            return hRes;

        hRes = ipShellLink->SetDescription(lpszDesc);
        if (FAILED(hRes))
            return hRes;

        // IPersistFile is using LPCOLESTR, so make sure
                // that the string is Unicode
#if !defined _UNICODE
        MultiByteToWideChar(CP_ACP, 0,
                       lpszShortcutPath, -1, wszTemp, MAX_PATH);
#else
        wcsncpy(wszTemp, lpszShortcutPath, MAX_PATH);
#endif

        // Write the shortcut to disk
        hRes = ipPersistFile->Save(wszTemp, TRUE);
    }

    return hRes;
}

/*********************************************************************
* Function, : ResolveShortcut
* Parameters, : lpszShortcutPath - string that specifies a path
                                     and file name of a shortcut
*          lpszFilePath - string that will contain a file name
* Returns, : S_OK on success, error code on failure
* Description, : Resolves a Shell link object (shortcut)
*********************************************************************/
HRESULT ResolveShortcut(/*in*/ LPCTSTR lpszShortcutPath,
                        /*out*/ LPTSTR lpszFilePath)
{
    HRESULT hRes = E_FAIL;
    CComPtr<IShellLink> ipShellLink;
        // buffer that receives the null-terminated string
        // for the drive and path
    TCHAR szPath[MAX_PATH];     
        // buffer that receives the null-terminated
        // string for the description
    TCHAR szDesc[MAX_PATH];
        // structure that receives the information about the shortcut
    WIN32_FIND_DATA wfd;   
    WCHAR wszTemp[MAX_PATH];

    lpszFilePath[0] = '\0';

    // Get a pointer to the IShellLink interface
    hRes = CoCreateInstance(CLSID_ShellLink,
                            NULL,
                            CLSCTX_INPROC_SERVER,
                            IID_IShellLink,
                            (void**)&ipShellLink);

    if (SUCCEEDED(hRes))
    {
        // Get a pointer to the IPersistFile interface
        CComQIPtr<IPersistFile> ipPersistFile(ipShellLink);

        // IPersistFile is using LPCOLESTR,
                // so make sure that the string is Unicode
#if !defined _UNICODE
        MultiByteToWideChar(CP_ACP, 0, lpszShortcutPath,
                                       -1, wszTemp, MAX_PATH);
#else
        wcsncpy(wszTemp, lpszShortcutPath, MAX_PATH);
#endif

        // Open the shortcut file and initialize it from its contents
        hRes = ipPersistFile->Load(wszTemp, STGM_READ);
        if (SUCCEEDED(hRes))
        {
            // Try to find the target of a shortcut,
                        // even if it has been moved or renamed
            hRes = ipShellLink->Resolve(NULL, SLR_UPDATE);
            if (SUCCEEDED(hRes))
            {
                // Get the path to the shortcut target
                hRes = ipShellLink->GetPath(szPath,
                                     MAX_PATH, &wfd, SLGP_RAWPATH);
                if (FAILED(hRes))
                    return hRes;

                // Get the description of the target
                hRes = ipShellLink->GetDescription(szDesc,
                                             MAX_PATH);
                if (FAILED(hRes))
                    return hRes;

                lstrcpyn(lpszFilePath, szPath, MAX_PATH);
            }
        }
    }

    return hRes;
}


//USAGE
void HowToCreateShortcut()
{
    LPCTSTR lpszFileName = _T("C:\\Work\\Window.exe");
    LPCTSTR lpszShortcutDesc = _T("Anything can go here");
    LPCTSTR lpszShortcutPath =
_T("C:\\Documents and Settings\\Administrator\\Desktop\\Sample Shortcut.lnk");

    CreateShortcut(lpszFileName, lpszShortcutDesc, lpszShortcutPath);
}

void HowToResolveShortcut()
{
    LPCTSTR lpszShortcutPath =
_T("C:\\Documents and Settings\\Administrator\\Desktop\\Sample Shortcut.lnk");
    TCHAR szFilePath[MAX_PATH];

    ResolveShortcut(lpszShortcutPath, szFilePath);
}
@tomX (m)
Re: How Do I Create A Desktop Shortcut For My Application?
« #4 on: October 31, 2007, 09:29 AM »

@Smart K,
Thanks once again. I'll give it a trial and let you know how it works out.
@tomX (m)
Re: How Do I Create A Desktop Shortcut For My Application?
« #5 on: October 31, 2007, 10:45 AM »

Yeah! I see what I've been missing out now. The Shell objects are actualy COM objects and I shouls have initialised COM in my workspace before using them. You know. . .

::CoInitialize(NULL);

Such a small ommission, such pains to go with it.
Thanks  Smart K and Seun.
 MySQL 5.0 Database Has Been Released!  Microsft Visual Studio 2008 Beta  Software Packaging  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 
Google
 
Web www.nairaland.com
Sections: TV/Movies (2) Music/Radio (2) Celebrities Job Talk Jobs/Vacancies (2) Career Talk Romance Books Politics Sports Fashion Travel
Health Schooling Religion General(2) Business Webmaster Programming Computers Phones Cars & Trucks

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.