• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 957
  • Last Modified:

Create a lnk file programmatically

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)
0
LeTay
Asked:
LeTay
  • 2
1 Solution
 
Tomas Helgi JohannssonCommented:
   Hi!

Take a look at this example : http://www.swissdelphicenter.ch/torry/showcode.php?id=317

Regards,
  Tomas Helgi
0
 
ZoppoCommented:
Hi LeTay,

here's a link to a sample code to create lnk files in Delphi: http://delphi.about.com/od/windowsshellapi/a/create_lnk.htm

hope that helps,

ZOPPO
0
 
jkrCommented:
Since this is posted in C++ also, see http://msdn2.microsoft.com/en-us/library/aa969393.aspx ("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.
        psl->SetPath(lpszPathObj);
        psl->SetDescription(lpszDesc);
 
        // 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);
            ppf->Release();
        }
        psl->Release();
    }
    return hres;
}
0
 
jkrCommented:
Why did you reference the C++ zone if you're not interested in a C++ solution?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now