Create a lnk file programmatically

Posted on 2007-07-31
Last Modified: 2010-04-05
In order to create an application startup shortcut on the Windows user Desktop, I know I need to create a .lnk file at c:\Document and Settings\UserName\DeskTop\ApplicationTitle.lnk
Can somebody provide the code for creating this lnk file ?
Says the title is 'ApplicationTitle', the full path to the Application is 'FullApplicationPath' and the icon should be the icon 0 of the program (Delphi program)
Question by:LeTay
    LVL 24

    Expert Comment

    by:Tomas Helgi Johannsson

    Take a look at this example :

      Tomas Helgi
    LVL 30

    Accepted Solution

    Hi LeTay,

    here's a link to a sample code to create lnk files in Delphi:

    hope that helps,

    LVL 86

    Expert Comment

    Since this is posted in C++ also, see ("Shell Links"):

    // CreateLink - uses the Shell's IShellLink and IPersistFile interfaces
    //              to create and store a shortcut to the specified object.
    // Returns the result of calling the member functions of the interfaces.
    // Parameters:
    // lpszPathObj  - address of a buffer containing the path of the object.
    // lpszPathLink - address of a buffer containing the path where the
    //                Shell link is to be stored.
    // lpszDesc     - address of a buffer containing the description of the
    //                Shell link.

    HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc)
        HRESULT hres;
        IShellLink* psl;
        // Get a pointer to the IShellLink interface.
        hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                                IID_IShellLink, (LPVOID*)&psl);
        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(IID_IPersistFile, (LPVOID*)&ppf);
            if (SUCCEEDED(hres))
                WCHAR wsz[MAX_PATH];
                // Ensure that the string is Unicode.
                MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
                // Add code here to check return value from MultiByteWideChar
                // for success.
                // Save the link by calling IPersistFile::Save.
                hres = ppf->Save(wsz, TRUE);
        return hres;
    LVL 86

    Expert Comment

    Why did you reference the C++ zone if you're not interested in a C++ solution?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now