Add the program to the Start Menu

Source: Internet
Author: User
# Define reg_shellfodler _ T ("software // Microsoft // windows // CurrentVersion // explorer // Shell Folders") void ctestdlg: onbnclickedbutton7 () {// todo: add the control notification handler code cstring szlink, szpath; cstring szprogram; If (iswin9x () {szprogram = "programs"; getprofilestring (HKEY_CURRENT_USER, reg_shellfodler, szprogram );} else {szprogram = "Common Programs"; getprofilestring (HKEY_LOCAL_MACHINE, reg_shellfodler, szprogram);} // Add Management System Menu cstring szfolder = szprogram + _ T ("// test"); createdirectory (szfolder, null); cstring szmenu = szfolder + _ T ("// test. lnk "); szpath = _ T (" D: // test.exe "); CreateLink (szpath. getbuffer (szpath. getlength (), szmenu. getbuffer (szmenu. getlength (); szpath. releasebuffer (); szlink. releasebuffer ();} bool iswin9x () {DWORD dwversion; dwversion =: getversion (); If (dwversion> = 0x80000000) // Win9x return true; else return false;/ /Win2k/WINXP} bool CreateLink (lptstr szpath, pstr szlink) {hresult hres; ishelllink * PSL; ipersistfile * PPF; DWORD wsz [max_path]; int nbeginat = csidl_w.topdirectory; coinitialize (null); // create an ishelllink instance hres = cocreateinstance (clsid_shelllink, null, clsctx_inproc_server, iid_ishelllink, (void **) & PSL); If (failed (hres )) return false; // set the target application PSL-> setpath (szpath); // obtain the path cstring strtemp = szpa Th; cstring strapppath; int nret = strtemp. reversefind ('//'); If (nret! =-1) {strapppath = strtemp. left (nret);} PSL-> setworkingdirectory (strapppath); // get its ipersistfile interface from ishelllink // data file used to save shortcuts (*. lnk) hres = PSL-> QueryInterface (iid_ipersistfile, (void **) & PPF); If (failed (hres) return false; // ensure that the data file name is in ANSI format multibytetowidechar (cp_acp, 0, szlink,-1, (lpwstr) wsz, max_path); // call ipersist: save // Save the shortcut data file (*. lnk) hres = PPF-> Save (lpwstr) wsz, stgm_readwrite );// Release ipersistfile and ishelllink APIs PPF-> release (); PSL-> release (); Return true;} bool getprofilestring (hkey key, cstring szpath, cstring & szkeyvalue) {hkey; // define the related hkey and disable it at the end of the query. // Open the hkey related to the path data_set. The first parameter is the root key name and the second parameter table. // Indicates the location of the key to be accessed. The third parameter must be 0, and key_read indicates that the key is queried. // Access the Registry. hkey is used to save the long ret =: regopenkeyex (Key, szpath, 0, key_read, & hkey); If (Ret! = Error_success) return false; // If the hkey cannot be enabled, terminate the execution of the program lpbyte owner_get = new byte [256]; // query the relevant data (User Name owner_get ). DWORD type_1 = REG_SZ; DWORD cbdata_1 = 80; // hkey is the handle to the key opened by the regopenkeyex () function just now. szkeyvalue: // indicates the key value name to be queried, type_1 indicates the type of the queried data, saved by owner_get. // The queried data. cbdata_1 indicates the pre-set data length. Ret =: regqueryvalueex (hkey, szkeyvalue, null, & type_1, owner_get, & cbdata_1); If (Ret! = Error_success) {: regclosekey (hkey); szkeyvalue = "" ;}// return the null value else szkeyvalue = cstring (owner_get); Delete [] owner_get ;:: regclosekey (hkey); // close the enabled hkey return true before the program ends ;}
Other functions

// Create a desktop shortcut

Cstring szlink = _ T ("Common Desktop ");
If (! Getprofilestring (HKEY_LOCAL_MACHINE,
Reg_shellfodler,
Szlink)
)
{
MessageBox (_ T ("An error occurred while obtaining the desktop path! "), Strcaption, mb_ OK | mb_iconinformation );
Return false;
}

 

Szlink + = _ T ("// test. lnk ");
Cstring szpath = _ T ("D: // test.exe"); // actual path
CreateLink (szpath. getbuffer (szpath. getlength (), szlink. getbuffer (szlink. getlength ()));
Szpath. releasebuffer ();
Szlink. releasebuffer ();

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////
// Add the delete program option in the control panel.
// Add
// "Software // Microsoft // windows // CurrentVersion // uninstall/0 ";

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////
System self-starting project
// Software // Microsoft // windows // CurrentVersion // run/

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////////
// Delete the Start Menu folder
Bool bwin9x = iswin9x ();
Cstring szprogram;
Cstring szfolder;

 

If (bwin9x)
{
Szprogram = "programs ";
Getprofilestring (HKEY_CURRENT_USER,
"Software // Microsoft // windows // CurrentVersion // explorer // Shell Folders ",
Szprogram );
}
Else
{
Szprogram = "Common Programs ";
Getprofilestring (HKEY_LOCAL_MACHINE,
"Software // Microsoft // windows // CurrentVersion // explorer // Shell Folders ",
Szprogram );
}

 

Szfolder = szprogram + "// test ";

// Delete a folder

Tchar szpath [max_path];
Memset (szpath, 0, sizeof (szpath ));
_ Stprintf (szpath, _ T ("% S/0"), szfolder );

 

Shfileopstruct SFO;
SFO. fanyoperationsaborted = false;
SFO. fflags = fof_silent;
SFO. hnamemappings = NULL;
SFO. hwnd = NULL;
SFO. lpszprogresstitle = NULL;
SFO. pfrom = szpath;
SFO. PTO = NULL;
SFO. wfunc = fo_delete;
If (shfileoperation (& SFO )! = 0)
MessageBox (_ T ("Folder deletion failed "));

 

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.