Reputation: 535
I tried the solutions in Launching Microsoft Edge with URL from code and How to open URL in Microsoft Edge from the command line? but they do not work for me.
Here's my code:
std::string url = "http://www.test.com";
std::wstring quotedArg = L"microsoft-edge:\"" + url + L"\"";
std::vector<WCHAR> argBuff(quotedArg.w_size() + 1);
wcscpy_s(&argBuff[0], argBuff.size(), quotedArg.w_str());
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
si.cb = sizeof si;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
if (!CreateProcess(L"start", &argBuff[0], NULL, NULL, FALSE,
0, NULL, NULL, &si, &pi)) {
DWORD error = GetLastError(); // here error = 2
return false;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
The error code after CreateProcess()
is 2, which in https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx stands for ERROR_FILE_NOT_FOUND
.
Update 1:
To Dúthomhas's questions:
I'm not binding the user with Edge. I was using ShellExecuteEx()
to open http/https URL as below snippet.
SHELLEXECUTEINFO sei = { };
sei.cbSize = sizeof sei;
sei.nShow = SW_SHOWNORMAL;
sei.lpFile = url.w_str();
sei.lpVerb = L"open";
sei.fMask = SEE_MASK_CLASSNAME;
sei.lpClass = url.startsWith("https:")
? L"https"
: L"http";
if (ShellExecuteEx(&sei)) {
return true;
}
However this does not work for Microsoft Edge and will pop up error dialog saying
<URL> The specified module could not be found
.
Update 2:
Put the full path of cmd /C start
in CreateProcess()
as suggested by Dúthomhas make the call succeeds,
wui::string quotedArg = L"/C start microsoft-edge:" + url;
std::vector<WCHAR> argBuf(quotedArg.w_size() + 1);
wcscpy_s(&argBuf[0], argBuf.size(), quotedArg.w_str());
CreateProcess(L"C:\\Windows\\System32\\cmd.exe", &argBuf[0], NULL,
NULL, FALSE, 0, NULL, NULL, &si, &pi)
But the result is no browser opened and a pop up dialog showing
microsoft-edge:<UR> The specified module could not be found
.
Upvotes: 2
Views: 6307
Reputation: 10048
You are doing a few weird things, like using a std::vector instead of a straight-up std::wstring. (It is okay to pass the .c_str() to functions like this.)
In any case, always make sure to read the documentation for functions like CreateProcess.
You must supply the complete command-line for your your process, not just pieces. There is no executable named 'start' -- it is a subcommand of cmd.exe. So if you use CreateProcess you must also give the full, true command-line:
C:\Windows\System32\cmd.exe /C start microsoft-edge:http://www.test.com
All said, however, you really shouldn't be dictating to your user which browser he should be using. Your user has selected a default browser because that is the browser he wishes to use. When you subvert that choice and launch a different browser anyway, he will hate your software.
[edit] Well, I don't have Windows 10 installed, so I haven't messed around with Edge, but it appears that MS has not given it the standard command-line abilities. I cannot fathom why.
Nevertheless, it looks like you might have to stick with the 'start' command "protocol", with the method used by your Update 2, simply leaving out the "microsoft-edge" part.
The only other recourse that I can suggest (and I don't know enough about Windows 10 to say that this is necessarily the only correct way) is to look in the Registry to see if edge is installed, and start the browser with the correct method.
Alas.
Upvotes: 3
Reputation: 612993
From what I can see, you are making rather heavy weather of this. Certainly, it is not as complicated as it seems. The following code brings up an Edge window, and navigates to the desired site:
#include <Windows.h>
int main()
{
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
SHELLEXECUTEINFOW sei = { sizeof sei };
sei.lpVerb = L"open";
sei.lpFile = L"microsoft-edge:http://www.stackoverflow.com";
ShellExecuteExW(&sei);
}
I suspect that you were getting your quotes in a muddle.
Upvotes: 5