Board index » cppbuilder » How do I create a file shortcut(.lnk)

How do I create a file shortcut(.lnk)

Hi All

How do I create a file shortcut (.lnk)  ?

Eduardo

 

Re:How do I create a file shortcut(.lnk)


Look for IShellLink description in Win32 API help file.
Quote

> How do I create a file shortcut (.lnk)  ?

Re:How do I create a file shortcut(.lnk)


Here's the code I use:

#include <shlobj.h>

// the following helper function makes working with the Win32 API a little
easier

AnsiString __fastcall TShortcutWizardForm::GetSpecialFolder(int iFolder) {
    char path[MAX_PATH];
    ZeroMemory(path, MAX_PATH);

    LPMALLOC lpMalloc;
    if(SUCCEEDED(SHGetMalloc(&lpMalloc)))
    {
        LPITEMIDLIST lpidl;
        if(SUCCEEDED(SHGetSpecialFolderLocation(Handle, iFolder, &lpidl)))
        {
            SHGetPathFromIDList(lpidl, path);
            lpMalloc->Free(lpidl);
        }
        lpMalloc->Release();
    }

    return AnsiString(path);

Quote
}

// this is the function that creates the actual link

bool __fastcall TShortcutWizardForm::CreateShortcutLink(AnsiString LinkDesc,
AnsiString LinkPath, AnsiString TargetPath, AnsiString TargetArgs)
{
    // Must be done before any other calls to make it work.
    CoInitialize(NULL);

    bool retval = false;
    HRESULT hres = 0;
    IShellLink* psl = NULL;
    SetLastError(0);

    LPCLASSFACTORY pcf = NULL;
    hres = CoGetClassObject(
        CLSID_ShellLink,
        CLSCTX_INPROC_SERVER,
        NULL,
        IID_IClassFactory,
        (LPVOID*)&pcf);

    if(SUCCEEDED(hres))
    {
        hres = pcf->CreateInstance(
            NULL,
            IID_IShellLink,
            (LPVOID*)&psl);

        pcf->Release();
    }
    if(SUCCEEDED(hres))
    {
        IPersistFile* ppf = NULL;
        // Set the path to the shortcut target, and add the description.
        psl->SetPath(TargetPath.c_str());
        psl->SetArguments(TargetArgs.c_str());
        psl->SetDescription(LinkDesc.c_str());
        psl->SetWorkingDirectory(ExtractFilePath(TargetPath).c_str());
         // Query IShellLink for the IPersistFile interface for saving the
         // shortcut in persistent storage.
        hres = psl->QueryInterface(IID_IPersistFile, &(void*)ppf);

        if(SUCCEEDED(hres))
        {
            wchar_t wsz[MAX_PATH];
            // Ensure that the string is ANSI.
            MultiByteToWideChar(CP_ACP, 0, LinkPath.c_str(), -1, wsz,
MAX_PATH);
            // Save the link by calling IPersistFile::Save.
            hres = ppf->Save(wsz, TRUE);
            ppf->Release();
        }
        psl->Release();
        retval = SUCCEEDED(hres);
    }

    switch(hres)
    {
        case S_OK:
            ShowMessage("Link was successfully created");
             break;
        case REGDB_E_CLASSNOTREG:
            ShowMessage("A specified class is not registered in the
registration database");
            break;
        case E_OUTOFMEMORY:
            ShowMessage("Out of memory");
            break;
        case E_INVALIDARG:
            ShowMessage("One or more arguments are invalid");
             break;
        case E_UNEXPECTED:
            ShowMessage("An unexpected error occurred");
             break;
        case CLASS_E_NOAGGREGATION:
            ShowMessage("Class cannot be created as part");
             break;
        case E_FAIL:
            ShowMessage("Could not create link");
            break;
        case E_NOTIMPL:
            ShowMessage("Interface not implemented");
            break;
        default:
            ShowMessage("Unknown error");
    }

    CoUninitialize();

    return retval;

Quote
}

// and lastly, here an example of calling it
    AnsiString LinkPath = GetSpecialFolder(CSIDL_DESKTOP);
    LinkPath += "\\Run My Program.lnk";

    // you can also use the Start Menu
    // AnsiString LinkPath = GetSpecialFolder(CSIDL_STARTMENU);
    // LinkPath += "Programs\\My App\\Run My Program.lnk";

    // or a direct path:
    // AnsiString LinkPath = "C:\\Some Folder\\My Link.lnk";

    if( ! CreateShortcutLink("Description", LinkPath, Application->ExeName,
"") )
    {
        // error creating the link
    }

Gambit

Quote
Eduardo Freitag <efrei...@ig.com.br> wrote in message

news:39f69476_1@dnews...
Quote
> How do I create a file shortcut (.lnk)  ?

Other Threads