Note: I compile through windows ADV 2000,c++ Builder 6.0. :D
The API provides a COM interface called IShellLink that allows us to create shortcuts. To create a shortcut on the desktop, we create a IShellLink object, set its properties, and then save this link to the desktop directory.
The following example code demonstrates how to create a shortcut. In this example, the shortcut is saved in the C:\ root directory.
#define No_win32_lean_and_mean
#include <shlobj.h>
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package (smart_init)
#pragma resource "*.DFM"
TForm1 *form1;
//---------------------------------------------------------------------------
__fastcall Tform1::tform1 (tcomponent* Owner)
: Tform (Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall Tform1::button1click (tobject *sender)
{
if (Opendialog1->execute ())
CreateShortcut (Opendialog1->filename);
}
//---------------------------------------------------------------------------
void Tform1::createshortcut (const ansistring &file)
{
ishelllink* Plink;
Ipersistfile* Ppersistfile;
if (SUCCEEDED (CoInitialize (NULL))
{
if (succeeded CoCreateInstance (Clsid_shelllink, Null,clsctx_inproc_server,
Iid_ishelllink, (void * *) &plink))
{
Plink->setpath (File.c_str ());
Plink->setdescription ("Haha, you see me.");
Plink->setshowcmd (Sw_show);
if (SUCCEEDED (Plink->queryinterface (Iid_ipersistfile, (void *) &ppersistfile))
{
Widestring strshortcutlocation ("C:\\bcbshortcut.lnk");
Ppersistfile->save (Strshortcutlocation.c_bstr (), TRUE);
Ppersistfile->release ();
}
Plink->release ();
}
}
}
The above example simply saves the shortcut file to the C:\ root directory, but is not saved to the desktop directory. To have the shortcut appear on the desktop, simply save the shortcut file to the Desktop directory. First we need to find the Windows Desktop directory. Once we know the directory where desktop is located, we can save the shortcut file to the Desktop directory. Windows can then display the shortcut icon to the desktop. Here are some examples of improvements: