Jump to content

Windows create a shortcut


Josh
 Share

Recommended Posts

My string values in the shortcut file are getting terminated after the first character. Can anyone see the error?

#include "windows.h"
#include "winnls.h"
#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"

// https://learn.microsoft.com/en-au/windows/win32/shell/links?redirectedfrom=MSDN#creating-a-shortcut-and-a-folder-shortcut-to-a-file
bool CreateShortcut(const WString& path, const WString& dest, const WString& desc)
{
    auto rpath = RealPath(path).Replace("/", "\\");
    auto rdest = RealPath(dest).Replace("/", "\\");

    LPCWSTR lpszPathObj = rpath.c_str();
    LPCWSTR lpszPathLink = rdest.c_str();
    LPCWSTR lpszDesc = desc.c_str();

    CoInitialize(NULL);
    HRESULT hres;
    IShellLinkW* psl;
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
    if (SUCCEEDED(hres))
    {
        IPersistFile* ppf;
        psl->SetPath(lpszPathObj);
        psl->SetDescription(lpszDesc);
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
        if (SUCCEEDED(hres))
        {
            hres = ppf->Save(lpszPathLink, TRUE);
            ppf->Release();
        }
        psl->Release();
    }
    return hres == S_OK;
}

// Example:
CreateShortcut("Ultra Engine.exe", GetPath(PATH_DESKTOP) + "/Ultra Engine.lnk", "Description");

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Weird. It works if I convert the wide strings to Utf8:
 

bool CreateShortcut(const WString& path, const WString& dest, const WString& desc)
{
    auto rpath = RealPath(path).Replace("/", "\\");
    auto rdest = RealPath(dest).Replace("/", "\\");

    String s1 = rpath.ToUtf8String();
    String s2 = desc.ToUtf8String();
    String s3 = ExtractDir(rpath).Replace("/","\\").ToUtf8String();

    LPCSTR lpszPathObj = s1.c_str();
    LPCWSTR lpszPathLink = rdest.c_str();
    LPCSTR lpszDesc = s2.c_str();

    CoInitialize(NULL);
    HRESULT hres;
    IShellLink* psl;
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
    if (not SUCCEEDED(hres)) return false;
    IPersistFile* ppf;
    psl->SetPath(lpszPathObj);
    psl->SetWorkingDirectory(s3.c_str());
    psl->SetDescription(lpszDesc);
    hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
    if (SUCCEEDED(hres))
    {
        hres = ppf->Save(lpszPathLink, TRUE);
        ppf->Release();
    }
    psl->Release();
    return hres == S_OK;
}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

And this is the code to get the start menu location to place a link / shortcut in:

    PIDLIST_ABSOLUTE pidlist;
    wchar_t path[MAX_PATH];
    HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAMS, &pidlist);
    BOOL b = SHGetPathFromIDListW(pidlist, path);
    WString s = WString(path);
    Print(s);

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...