Andrew Truckle
Andrew Truckle

Reputation: 19117

Starting Microsoft Edge from MFC with web file paramater

If I open up a console prompt I can type this command:

start msedge "d:\HTML\Verticle Alignment.HTM"

It starts Microsoft Edge and opens the web page.

So I tried to do this programmatically in a test program using MFC:

void CMFCApplication8Dlg::OnBnClickedButton1()
{
    ExecuteProgram(_T("start"), _T("msedge d:\\HTML\\Verticle Alignment.HTM"));
}


BOOL CMFCApplication8Dlg::ExecuteProgram(CString strProgram, CString strArguments)
{
    SHELLEXECUTEINFO    se = { 0 };
    MSG                 sMessage;
    DWORD               dwResult;

    se.cbSize = sizeof(se);
    se.lpFile = strProgram;
    se.lpParameters = strArguments;
    se.nShow = SW_SHOWDEFAULT;
    se.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShellExecuteEx(&se);

    if (se.hProcess != nullptr)
    {
        do
        {
            dwResult = ::MsgWaitForMultipleObjects(1, &(se.hProcess), FALSE,
                INFINITE, QS_ALLINPUT);
            if (dwResult != WAIT_OBJECT_0)
            {
                while (PeekMessage(&sMessage, nullptr, NULL, NULL, PM_REMOVE))
                {
                    TranslateMessage(&sMessage);
                    DispatchMessage(&sMessage);
                }
            }
        } while ((dwResult != WAIT_OBJECT_0) && (dwResult != WAIT_FAILED));

        CloseHandle(se.hProcess);
    }

    if ((DWORD_PTR)(se.hInstApp) < 33)
    {
        // Throw error
        AfxThrowUserException();
        return FALSE;
    }

    return TRUE;
}

But when I run it I get this error message:

Error

So how can I launch my file in Microsoft Edge? I am using the latest Windows 10 so it is Microsoft Edge Chromium.

I have seen other questions which refer to making Edge the default browser and then just "opening" the data file and it working it all out but this is not OK in this case. In my editor I have a menu flyout which lists all the installed browsers (excluding Edge for now). But i want to add Edge so need to be able to programatically start it with my file to view.

Upvotes: 0

Views: 364

Answers (1)

Andrew Truckle
Andrew Truckle

Reputation: 19117

Based on the comments provided to me, and to factor in for spaces in the file name, this works:

ExecuteProgram(_T("msedge"), _T("\"d:/HTML/Verticle Alignment.HTM\""));
  • The program to execute needs to be msedge and not start.
  • The parameter needs to be wrapped in " quotes.

Upvotes: 1

Related Questions