方法一:(不修改shell32.dll,不被360拦截)
void CreateLinkThenChangeIcon(CString fname_to_create_link, CString lnk_fname)
{//程序路径,快捷方式名称
HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
IShellLink *pisl;
hr = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pisl);
if (SUCCEEDED(hr))
{
IPersistFile* pIPF;
pisl->SetPath(fname_to_create_link);
hr = pisl->QueryInterface(IID_IPersistFile, (void**)&pIPF);
if (SUCCEEDED(hr))
{
CString csStartPath;
SHGetSpecialFolderPath(NULL,(LPWSTR)(LPCTSTR)csStartPath, CSIDL_DESKTOP, 0);
CString csLnkPath;
csLnkPath.Format(_T("%s\\%s"),csStartPath,lnk_fname);
USES_CONVERSION;
LPCOLESTR lpOleStr = csLnkPath;
pIPF->Save(lpOleStr, FALSE);
pIPF->Release();
}
pisl->Release();
}
CoUninitialize();
}
}
方法二:(修改shell32.dll,被360拦截提示)
void CreateLinkThenChangeIcon(CString fname_to_create_link, CString lnk_fname)
{//程序路径,快捷方式名称
CoInitialize( NULL );
HRESULT hres;
IShellLink *psl = NULL;
IPersistFile *pPf = NULL;
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(NULL, CSIDL_DESKTOP, &pidl);
SHGetPathFromIDList(pidl, buf);
lstrcat(buf,_T("\\"));
lstrcat(buf,lnk_fname);
hres = pPf->Save((LPCOLESTR)buf, TRUE);
if(FAILED(hres))
goto cleanup;
GetSystemDirectory(buf, 256);
lstrcat(buf,_T("\\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((LPCOLESTR)buf, TRUE);
cleanup:
if(pPf)pPf->Release();
if(psl)psl->Release();
CoUninitialize();
}