-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;
  • 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? – Ted Lyngmo Jul 04 '22 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. https://stackoverflow.com/questions/3906974/how-to-programmatically-create-a-shortcut-using-win32 -- 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. – paddy Jul 04 '22 at 13:58
  • 1
    You can create a "regular" shortcut with arguments (edge.exe, url, etc.) like shown here https://stackoverflow.com/a/14632782/403671 (C# but same in C++) but you cannot create an Internet Shortcut an define the browser. – Simon Mourier Jul 04 '22 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. – Ted Lyngmo Jul 04 '22 at 14:37
  • @TedLyngmo - that's exactly what I said but this is not an "Internet Shortcut" – Simon Mourier Jul 04 '22 at 15:07
  • @SimonMourier Ah, ok, sorry I misunderstood. – Ted Lyngmo Jul 04 '22 at 15:18
  • @TedLyngmo it is not me trying to decide which browser to use but the vendor that provides the application is requiring our users to use Edge. They are still using components of IE and Edge will allow a compatible version of IE to be used in its browser. – Norrin Radd Jul 04 '22 at 17:58
  • @paddy I have looked at the page you mentioned but was not able to "get it to work" for the requirement of both a designated browser and specific URL. I could only pick one or the other. I agree that Edge is not the ideal browser but it is required for the application we are using. – Norrin Radd Jul 04 '22 at 18:04
  • @JoshSpader One of the [answers](https://stackoverflow.com/a/16633100/7582247) to the question that you linked to contains a function to create links where you can provide arguments to the executable. Did you try that out? – Ted Lyngmo Jul 04 '22 at 18:13

1 Answers1

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.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • 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 – Norrin Radd Jul 05 '22 at 02:01