【发布时间】:2016-01-28 19:06:59
【问题描述】:
我尝试了Launching Microsoft Edge with URL from code 和How to open URL in Microsoft Edge from the command line? 中的解决方案,但它们对我不起作用。
这是我的代码:
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);
CreateProcess()后面的错误码是2,在https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx中代表ERROR_FILE_NOT_FOUND。
更新 1:
对于 Dúthomhas 的问题:
我没有将用户与 Edge 绑定。我使用ShellExecuteEx() 打开http/https URL,如下sn-p。
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;
}
但这不适用于 Microsoft Edge,并且会弹出错误对话框提示
<URL> The specified module could not be found.
更新 2:
按照Dúthom的建议将cmd /C start的完整路径放入CreateProcess(),使调用成功,
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)
但结果是没有打开浏览器并显示弹出对话框
microsoft-edge:<UR> The specified module could not be found.
【问题讨论】:
-
@david-heffernan 这不是“如何从命令行在 Microsoft Edge 中打开 URL”的副本,因为该答案中的解决方案在这里不起作用。
-
这不是重复的工作方式。大概你只需要使用
ShellExecuteEx。
标签: c++ windows-10 microsoft-edge