【问题标题】:How to programmatically create a shortcut using Win32如何使用 Win32 以编程方式创建快捷方式
【发布时间】:2011-04-23 20:19:35
【问题描述】:

我需要使用 C++ 以编程方式创建快捷方式。

如何使用 Win32 SDK 做到这一点?

什么 API 函数可用于此目的?

【问题讨论】:

    标签: c++ windows winapi shortcut


    【解决方案1】:

    试用 Windows Shell Links. 此页面还包含一个C++ example。描述性片段:

    使用外壳链接

    本节包含的示例 演示如何创建和解决 基于 Win32 的快捷方式 应用。本节假设您 熟悉 Win32、C++ 和 OLE COM 编程。

    编辑:添加代码示例以防链接失效(而 MSDN 链接确实经常失效。)

    // 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.
    // lpszDesc     - Address of a buffer that contains a description of the 
    //                Shell link, stored in the Comment field of the link
    //                properties.
    
    #include "stdafx.h"
    #include "windows.h"
    #include "winnls.h"
    #include "shobjidl.h"
    #include "objbase.h"
    #include "objidl.h"
    #include "shlguid.h"
    
    HRESULT CreateLink(LPCWSTR lpszPathObj, LPCSTR lpszPathLink, LPCWSTR lpszDesc) 
    { 
        HRESULT hres; 
        IShellLink* psl; 
    
        // 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); 
    
            // 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); 
    
                // Save the link by calling IPersistFile::Save. 
                hres = ppf->Save(wsz, TRUE); 
                ppf->Release(); 
            } 
            psl->Release(); 
        } 
        return hres; 
    

    【讨论】:

    【解决方案2】:

    这篇 MSDN 文章 Shell Links 提供了有关该主题的综合教程和代码示例。

    【讨论】:

    • 这很有趣,你提供的链接和 Paul 提供的链接是同一个页面,但它们的链接在物理上是不同的。我不知道这是为什么。只是奇怪。只是想把它扔出去。
    【解决方案3】:

    这是一个基于 William Rayer code on codeproject.com的c++示例代码

    头文件(ShortcutProvider.h):

    #include <Windows.h>
    
    class ShortcutProvider
    {
    public:
        /*
        -------------------------------------------------------------------
        Description:
        Creates the actual 'lnk' file (assumes COM has been initialized).
    
        Parameters:
        pszTargetfile    - File name of the link's target.
        pszTargetargs    - Command line arguments passed to link's target.
        pszLinkfile      - File name of the actual link file being created.
        pszDescription   - Description of the linked item.
        iShowmode        - ShowWindow() constant for the link's target.
        pszCurdir        - Working directory of the active link. 
        pszIconfile      - File name of the icon file used for the link.
        iIconindex       - Index of the icon in the icon file.
    
        Returns:
        HRESULT value >= 0 for success, < 0 for failure.
        --------------------------------------------------------------------
        */
        HRESULT Create(LPSTR pszTargetfile, LPSTR pszTargetargs,
            LPSTR pszLinkfile, LPSTR pszDescription, 
            int iShowmode, LPSTR pszCurdir, 
            LPSTR pszIconfile, int iIconindex);
    };
    

    源文件(ShortcutProvide.cpp):

    #include "ShortcutProvider.h"
    #include <Windows.h>
    #include <shlobj.h>
    #include <winnls.h>
    #include <shobjidl.h>
    #include <objbase.h>
    #include <objidl.h>
    #include <shlguid.h>
    
    
    HRESULT ShortcutProvider::Create(LPSTR pszTargetfile, LPSTR pszTargetargs,
                                    LPSTR pszLinkfile, LPSTR pszDescription, 
                                    int iShowmode, LPSTR pszCurdir, 
                                    LPSTR pszIconfile, int iIconindex)
      {
        HRESULT       hRes;                  /* Returned COM result code */
        IShellLink*   pShellLink;            /* IShellLink object pointer */
        IPersistFile* pPersistFile;          /* IPersistFile object pointer */
        WCHAR wszLinkfile[MAX_PATH]; /* pszLinkfile as Unicode 
                                                string */
        int           iWideCharsWritten;     /* Number of wide characters 
                                                written */
        CoInitialize(NULL);
        hRes = E_INVALIDARG;
        if (
             (pszTargetfile != NULL) && (strlen(pszTargetfile) > 0) &&
             (pszTargetargs != NULL) &&
             (pszLinkfile != NULL) && (strlen(pszLinkfile) > 0) &&
             (pszDescription != NULL) && 
             (iShowmode >= 0) &&
             (pszCurdir != NULL) && 
             (pszIconfile != NULL) &&
             (iIconindex >= 0)
           )
        {
          hRes = CoCreateInstance(
            CLSID_ShellLink,     /* pre-defined CLSID of the IShellLink 
                                     object */
            NULL,                 /* pointer to parent interface if part of 
                                     aggregate */
            CLSCTX_INPROC_SERVER, /* caller and called code are in same 
                                     process */
            IID_IShellLink,      /* pre-defined interface of the 
                                     IShellLink object */
            (LPVOID*)&pShellLink);         /* Returns a pointer to the IShellLink 
                                     object */
          if (SUCCEEDED(hRes))
          {
            /* Set the fields in the IShellLink object */
            hRes = pShellLink->SetPath(pszTargetfile);
            hRes = pShellLink->SetArguments(pszTargetargs);
            if (strlen(pszDescription) > 0)
            {
              hRes = pShellLink->SetDescription(pszDescription);
            }
            if (iShowmode > 0)
            {
              hRes = pShellLink->SetShowCmd(iShowmode);
            }
            if (strlen(pszCurdir) > 0)
            {
              hRes = pShellLink->SetWorkingDirectory(pszCurdir);
            }
            if (strlen(pszIconfile) > 0 && iIconindex >= 0)
            {
              hRes = pShellLink->SetIconLocation(pszIconfile, iIconindex);
            }
    
            /* Use the IPersistFile object to save the shell link */
            hRes = pShellLink->QueryInterface(
              IID_IPersistFile,         /* pre-defined interface of the 
                                            IPersistFile object */
              (LPVOID*)&pPersistFile);            /* returns a pointer to the 
                                            IPersistFile object */
            if (SUCCEEDED(hRes))
            {
              iWideCharsWritten = MultiByteToWideChar(CP_ACP, 0, 
                                                   pszLinkfile, -1,
                                                   wszLinkfile, MAX_PATH);
              hRes = pPersistFile->Save(wszLinkfile, TRUE);
              pPersistFile->Release();
            }
            pShellLink->Release();
          }
    
        }
        CoUninitialize();
        return (hRes);
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用system函数执行mklink命令。

      system("mklink /d shortcut_name Target_file");
      

      【讨论】:

      • 这不会创建快捷方式(lnk 文件)
      • 但其他一些组合,例如 /J 用于链接目录。
      • /d 用于指向目录的链接;只需删除 /d 即可获得文件链接
      • mklink 工具在文件系统级别创建硬/软链接。这与创建 *.lnk 文件或 shell 完全无关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 2015-10-30
      • 1970-01-01
      相关资源
      最近更新 更多