vc创建桌面快捷方式
2012-09-13 10:18:34 来源:WEB开发网核心提示:BOOL CDlg::CreateDesktopShotCut(CString strName, CString strSourcePath){if (FAILED(CoInitialize(NULL)))return FALSE;BOOL bRet = FALSE;TCHAR Path[MAX_PATH+1];CSt
BOOL CDlg::CreateDesktopShotCut(CString strName, CString strSourcePath) { if (FAILED(CoInitialize(NULL))) return FALSE; BOOL bRet = FALSE; TCHAR Path[MAX_PATH+1]; CString strDestDir; LPITEMIDLIST pidl; LPMALLOC pShell; if(SUCCEEDED(SHGetMalloc(&pShell))) { if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOPDIRECTORY,&pidl))) { if(!SHGetPathFromIDList(pidl,Path)) { pShell->Free(pidl); } pShell->Release(); strDestDir.Format(_T("%s"),Path); strDestDir+= _T("\\"); strDestDir+= strName;//设置桌面快捷方式的名字 strDestDir+= _T(".lnk"); IShellLink* psl; if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl))) { psl->SetPath(strSourcePath + _T("Player.exe"));//设置快捷方式的目标位置 //比如目标位置为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, (LPCTSTR)strDestDir,-1,wsz,MAX_PATH);//设置桌面快捷方式的名字 if(SUCCEEDED(ppf->Save(strDestDir,TRUE)))//保存快捷方式到桌面 { ppf->Release(); psl->Release(); bRet = TRUE; }else{ ppf->Release(); psl->Release(); } }else{ ppf->Release(); psl->Release(); } } } } CoUninitialize(); if (!bRet) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); // Display the string. TRACE(_T("----------------Create Link Error: %s\r\n"), (TCHAR*)lpMsgBuf); _ComnLog->LogOutV(_T("----------------CreateDesktopShotCut Failed: %s\r\n"), (TCHAR*)lpMsgBuf); // Free the buffer. LocalFree( lpMsgBuf ); } return bRet; }
更多精彩
赞助商链接