In Windows, the Start Menu program group and desktop shortcut are the portal of the entire system for common users. Therefore, the general installer creates these two links at runtime. So is there a chance to create a common green Program (I am talking about the program that does not rely on the packaging wizard.
There are several key points for creating these two types of links under VC. The first is to obtain the directory where the link is located. For example, the desktop directory is usually C:/Documents ents and settings/username/desktop. The Program Group directory is usually C:/Documents and Settings/username/"start" menu/program.
Access a specific registry key.
The second key point is to use the COM interface to write the lnk file to the above directory. The principle is not profound. Please refer to the following code directly.
Bool createprogramgroup (const char * szgroupname)
{
Cregkey ckey;
If (error_success! = Ckey. Open (HKEY_CURRENT_USER,
"Software // Microsoft // windows // CurrentVersion // explorer // Shell Folders "))
{
Return false;
}
Char srorotdir [max_path];
DWORD dwsize = max_path;
If (error_success! = Ckey. queryvalue (srorotdir, "programs", & dwsize ))
{
Ckey. Close ();
Return false;
}
Ckey. Close ();
Char szfulldir [max_path];
Sprintf (szfulldir, "% S // % s", srorotdir, szgroupname );
Createdirectory (szfulldir, null );
Return true;
}
Bool CreateLink (const char * szlinkname, const char * szprogpath,
Bool bdesktop, const char * szgroupname/* = 0 */)
{
Ccomptr <ishelllink> spishelllink;
Hresult hR =: cocreateinstance (clsid_shelllink, null, 1,
Iid_ishelllink, (lpvoid *) & spishelllink );
If (failed (HR ))
{
Return false;
}
Spishelllink-> setpath (szprogpath );
Spishelllink-> setshowcmd (sw_shownormal );
// Obtain the directory where the program is located and set it to the working directory
Char * szsep = strrchr (szprogpath ,'//');
If (! Szsep)
Return false;
// Obtain the program directory
Char szprogfolder [max_path];
Memset (szprogfolder, 0, max_path );
Strncpy (szprogfolder, szprogpath, szsep-szprogpath + 1 );
Spishelllink-> setworkingdirectory (szprogfolder );
Ccomptr <ipersistfile> spipersistfile;
HR = spishelllink-> QueryInterface (iid_ipersistfile, (void **) & spipersistfile );
If (failed (HR ))
{
Return false;
}
Cregkey ckey;
If (error_success! = Ckey. Open (HKEY_CURRENT_USER,
"Software // Microsoft // windows // CurrentVersion // explorer // Shell Folders "))
{
Return false;
}
Char srorotdir [max_path];
DWORD dwsize = max_path;
If (error_success! = Ckey. queryvalue (srorotdir, bdesktop? "Desktop": "programs", & dwsize ))
{
Ckey. Close ();
Return false;
}
Ckey. Close ();
Char szfulldir [max_path];
If (szgroupname)
Sprintf (szfulldir, "% S // % S. lnk", srootdir, szgroupname, szlinkname );
Else
Sprintf (szfulldir, "% S // % S. lnk", srootdir, szlinkname );
Wchar wszfulldir [max_path];
: Multibytetowidechar (0, 0, szfulldir,-1, wszfulldir, max_path );
HR = spipersistfile-> Save (wszfulldir, false );
If (failed (HR ))
{
Return false;
}
: Shchangenotify (shcn_allevents, shcnf_path | shcnf_flush, szfulldir, 0 );
Return true;
}
Example:
Coinitialize (null );
Char szdemoexedir [max_path];
Sprintf (szdemoexedir, "C: // program files // demo // demo.exe ");
Char szuninstalldir [max_path];
Sprintf (szuninstalldir, "C: // program files // demo // uninstall.exe ");
CreateLink ("Demo", szdemoexedir, true );
Createprogramgroup ("Demo program group ");
CreateLink ("Demo", szdemoexedir, false, "Demo program group ");
CreateLink ("Uninstall Demo", szuninstalldir, false, "Demo program group ");
Couninitialize ();