Главная страница | назад





Article #17059: How do I create shortcuts programatically?

 Question and Answer Database

FAQ2059C.txt   How do I create shortcuts programatically?
Category   :Windows API
Platform    :All
Product    :C++Builder  ALL

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.

7/2/98 10:32:32 AM
 

Last Modified: 01-SEP-99