Create shortcuts on Windows Desktop

Source: Internet
Author: User

API provides a COM interface called ishelllink that allows us to create shortcuts. To create a shortcut on the desktop, we create an ishelllink object, set its properties, and save the link to the desktop directory.
The example code below 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-> setdeion (/"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 preceding example only saves the shortcut file to the C: // root directory, but not to the desktop directory. To make the shortcut appear on the desktop, you only need to save the shortcut file to the desktop directory. First, we need to find the Windows desktop directory. Once we know the directory where the desktop is located, we can save the shortcut file to the desktop directory. Then, Windows displays the shortcut icon on the desktop. The following is an improved example:
//----------------------------------------------------------------------
Void tform1: createshortcut (const ansistring & file)
{
Ishelllink * plink;
Ipersistfile * ppersistfile;
Lpmalloc shellmalloc;
Lp99vemidlist implements toppidl;
Char character topdir [max_path];
If (failed (shgetmalloc (& shellmalloc )))
Return;
If (failed (shgetspecialfolderlocation (null, csidl_desktopdirectory, & desktoppidl )))
Return;
If (! Shgetpathfromidlist (using toppidl, using topdir ))
{
Shellmalloc-> free (writable toppidl );
Shellmalloc-> release ();
Return;
}

Shellmalloc-> free (writable toppidl );
Shellmalloc-> release ();

If (succeeded (coinitialize (null )))
{
If (succeeded (cocreateinstance (clsid_shelllink, null, clsctx_inproc_server, iid_ishelllink, (void **) & plink )))
{
Plink-> setpath (file. c_str ());
Plink-> setdeion (/"Haha, you see me ./");
Plink-> setshowcmd (sw_show );
If (succeeded (plink-> QueryInterface (iid_ipersistfile, (void **) & ppersistfile )))
{
Widestring strshortcutlocation (shorttopdir );
Strshortcutlocation + =/"// bcb1_cut. lnk /";
Ppersistfile-> Save (strshortcutlocation. c_bstr (), true );
Ppersistfile-> release ();
}
Plink-> release ();
}
Couninitialize ();
}
}
//----------------------------------------------------------------------

Don't be stuck in the mud of COM
Creating shortcuts includes using COM. Don't let you get into the complexity of COM. Com is only a method for creating and using objects. In this example, we can consider using equivalent C ++ technology instead of COM.

 

The following is a comparison between the com code and the equivalent C ++ code. The com code is represented in light color, and the equivalent C ++ code is represented in dark color.

Ishelllink * plink;

Ipersistfile * ppersistfile;

Tshelllink * link;

Tpersistfile * persistfile;

Coinitialize ();

Cocreateinstance (clsid_shelllink, null, clsctx_inproc_server, iid_ishelllink, (void **) & plink );

Link = new tshelllink;

Plink-> setpath (file. c_str ());

Link-> setpath (file. c_str ());

Plink-> setshowcmd (sw_show );

Link-> setshowcmd (sw_show );

Plink-> QueryInterface (iid_ipersistfile dynamic_cast <tpersistfile *> (Link );

Persistfile = (void **) & ppersistfile) ppersistfile-> Save (/"C: //", true );

Persistfile-> Save (/"C ://///");

Ppersistfile-> release ();

Plink-> release ();

Delete persistfile

Delete link;

Couninitialize ();

 

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.