OWL NExt - Knowledge Base

[ Home | Contents | Search | Next | Previous | Up ]

How do I create shortcuts programmatically?

Date: 12/1/99
Time: 9:44:16 PM

Question:


I am trying to make an install program.  I want to my program to create shortcuts to the exe's that it installs.  How do I create these shortcuts??

Answer:


This involves creating a link to the shell and then calling a  few shell functions.   The following code is a function that will create the shortcut:

//CODE----------------------------------------------------------
HRESULT CreateLink(LPCSTR lpszPathObj, LPSTR lpszPathLink, LPSTR lpszDesc)
{
    HRESULT hres;
    IShellLink* psl;

    // Get a pointer to the IShellLink interface.
    hres = CoCreateInstance((_GUID)CLSID_ShellLink, NULL,
                             CLSCTX_INPROC_SERVER,
                             (_GUID)IID_IShellLink,
                             (void**)&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((_GUID)IID_IPersistFile,
                                   (void**)&ppf);

        if (SUCCEEDED(hres)) {
            WORD wsz[MAX_PATH];

            // Ensure that the string is ANSI.
            MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1,
                                (wchar_t*)wsz, MAX_PATH);

            // Save the link by calling IPersistFile::Save.
            hres = ppf->Save((wchar_t*)wsz, TRUE);
            ppf->Release();
        }
        psl->Release();
    }
    return hres;
}
//END CODE----------------------------------------------------

That should do it.  You now have a link to your .exe.

Last changed: July 14, 2001