/************************************************************************\
* LPSTR szPath -------快捷方式的目标应用程序名
* LPSTR szLink -------快捷方式的数据文件名(*.lnk)
* workingDirect-------目标程序的工作目录(用于加载资源等)
\*********************************************[TangJunxin 2011-10-29]**/
bool CreateLink( LPSTR szPath, LPSTR szLink,LPSTR workingDirect )
{
HRESULT hres ;
IShellLink * psl ;
IPersistFile* ppf ;
WCHAR wchar[MAX_PATH] ;
//创建一个IShellLink实例
hres = CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
if( FAILED( hres)) return FALSE ;
//设置目标应用程序
psl -> SetPath( szPath) ;
//设置快捷键(此处设为Shift+Ctrl+'R')
psl -> SetHotkey( MAKEWORD( 'R',
HOTKEYF_SHIFT |HOTKEYF_CONTROL)) ;
psl->SetWorkingDirectory(workingDirect);
//从IShellLink获取其IPersistFile接口
//用于保存快捷方式的数据文件 (*.lnk)
hres = psl -> QueryInterface( IID_IPersistFile,
(void**)&ppf) ;
if( FAILED( hres)) return FALSE ;
// 确保数据文件名为ANSI格式
MultiByteToWideChar( CP_ACP, 0, szLink, -1, wchar, MAX_PATH) ;
//调用IPersist:Save
//保存快捷方式的数据文件 (*.lnk)
hres = ppf -> Save( wchar, STGM_READWRITE) ;
//释放IPersistFile和IShellLink接口
ppf -> Release( ) ;
psl -> Release( ) ;
return TRUE;
}
void SendDesktopShortcut()
{
TCHAR lpszDesktopPath[MAX_PATH];
::SHGetSpecialFolderPath(AfxGetMainWnd()-> m_hWnd,lpszDesktopPath,CSIDL_DESKTOP,NULL);
CString strLinkPath = CString(lpszDesktopPath) + "\\Play.lnk";
TCHAR szCurDirect[MAX_PATH];
GetCurrentDirectory(MAX_PATH,szCurDirect);
#ifdef DEBUG
CString strExeName = "Play_d.exe";
#else
CString strExeName = "Play.exe";
#endif
CString strDestPath = CString(szCurDirect) + "\\" + strExeName;
CreateLink(strDestPath.GetBuffer(),strLinkPath.GetBuffer(),szCurDirect);
}