Create a shortcut on the Windows desktop

Source: Internet
Author: User
Tags root directory

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.