c++ - 使用Windows API创建快捷方式.lnk

标签 c++ windows winapi

我在使用C++创建快捷方式时遇到问题。.lnk文件已创建,但是目标具有无效路径。
您能解释一下为什么这段代码没有创建正确的快捷方式吗?有人可以帮我修复我的代码吗?
这是代码

// RepChrome.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "winnls.h"
#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"
#include <shlobj.h>


HRESULT CreateLink(LPCWSTR lpszPathObj1, LPCSTR lpszPathLink, LPCWSTR lpszDesc,LPCWSTR lpszarg) 
{ 
    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(lpszPathObj1); 
        psl->SetArguments(lpszarg);
        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); 

            // Add code here to check return value from MultiByteWideChar 
            // for success.

            // Save the link by calling IPersistFile::Save. 
            hres = ppf->Save(wsz, TRUE); 
            ppf->Release(); 
        } 
        psl->Release(); 
    } 
    return hres; 
}    

int _tmain(int argc, _TCHAR* argv[])
{
    char sp[MAX_PATH] = { 0 };
    WCHAR p[MAX_PATH]=  { 0 };
    
    WCHAR deskPath[MAX_PATH] = { 0 };
    SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, 0, deskPath);
    sprintf_s( sp,sizeof(deskPath),"%s\\My Program.lnk",deskPath);
    WCHAR path[MAX_PATH] = { 0 };
    SHGetFolderPathW(NULL, CSIDL_PROGRAM_FILESX86, NULL, 0, path);    
    
    swprintf_s( p,sizeof(path),L"%s\\My Program\\start.exe",path);

    CreateLink(p, sp, L"",L""); 

    return 0;
}

最佳答案

当调用sprintf_s()时,%s需要一个char*字符串,但是您却给了它一个wchar*字符串。另外,您为sprintf_s()swprintf_s()的第二个参数传递了错误的值。
实际上,您根本不应该在此代码中使用任何char数据,尤其是因为无论如何您只是将其转换为wchar,因此您应该仅使用所有wchar字符串。将lpszPathLink参数更改为LPCWSTR,将sp缓冲区更改为WCHAR[],然后将sprintf_s()更改为swprintf_s()
另外,由于您一直用于链接的所有值都是WCHAR,因此您应该直接使用IShellLinkW而不是基于TCHARIShellLink
另外,CreateLink()要求预先调用CoInitialize/Ex(),但在此代码中未对其进行调用。
尝试以下方法:

// RepChrome.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "winnls.h"
#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"
#include <shlobj.h>

HRESULT CreateLink(LPCWSTR lpszPathObj1, LPCWSTR lpszPathLink, LPCWSTR lpszDesc, LPCWSTR lpszarg)
{
    HRESULT hres;
    IShellLinkW* 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_PPV_ARGS(&psl));
    if (SUCCEEDED(hres))
    {
        IPersistFile* ppf;

        // Set the path to the shortcut target and add the description.
        psl->SetPath(lpszPathObj1);
        psl->SetArguments(lpszarg);
        psl->SetDescription(lpszDesc);

        // Query IShellLink for the IPersistFile interface, used for saving the
        // shortcut in persistent storage.
        hres = psl->QueryInterface(IID_PPV_ARGS(&ppf));
        if (SUCCEEDED(hres))
        {
            // Save the link by calling IPersistFile::Save.
            hres = ppf->Save(lpszPathLink, TRUE);
            ppf->Release();
        }
        psl->Release();
    }
    return hres;
}

int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);

    WCHAR sp[MAX_PATH] = { 0 };
    WCHAR p[MAX_PATH] = { 0 };

    WCHAR deskPath[MAX_PATH] = { 0 };
    SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, 0, deskPath);
    swprintf_s( sp, _countof(sp), L"%s\\My Program.lnk", deskPath);

    WCHAR path[MAX_PATH] = { 0 };
    SHGetFolderPathW(NULL, CSIDL_PROGRAM_FILESX86, NULL, 0, path);
    swprintf_s( p, _countof(p), L"%s\\My Program\\start.exe", path);

    CreateLink(p, sp, L"",L"");

    CoUninitialize();

    return 0;
}

关于c++ - 使用Windows API创建快捷方式.lnk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63443681/

相关文章:

c++ - friend 类不适合我?

c++ - N-Queen问题:无法弄清楚为什么我的解决方案不起作用

c - 串行端口 : Read data problem with loopback, 未读取任何内容

java - 导出 Weka 模型以在 C 或 C++ 中使用

c++ - EnumPrinters 示例的链接器问题

windows - Windows 上的 Rust 回溯?

c++ - 最小化窗口时会发送什么消息?

c++ - 如何使用 C++ (keybd_event) 发送 unicode 键

c - 调整窗口大小时是否可以完全消除闪烁?

c++ - 如何使用Lz4库解压小于原始大小的文件?