The API provides a COM interface called ISHELLLINK Allows us to create shortcuts. To create shortcuts at the desktop, we create an ishellLink object, set its properties, then save this Link to the Desktop directory.
The following example code demonstrates how to create a shortcut. In this example, this shortcut is saved in the C: / Drive directory. / / -------------------------------------------------------------------------------------------- ---------------------- Include
The above example simply saved the shortcut file to the C: / DRIVE directory, but did not save it 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 have to find Windows's Desktop directory, see Judging the section of Windows Desktop and related directories. Once we know the directory where Desktop is located, we can save the shortcut file to the Desktop directory. Then Windows can display the shortcut icon on the desktop.
Here is an improved example: file: // -------------------------------------- -------------------------------- Void TForm1 :: CreateShortcut (const ANSISTRING & FILE) {ishelllink * PLINK; IPERSISTFILE * PPERSISTFILE ; LPMALLOC ShellMalloc; LPITEMIDLIST DesktopPidl; char DesktopDir [MAX_PATH]; if (FAILED (SHGetMalloc (& ShellMalloc))) return; if (FAILED (SHGetSpecialFolderLocation (NULL, CSIDL_DESKTOPDIRECTORY, & DesktopPidl))) return;! if (SHGetPathFromIDList (DesktopPidl, DesktopDir) ) {ShellMalloc-> Free (DesktopPidl); ShellMalloc-> Release (); return;} ShellMalloc-> Free (DesktopPidl); ShellMalloc-> Release (); if (SUCCEEDED (CoInitialize (NULL))) {if (SUCCEEDED ( CocreateInstance (CLSID_SHELLLINK, NULL, CLSCTX_INPROC_SERVER, IID_ISHELLLINK, (VO ID **) & plink)) {plink-> setpath (file.c_str ()); PLINK-> SetDescription ("WOO HOO, LOOK AT HOMER's Shortcut); PLINK-> setShowcmd (SW_SHOW); if (Succeeded) -> QueryInterface (IID_IPersistFile, (void **) & pPersistFile))) {WideString strShortCutLocation (DesktopDir); strShortCutLocation = "//bcbshortcut.lnk"; pPersistFile-> Save (strShortCutLocation.c_bstr (), TRUE); pPersistFile-> Release ();} PLINK-> Release ();
}} File: // -------------------------------------------- -------------------------- Do not trap in a mud in COM, create shortcuts including some to COM. Don't let you fall into the complexity of COM. COM is just a way to create and use the object. In this example we can consider not using COM but use equivalent C technology. COM code C psuedo-equivalentIShellLink * pLink; TShellLink * Link; IPersistFile * pPersistFile; TPersistFile * PersistFile; CoInitialize (); CoCreateInstance (CLSID_ShellLink, Link = new TShellLink; NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) & pLink) pLink-> SetPath (file.c_str ()); link-> setpath (file.c_str ()); plink-> setshowcmd (sw_show); link-> setshowcmd (sw_show); plink-> queryinterface (IID_IPERSISTFILE PERSISTFILE = (void **) & ppersistfile)) Dynamic_cast