-1

I need to be able to create an Internet shortcut to a specific URL and always open it with Microsoft Edge. The only info that is out there seems to be [this page][1].

I'm not sure how to use this site, or look for an example on how to create an Internet shortcut with target path and URL.

Any ideas?

I did manage to find this code and was able to get it to work with either browser type or URL but not both. Tried escaping quotation marks but still nothing.

{
    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\\ShortcutTest", deskPath);

    WCHAR path[MAX_PATH] = { 0 };
    std::wstring path1 = L"C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe" "http://www.bing.com";
    SHGetFolderPathW(NULL, CSIDL_PROGRAM_FILESX86, NULL, 0, path);
    swprintf_s(p, _countof(p), path1.c_str(), path);

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

    CoUninitialize();

    return 0;
9
  • 4
    Why should you as a programmer decide that the user should always use Edge? As a user, I really dislike when programs do that. Why not open the URL with the browser the user has chosen? Commented Jul 4, 2022 at 13:58
  • 1
    Did you try searching Stack Overflow? Here is an answer with example code, on Stack Overflow, which was a top search result in a popular web search engine. stackoverflow.com/questions/3906974/… -- If you want it to always open with Edge though, consider most sane people do not use that as default browser. You may need the shortcut to target the application itself with the URL as a command-line argument. Check the Edge docs, experiment with shortcuts etc to see if it's viable before you go looking to do it programmatically. Commented Jul 4, 2022 at 13:58
  • 1
    You can create a "regular" shortcut with arguments (edge.exe, url, etc.) like shown here stackoverflow.com/a/14632782/403671 (C# but same in C++) but you cannot create an Internet Shortcut an define the browser. Commented Jul 4, 2022 at 14:15
  • 1
    @SimonMourier You can create a shortcut with a target like "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" https://www.google.com which will start Edge which will in turn open www.google.com. Commented Jul 4, 2022 at 14:37
  • @TedLyngmo - that's exactly what I said but this is not an "Internet Shortcut" Commented Jul 4, 2022 at 15:07

1 Answer 1

0

MSDN provides example code for creating shortcuts with IShellLink. This is also referenced in answers on Stack Overflow, most notably this one: How to programmatically create a shortcut using Win32

For your requirement specifically, note that the IShellLink object provides a method SetArguments. You can use this to specify the URL that will be passed on the command line when running MS Edge.

So let's expand the example CreateLink function, rearrange it a little and add a parameter that lets you provide arguments:

#include <windows.h>
#include <shlobj.h>

HRESULT CreateLink(LPCWSTR lpszShortcut,
                   LPCWSTR lpszPath,
                   LPCWSTR lpszArgs,
                   LPCWSTR lpszDesc)
{
    HRESULT hres;
    IShellLinkW* psl;

    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (LPVOID*)&psl);
    if (SUCCEEDED(hres))
    {
        IPersistFile* ppf;

        psl->SetPath(lpszPath);
        psl->SetArguments(lpszArgs);
        psl->SetDescription(lpszDesc);

        // Save link
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
        if (SUCCEEDED(hres))
        {
            hres = ppf->Save(lpszShortcut, TRUE);
            ppf->Release();
        }

        psl->Release();
    }

    return hres;
}

Now all you need is to invoke it correctly. Taking your example as a starting point:

#include <string>

int main()
{
    HRESULT hres = CoInitialize(NULL);
    if (SUCCEEDED(hres))
    {
        PWSTR deskPath, programsPath;
        SHGetKnownFolderPath(FOLDERID_Desktop, 0, NULL, &deskPath);
        SHGetKnownFolderPath(FOLDERID_ProgramFilesX86, 0, NULL, &programsPath);

        std::wstring linkFile = std::wstring(deskPath) + L"\\ShortcutTest.lnk";
        std::wstring linkPath = std::wstring(programsPath) + L"\\Microsoft\\Edge\\Application\\msedge.exe";
        LPCWSTR linkArgs = L"https://www.stackoverflow.com";
        LPCWSTR linkDesc = L"Launch Stack Overflow";

        CreateLink(linkFile.c_str(), linkPath.c_str(), linkArgs, linkDesc);

        CoTaskMemFree(deskPath);
        CoTaskMemFree(programsPath);
    }

    CoUninitialize();
    return 0;
}

Note that I also used the correct extension for the shortcut file, which is .lnk. This is required for Windows to recognize the file as a shortcut.

I also changed the method of acquiring standard folders, as per recommendation in MSDN documentation. You should generally avoid anything that uses MAX_PATH. For clarity I am not testing whether these calls succeed, but you should do so in a complete program.

Sign up to request clarification or add additional context in comments.

1 Comment

You are a lifesaver! I wasn't implementing SetArguments and I stayed away from IShellLink because of the Note about it not being able to create shortcuts with URL. Also, I haven't tested with intranet sites that we need to use but Im assuming it will work the same. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.