How to use MFC to create shortcuts

Source: Internet
Author: User

 

Void policyshell (long weventid, // event flag
Lpstr szpath) // path
{
Shchangenovel (weventid, shcnf_flush | shcnf_path, szpath, 0 );
// Obtain the parent directory of szpath
Char * P;
For (P = szpath + lstrlen (szpath)-1; * P! = '//'; P --);
* P = '/0 ';
Shchangenovel (shcn_updatedir | shcn_interrupt, shcnf_flush | shcnf_path, szpath, 0 );
}

// Create a shortcut
Bool CreateLink (lpstr szpath, lpstr szlink)

/* Target application name of the szpath shortcut

Szlink shortcut data file name (*. lnk )*/
{
Hresult hres;
Ishelllink * PSL;
Ipersistfile * PPF;
Word wsz [max_path];

//// Initialize com
Coinitialize (null );

// Create an ishelllink instance
Hres = cocreateinstance (clsid_shelllink, null, clsctx_inproc_server, iid_ishelllink,
(Void **) & PSL );
If (failed (hres ))
{
Couninitialize ();
Return false;
}

// Set the target application
Psl-> setpath (szpath );

// Set the shortcut key (SHIFT + Ctrl + 'R ')
// PSL-> sethotkey (makeword ('R ',
// Hotkeyf_shift | hotkeyf_control ));

Char * P;
For (P = szpath + lstrlen (szpath)-1; * P! = '//'; P --);
* P = '/0 ';
Psl-> setworkingdirectory (szpath );

// Obtain its ipersistfile interface from ishelllink
// Used to save the shortcut data file (*. lnk)
Hres = PSL-> QueryInterface (iid_ipersistfile, (void **) & PPF );
If (failed (hres ))
{
Couninitialize ();
Return false;
}

// Ensure that the data file name is in ANSI format
Multibytetowidechar (cp_acp, 0, szlink,-1, wsz, max_path );

// Call ipersistfile: Save
// Save the shortcut data file (*. lnk)
Hres = PPF-> Save (wsz, stgm_readwrite );
// Release the ipersistfile and ishelllink Interfaces
PPF-> release ();
Psl-> release ();
Couninitialize ();
Return true;
}

Void createthisappshortcut ()
{
Char szpath [max_path] = ""; // target application name of the shortcut
Char szlink [max_path] = ""; // shortcut data file name
/// Desktop shortcut
Getmodulefilename (null, szpath, max_path );
Getwindowsdirectory (szlink, max_path );
If (szlink [lstrlen (szlink)-1] = '//')
Lstrcat (szlink, "desktop // estk receiving settings. lnk ");
Else
Lstrcat (szlink, "// desktop // estk receiving settings. lnk ");
// Create a shortcut
If (! CreateLink (szpath, szlink ))
Return;
// Notify the shell about changes
Policyshell (shnning_create | shnning_interrupt, szlink );

//// Quick Start
Getmodulefilename (null, szpath, max_path );
Getwindowsdirectory (szlink, max_path );
If (szlink [lstrlen (szlink)-1] = '//')
Lstrcat (szlink, "Application Data // Microsoft // Internet Explorer // Quick Launch // estk receiving settings. lnk ");
Else
Lstrcat (szlink, "// Application Data // Microsoft // Internet Explorer // Quick Launch // estk receiving settings. lnk ");
If (! CreateLink (szpath, szlink ))
Return;
Policyshell (shnning_create | shnning_interrupt, szlink );
}

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.