How do I start the MS Edge browser on a file from C++

In WIn 7 and WIn 10, the program attached opens IE on a file on the same machine.  But I need to make it open the new  Microsoft Edge browser instead in Windows 10.  Just changing the path to Edge does not seem to work.
openBrowser.cpp
anAppBuilderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Try 'ShellExecute()' (https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx) instead of 'popen()', e.g.

void startBrowser(){		//Start IE on a file on the current machine
	char* charHtmlPath = "C:\\Users\\...\\myFile.html\"";
	FILE *theBrowser;
	string cmdToStart, htmlPath, ieOnFile, iePath, targetHtmlPath;

	//Old code that starts IE on the file. Works in Win 7 and Win 10
	iePath = "\"C:\\\\Program Files (x86)\\Internet Explorer\\iexplore.exe\" ";

	//New code that does not work.
	//iePath = "\"C:\\\\Program Files\\WindowsApps\\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge\" ";

	htmlPath = charHtmlPath;
	targetHtmlPath = "\"file:///" + htmlPath + "\"";
	ieOnFile = "\"" + iePath + targetHtmlPath;

	ShellExecute(NULL,"open",ioOnFile.c_str(),NULL,NULL,SW_SHOW); // <---
	return;
}

Open in new window

anAppBuilderAuthor Commented:
Thank you jkr.  I'm compiling in minGW and it looks like I'm missing a library that it needs to resolve ShellExecute.  Could you please tell me what library I need to bring in?
jkrCommented:
The MSDN link above list these as


Header
Shellapi.h

Library
Shell32.lib

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

anAppBuilderAuthor Commented:
Thank you again, jkr.  And my apologies for a slow response.  I've been reading and testing.  The openSpecificBrowser code below works for IE and Chrome, but does not open Edge, so something is still wrong.  I hope you can help.

Additional comment:
The second code snippet below opens the default browser.  It does in fact open Edge if Edge is the default, but I am asking for a way to open Edge specifically. It is based on http://www.experts-exchange.com/articles/1595/Execute-a-Program-with-C.html
openSpecificBrowser.cpp
openDefaultBrowser.cpp
jkrCommented:
Sorry for the delay, was unavailable for a couple of days. Apparently it's a bit more complicated to launch Edge, see https://social.msdn.microsoft.com/Forums/en-US/8558e8d0-3d84-43a6-8716-4959aa0155b7/how-do-i-launch-edge-from-my-application?forum=vcgeneral

The scoop is to

#include "windows.h"
#include <atlbase.h> 
#include <atlstr.h>

#include <Shobjidl.h>
#include "Shellapi.h"


int main()
{
	HRESULT hr = E_FAIL;
	hr = CoInitialize(NULL);
	CComPtr<IApplicationActivationManager> activationManager;
	LPCWSTR edgeAUMID = L"Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge";
	//LPCWSTR edgeAUMID = L"Microsoft.Windows.Spartan_cw5n1h2txyewy!Microsoft.Spartan.Spartan";
	hr = CoCreateInstance(CLSID_ApplicationActivationManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&activationManager));
	if (SUCCEEDED(hr))
	    {
	        DWORD newProcessId;
	        hr = activationManager->ActivateApplication(edgeAUMID, L"www.microsoft.com", AO_NONE, &newProcessId);
	    }
	else
	    {
	        std::wcout << L"Failed to launch Microsoft Edge";
	    }

        return 0;
}

Open in new window

anAppBuilderAuthor Commented:
Thank you again, jkr.  I am using MinGW not Visual Studio so I don't automatically have the libraries referenced by these headers:
#include <atlbase.h> 
#include <atlstr.h>

Open in new window

This link "stackoverflow.com/questions/3898287/c-include-atlbase-h-is-not-found" suggests downloading Windows Driver Kit Version 7.1.0. Of course that's obsolete, but when I look at the latest WDK here "https://msdn.microsoft.com/en-us/library/windows/hardware/ff557573(v=vs.85).aspx", it appears that "WDK 10 is integrated with Microsoft Visual Studio 2015..." Changing to Visual Studio is not an option for what I'm doing.  Is the really the only way to start Edge from C++?
anAppBuilderAuthor Commented:
Thank you again, jkr.  I dug a bit more and found a good pointer here: stackoverflow.com/questions/31164253/how-to-open-url-in-microsoft-edge-from-the-command-line/32281705#32281705. That allowed me to get to this:
ShellExecute( 0, "open", "microsoft-edge:http://www.experts-exchange.com//", NULL, "", SW_SHOWNORMAL)

Open in new window

  I tested this (see code snippet below) for opening both web addresses and local files. Local files open properly, but note the format for a local file with spaces in the file path.
openMSEdgeBrowser.cpp
anAppBuilderAuthor Commented:
Thank you, jkr, for pointing me in the right direction.  And I'm very glad that there is something simpler than the Microsoft Forums solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 10

From novice to tech pro — start learning today.