【问题标题】:C++: How do I create a Shortcut in the Start Menu on WindowsC++:如何在 Windows 的开始菜单中创建快捷方式
【发布时间】:2016-02-23 20:09:14
【问题描述】:

我想知道如何在 Windows 上获取开始菜单文件夹的路径,然后为可能包含非 ASCII 字符的路径创建快捷方式。

【问题讨论】:

    标签: c++ windows shortcut startmenu


    【解决方案1】:

    这里是解决方案。它使用 Qt,但也可以不使用。然后只需使用std::wstring 而不是QString。为了连接路径和文件名,您将不得不使用字符串操作而不是使用QDir

    #include <shlobj.h> 
    
    bool createStartMenuEntry(QString targetPath) {
        targetPath = QDir::toNativeSeparators(targetPath);
    
        WCHAR startMenuPath[MAX_PATH];
        HRESULT result = SHGetFolderPathW(NULL, CSIDL_COMMON_PROGRAMS, NULL, 0, startMenuPath);
    
        if (SUCCEEDED(result)) {
            QString linkPath = QDir(QString::fromWCharArray(startMenuPath)).absoluteFilePath("Shortcut Name.lnk");
    
            CoInitialize(NULL);
            IShellLinkW* shellLink = NULL;
            result = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLinkW, (void**)&shellLink);
            if (SUCCEEDED(result)) {
                shellLink->SetPath(targetPath.toStdWString().c_str());
                shellLink->SetDescription(L"Shortcut Description");
                shellLink->SetIconLocation(targetPath.toStdWString().c_str(), 0);
                IPersistFile* persistFile;
                result = shellLink->QueryInterface(IID_IPersistFile, (void**)&persistFile);
    
                if (SUCCEEDED(result)) {
                    result = persistFile->Save(linkPath.toStdWString().c_str(), TRUE);
    
                    persistFile->Release();
                } else {
                    return false;
                }
                shellLink->Release();
            } else {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }
    

    这是获取开始菜单文件夹位置的部分:

    WCHAR startMenuPath[MAX_PATH];
    HRESULT result = SHGetFolderPathW(NULL, CSIDL_COMMON_PROGRAMS, NULL, 0, startMenuPath);
    

    剩下的就是创建快捷方式了。将快捷方式名称和说明换成您想要的值。

    【讨论】:

      【解决方案2】:

      与接受的答案相同,但使用 Visual Studio 方法。

      用法:

      CString sProgramsPath = getenv("PROGRAMDATA");
      CString sShortcutPath = sProgramsPath += "\\Microsoft\\Windows\\Start Menu\\Programs\\SHORTCUT_NAME.lnk";
      // (that's .LNK)
      
      CreateLink("C:\\target_file_path\\target_file_name.exe",
                  "sShortcutPath",
                  "C:\\target_file_path\\,
                  "Shortcut Description");
      

      功能:

      /*============================================================================*/
      
      // CreateLink - Uses the Shell's IShellLink and IPersistFile interfaces 
      //              to create and store a shortcut to the specified object. 
      //
      // Returns the result of calling the member functions of the interfaces. 
      //
      // Parameters:
      // lpszPathObj  - Address of a buffer that contains the path of the object,
      //                including the file name.
      // lpszPathLink - Address of a buffer that contains the path where the 
      //                Shell link is to be stored, including the file name.
      // lpszPath     - Working directory of target Obj file
      // lpszDesc     - Address of a buffer that contains a description of the 
      //                Shell link, stored in the Comment field of the link
      //                properties.
      
          HRESULT                     CreateLink(
          LPCSTR                      lpszPathObj,
          LPCSTR                      lpszPathLink,
          LPCSTR                      lpszPath,
          LPCSTR                      lpszDesc )
      
      /*============================================================================*/
      { 
          IShellLink* psl = NULL;
          HRESULT hres = CoInitialize(NULL);
      
          if (!SUCCEEDED(hres))
              LOGASSERT(FALSE);
      
          // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
          // has already been called.
      
          hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); 
          if (SUCCEEDED(hres)) 
          { 
              IPersistFile* ppf; 
      
              // Set the path to the shortcut target and add the description. 
              psl->SetPath(lpszPathObj);
              psl->SetDescription(lpszDesc);
              psl->SetWorkingDirectory(lpszPath);
      
              // Query IShellLink for the IPersistFile interface, used for saving the 
              // shortcut in persistent storage. 
              hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); 
      
              if (SUCCEEDED(hres))
              { 
                  WCHAR wsz[MAX_PATH]; 
      
                  // Ensure that the string is Unicode. 
                  MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); 
      
                  // Add code here to check return value from MultiByteWideChar 
                  // for success.
      
                  // Save the link by calling IPersistFile::Save. 
                  hres = ppf->Save(wsz, TRUE);
                  if (!SUCCEEDED(hres))
                      LOGASSERT(FALSE);
      
                  ppf->Release(); 
              } 
              psl->Release(); 
          }
      
          CoUninitialize();
      
          return hres;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-10
        • 2010-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        相关资源
        最近更新 更多