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 ();