BOOL CreateDesktopShotCut( CString strName, CString strSourcePath )
{
if(FAILED(CoInitialize(NULL)))
{
return FALSE;
}
int i;
char Path[MAX_PATH+1];
CString strDestDir;
i=CSIDL_DESKTOPDIRECTORY;
LPITEMIDLIST pidl;
LPMALLOC pShell;
if(SUCCEEDED(SHGetMalloc(&pShell)))
{
if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,i,&pidl)))
{
if(!SHGetPathFromIDList(pidl,Path))
{
pShell->Free(pidl);
::CoUninitialize();
return FALSE;
}
pShell->Release();
strDestDir.Format("%s",Path);
strDestDir+="\\";
strDestDir+=strName;//设置桌面快捷方式的名字
strDestDir+=".lnk";
IShellLink* psl;
if(SUCCEEDED(CoCreateInstance
(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&psl)))
{
psl->SetPath(strSourcePath);//设置快捷方式的目标位置
//比如目标位置为C:\windows\a.txt 起始位置就应该设置为C:\windows否则会导致不可预料的错误
//如果是文件夹的快捷方式起始位置和目标位置可以设置为一样
psl->SetWorkingDirectory(strSourcePath); //设置快捷方式的起始位置
IPersistFile* ppf;
if(SUCCEEDED(psl->QueryInterface(IID_IPersistFile,(LPVOID*)
&ppf)))
{
WCHAR wsz[MAX_PATH];
MultiByteToWideChar
(CP_THREAD_ACP,MB_PRECOMPOSED,strDestDir,-1,wsz,MAX_PATH);//设置桌面快捷方式的名字
if(SUCCEEDED(ppf->Save(wsz,TRUE)))//保存快捷方式到桌面
{
ppf->Release();
psl->Release();
::CoUninitialize();
return TRUE;
}else
{
ppf->Release();
psl->Release();
::CoUninitialize();
return FALSE;
}
}else
{
ppf->Release();
psl->Release();
::CoUninitialize();
return FALSE;
}
}else
{
::CoUninitialize();
return FALSE;
}
}else
{
::CoUninitialize();
return FALSE;
}
}else
{
::CoUninitialize();
return FALSE;
}
}