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

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

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??

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:
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,
    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((_GUID)IID_IPersistFile, 

        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);
    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