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.