如何使用 C++ 代码中的特定 URL 启动 Microsoft Edge

Lee*_*Lee 2 c++ windows-10 microsoft-edge

我尝试了使用代码中的 URL 启动 Microsoft Edge如何从命令行在 Microsoft Edge 中打开 URL 中的解决方案但它们对我不起作用。

这是我的代码:

    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);
Run Code Online (Sandbox Code Playgroud)

后面的错误代码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,如下所示。

    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;
    }
Run Code Online (Sandbox Code Playgroud)

但是,这不适用于 Microsoft Edge,并且会弹出错误对话框说

<URL> The specified module could not be found.


更新 2:

放的完整路径cmd /C startCreateProcess()由Dúthomhas的建议使调用成功,

    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)
Run Code Online (Sandbox Code Playgroud)

但结果是没有打开浏览器并显示一个弹出对话框

microsoft-edge:<UR> The specified module could not be found.

Dav*_*nan 5

据我所知,你正在制造相当恶劣的天气。当然,它并不像看起来那么复杂。以下代码会打开一个 Edge 窗口,并导航到所需的站点:

#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);
}
Run Code Online (Sandbox Code Playgroud)

我怀疑你是在混淆你的报价。