
Introduction
�Shortcut� is a small utility to create Links on Start Menu, Start->Program Menu or on the Desktop. Most of the time, the only type of application that touches the Start menu is an installation program. You may want to add items to the Start menu if you think it will help users too. In few cases, you might want to add some functionality to the Start menu similar to the Documents submenu. The Start menu is not much more than a special view of a few special purpose directories. All the items in the Start menu should be links or directories. The shell builds the Start menu by reading the entries in the All Users\Start Menu and <User Name>\Start Menu directories. Any files that show up in these directories automatically appear in the Start menu.
Source Code
IShellLink
encapsulates everything about a link. The physical manifestation of an IShellLink
is a .lnk file. IShellLink
exposes interfaces that let you do everything from create the link and command line to resolve the link back to the actual item. From an active IShellLink
object, you can call QueryInterface()
to get an IPersistFile
instance. This IPersistFile
instance will allow you to save the link to disk.
Add the following statement of code in OnInitDialog()
.
CoInitialize(NULL) ;
Call CreateLink(int nFolder)
to create the link in a specified location. nFolder
can be any one of the following values:
CSIDL_STARTMENU
- To add a menu item in Start menu.
CSIDL_PROGRAMS
- To add a menu item in Start->Program menu.
CSIDL_DESKTOP
- To add a link on Desktop.
void CShortcutDlg::CreateLink(int nFolder)
{
UpdateData(TRUE) ;
if(m_strItem.IsEmpty() || m_strFile.IsEmpty())
{
AfxMessageBox("Please,enter the Menu Item and File name correctly.");
return;
}
LPITEMIDLIST pidl;
HRESULT hr = SHGetSpecialFolderLocation(NULL, nFolder, &pidl);
char szPath[_MAX_PATH];
BOOL f = SHGetPathFromIDList(pidl, szPath);
LPMALLOC pMalloc;
hr = SHGetMalloc(&pMalloc);
pMalloc->Free(pidl);
pMalloc->Release();
CString szLinkName = m_strItem;
szLinkName+= _T(".lnk");
CString m_szCurrentDirectory = szPath ;
CString szTemp = szLinkName;
szLinkName.Format( "%s\\%s", m_szCurrentDirectory, szTemp );
HRESULT hres = NULL;
IShellLink* psl = NULL;
hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink,
reinterpret_cast<void**>(&psl));
if (SUCCEEDED(hres))
{
IPersistFile* ppf = NULL;
psl->SetPath(m_strFile);
hres = psl->QueryInterface(IID_IPersistFile,
reinterpret_cast<void**>(&ppf));
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, szLinkName, -1,
wsz, MAX_PATH);
hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
UpdateData(FALSE) ;
}
How to Use
Using the �Shortcut� utility is very simple. Just enter the Name of the link and select the file to be used for the link. Go for creating the link on Start menu or Start->Program or Desktop.
References
- MSDN
- Windows Shell Programming by Scott Seely