C++: How do I create a Shortcut in the Start Menu on Windows
Asked Answered
S

3

3

I would like to know how to obtain the path to the start menu folder on Windows and then create a shortcut to a path that might contain non-ASCII characters.

Sabrasabre answered 21/11, 2015 at 9:45 Comment(0)
S
3

Here is the solution. It uses Qt but it's also possible without. Then just use std::wstring instead of QString. For concatenating the paths and filenames you will then have to use string operations instead of using 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;
}

Thats the part that obtains the location of the start-menu folder:

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

The rest is then creation of the shortcut. Exchange shortcut name and description for your desired values.

Sabrasabre answered 21/11, 2015 at 9:45 Comment(0)
K
1

Same idea as accepted answer but Visual Studio method.

Usage:

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");

Function:

/*============================================================================*/

// 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;
}
Karp answered 7/6, 2018 at 18:1 Comment(0)
S
0

The answer by Enigma is very close, but appears to not work on Windows 10. It fails with error E_ACCESSDENIED when calling the Save function on IPersistFile.

I managed to adapt their answer to work by following the Windows documentation. The key is to use SHGetKnownFolderPath rather than getenv("PROGRAMDATA"):

#define WIN32_LEAN_AND_MEAN

#include "windows.h"
#include "combaseapi.h"
#include "shlobj.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

bool windowsInstall()
{
    char programFilesPath[1024] = {0};

    {
        WCHAR* wideProgramFilesPath = NULL;
        SHGetKnownFolderPath(FOLDERID_Programs, 0, NULL, (&wideProgramFilesPath));
        wcstombs(programFilesPath, wideProgramFilesPath, sizeof(programFilesPath));
        CoTaskMemFree(wideProgramFilesPath);
    }
    char shortcutPath[2048] = {0};
    /*
    * Fill these in with your application details!
    *
    * The text before .lnk in shortcutPath will show as the shortcut text to the user */
    snprintf(shortcutPath, sizeof(shortcutPath), "%s\\My Program.lnk", programFilesPath);
    const char* executableFilename = "C:\\Path\\To\\MyProgram\\My_Program.exe";
    const char* executableWorkingDirectory = "C:\\Path\\To\\MyProgram";
    const char* shortcutDescription = "This is My Program's description.";
    HRESULT result = CoInitialize(NULL);
    if (!(SUCCEEDED(result)))
    {
        return false;
    }
    IShellLink* link = NULL;
    result= CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, ((LPVOID*)(&link)));
    if (!(SUCCEEDED(result)))
    {
        CoUninitialize();
        return false;
    }
    link->SetPath(executableFilename);
    link->SetWorkingDirectory(executableWorkingDirectory);
    link->SetDescription(shortcutDescription);
    IPersistFile* persistFile = NULL;
    result= link->QueryInterface(IID_IPersistFile, ((void**)(&persistFile)));
    if (!(SUCCEEDED(result)))
    {
        link->Release();
        CoUninitialize();
        return false;
    }
    WCHAR wideShortcutPath[1024];
    MultiByteToWideChar(CP_ACP, 0, shortcutPath, -1, wideShortcutPath, (sizeof(wideShortcutPath) / sizeof(wideShortcutPath[0])));
    result= persistFile->Save(wideShortcutPath, TRUE);
    if (!(SUCCEEDED(result)))
    {
        persistFile->Release();
        link->Release();
        CoUninitialize();
        return false;
    }
    persistFile->Release();
    link->Release();
    CoUninitialize();
    return true;
}
Sherlocke answered 21/7, 2023 at 23:42 Comment(3)
You should be using IShellLinkW, _snwprintf, etc instead, then there is no need to convert wchar_t strings to char strings and vice versa.Ichthyoid
And I just noticed that bweber did in fact use the SHGetKnownFolderPath function in his answer. The main reason why I didn't read their answer is that I saw "Qt" and immediately skipped it over, since I don't and won't use Qt so I assumed there would be some Qt-essential feature they required. The "right thing" to do in my opinion would be to remove any Qt dependency in that answer instead. I'll keep my answer here as a complete example which doesn't require any dependencies.Sherlocke
It may not have dependencies, but it has unnecessary data conversions that can and should be avoided to prevent any unwanted data loss.Ichthyoid

© 2022 - 2024 — McMap. All rights reserved.