HOWTO: Change the Icon of a Shortcut Through IShellLinkID: Q179904
|
This article describes how to create a shortcut and change the icon that is displayed for the shortcut.
IShellLink provides methods for obtaining and setting the icon for a
shortcut. The steps for changing the icon for a shortcut are as follows:
/*PARAMETERS
fname_to_create_link = (e.g.) "c:\\mytextfile.txt"
lnk_fname = (e.g.) "yourname.lnk"
*/
void CreateLinkThenChangeIcon(LPTSTR fname_to_create_link,
LPTSTR lnk_fname)
{
HRESULT hres;
IShellLink *psl = NULL;
IPersistFile *pPf = NULL;
WORD wsz[256];
TCHAR buf[256];
int id;
LPITEMIDLIST pidl;
hres = CoCreateInstance( CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*)&psl);
if(FAILED(hres))
goto cleanup;
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&pPf);
if(FAILED(hres))
goto cleanup;
hres = psl->SetPath(fname_to_create_link);
if(FAILED(hres))
goto cleanup;
//place the shortcut on the desktop
SHGetSpecialFolderLocation(hwnd, CSIDL_DESKTOP, >pidl);
SHGetPathFromIDList(pidl, buf);
lstrcat(buf,"\\");
lstrcat(buf,lnk_fname);
MultiByteToWideChar(CP_ACP, 0, buf, -1, wsz, MAX_PATH);
hres = pPf->Save(wsz, TRUE);
if(FAILED(hres))
goto cleanup;
GetSystemDirectory(buf, 256);
lstrcat(buf,"\\shell32.dll");
hres = psl->SetIconLocation(buf, 1);
if(FAILED(hres))
goto cleanup;
hres = psl->GetIconLocation(buf, 256, &id);
if(FAILED(hres))
goto cleanup;
pPf-&Save(wsz, TRUE);
cleanup:
if(pPf)
pPf->Release();
if(psl)
psl->Release();
}
Additional query words:
Keywords : kbLinks kbNTOS400 kbWinOS2000 kbWinOS95 kbWinOS98 kbGrpShell
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: June 24, 1999