vc++ windows 开始菜单添加快捷方式

开始菜单创建快捷方式

在windows软件开发中,软件安装过程中总是需要在开始菜单创建快捷方式,下面介绍一种开始菜单创建快捷方式的方法,具体代码如下:

/*

* 创建快捷方式

* szExePath[in]:要创建快捷方式的exe文件全路径

* szLinkName[in]:要创建的快捷方式.link文件的全路径

* iIcon[in]:要创建快捷方式的exe文件RC资源中的icon值(默认为0)

*/

BOOL MainFrame::CreateOneLink (LPCTSTR  lpszExePath, LPCTSTR lpszLnkPath, int iIcon)

{

         ASSERT(lpszExePath != NULL);

 

         if (NULL == lpszExePath)

         {

                   return ERROR_INVALID_PARAMETER;

         }

 

         DWORD dwRlt = ERROR_SUCCESS;

         HRESULT hres = S_OK;

         IShellLink *pShellLink = NULL;

         IPersistFile *pPersistFile = NULL;

         WCHAR szwShortCutName[MAX_PATH];

         try

         {

                   ::CoInitialize(NULL); // 初始化 COM 库

 

                   // 创建 COM 对象并获取其实现的接口

                   hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&pShellLink);

                   if (FAILED(hres))

                   {

                            throw(hres);

                   }

 

                   TCHAR szWorkPath[MAX_PATH] = { 0 };

                   StringCchCopy(szWorkPath, MAX_PATH, lpszExePath);

                   LPTSTR pszEnd = _tcsrchr(szWorkPath, _T('\\'));

                   if (pszEnd != NULL)

                   {

                            *pszEnd = NULL;

                   }

 

                   // 设置快捷方式的各种属性

                   pShellLink->SetPath(lpszExePath); // 快捷方式所指的应用程序名

                   pShellLink->SetDescription(_T("描述")); // 描述

                   pShellLink->SetWorkingDirectory(szWorkPath); // 设置工作目录

                   pShellLink->SetIconLocation(lpszExePath, iIcon);//直接取exe文件中的图标进行设置

 

                   // 查询 IShellLink 接口从而得到 IPersistFile 接口来保存快捷方式

                   hres = pShellLink->QueryInterface(IID_IPersistFile, (void **)&pPersistFile);

                   if (FAILED(hres))

                   {

                            throw(hres);

                   }

 

                   StringCchPrintf(szwShortCutName, MAX_PATH, _T("%s"), lpszLnkPath);

                   //使用 IPersistFile 接口的 Save() 方法保存快捷方式

                   hres = pPersistFile->Save(szwShortCutName, TRUE);

 

         }

         catch (DWORD dwExpRlt)

         {

                   dwRlt = dwExpRlt;

         }

         catch (...)

         {

         }

 

         if (pPersistFile != NULL){

                   pPersistFile->Release(); pPersistFile = NULL;

         }

 

         if (pShellLink != NULL){

                   pShellLink->Release(); pPersistFile = NULL;

         }

 

         CoUninitialize();

 

         return dwRlt;

}

/*

* 设置开始菜单快捷方式

* szExePath[in]:要创建快捷方式的exe文件全路径

* szLinkName[in]:要创建的快捷方式的名字

* iIcon[in]:要创建快捷方式的exe文件RC资源中的icon值(默认为0)

*/

void MainFrame::CreateStartMenuLink(LPCTSTR szExePath, LPCTSTR szLinkName, int iIcon)

{

         LPITEMIDLIST pidlBeginAt, pidlDestination;

         TCHAR szStartMenuPath[MAX_PATH] = { 0 };

         LPITEMIDLIST  ppidl = NULL;

         //获取所有用户的开始菜单路径,也可以通过修改CSIDL_COMMON_PROGRAMS获取其它用户的开始菜单路径

         HRESULT hSpecialPath = SHGetSpecialFolderLocation(NULL, CSIDL_COMMON_PROGRAMS, &ppidl);

         if (hSpecialPath == S_OK)

         {

                   BOOL flag = SHGetPathFromIDList(ppidl, szStartMenuPath);

                   CoTaskMemFree(ppidl);

         }

         else

         {

                   //失败

                   return;

         }

         //为开始菜单创建一个目录(这个是可选的)

         StringCchCat(szStartMenuPath, _countof(szStartMenuPath), _T("\\我的开始菜单"));

         memset(szStartMenuPath, 0, sizeof(szStartMenuPath));

         LPITEMIDLIST  ppidl2 = NULL;

         HRESULT hSpecialPath2 = SHGetSpecialFolderLocation(NULL, CSIDL_COMMON_PROGRAMS, &ppidl2);

         if (hSpecialPath2 == S_OK)

         {

                   BOOL flag = SHGetPathFromIDList(ppidl2, szStartMenuPath);

                   CoTaskMemFree(ppidl2);

         }

         else

         {

                   //失败

                   return;

         }

         //如果该目录还不存在,则创建

         StringCchCat(szStartMenuPath, _countof(szStartMenuPath), _T("\\我的开始菜单"));

         if (!PathFileExists(szStartMenuPath))

         {

                   CreateDirectory(szStartMenuPath, 0);

                   // 通知shell有关变化

                   SHChangeNotify(SHCNE_CREATE | SHCNE_INTERRUPT,

                            SHCNF_FLUSH | SHCNF_PATH,

                            szStartMenuPath, 0);

         }

         TCHAR szExeLinkFullPath[MAX_PATH] = { 0 };

         StringCchPrintf(szExeLinkFullPath, _countof(szExeLinkFullPath), _T("%s\\%s.lnk"), szStartMenuPath, szLinkName);

         CreateOneLink (szExePath, szExeLinkFullPath, iIcon);

         // 通知shell有关变化

         SHChangeNotify(SHCNE_CREATE | SHCNE_INTERRUPT,

                   SHCNF_FLUSH | SHCNF_PATH,

                   szExeLinkFullPath, 0);

}

相关头文件自行引入

posted @ 2016-04-07 14:41  哼哼哈哈二将  阅读(975)  评论(0编辑  收藏  举报