Search code examples
visual-c++browserc++-clishellexecute

Way to launch a browser w/ specific webpage without using ShellExecute? (Visual C++)


I want to add a button to my visual C++ form that will open with a specific browser. So far for links I've been using:

System::Diagnostics::Process::Start("UrlHere")

Which, as standard, opens with whatever your default browser is.

I'm wondering what the process would be to force the URL to open with a specific browser and if it's possible without the use of ShellExecute?

Edit - You are correct, this is C++/CLI. Removed the C++ Tag.

Edit Edit - Apologies if it came across as misleading. Some slight elaboration;

The buttons will launch to application URL's, some of which can only be used in Internet Explorer, others that CAN (and should) be used in Chrome. This is why I need to avoid using the default browser and have different buttons using different browsers when launching URLs


Solution

  • Before answering the 'how', I'd like to ask the question "should you be implementing this?"

    • By not launching the user's default browser, you are subverting the user's decision.
      • Perhaps the user prefers a particular interface, and is willing to live with the incorrect renderings that come with it.
      • Perhaps the user has a browser addon that they really need, such as a screen reader for the blind.
    • You are requiring additional software installed that the user may or may not want.
      • Perhaps the user doesn't want Chrome. Perhaps the user prefers FireFox.
    • You are saying that you know which browser is best, now and forever.
      • What if the next version of IE makes it work with the sites that are currently Chrome-only? What if the next version of Chrome fixes the sites that are currently IE-only?
      • What if the site changes so that it works in more browsers?
      • Do you go back and release a new version of your software that changes the browser for particular sites?
    • You're trying to solve a problem that may already be fixed.
      • Both Chrome and Firefox support a addon that will render a tab using the IE engine. It can be set to automatically activate when certain URLs are seen.
      • Perhaps there is a browser that already works with all your sites, that you don't know about.

    Therefore, my recommendation is no, do not do this. The user has decided which browser they want to use, respect that decision and use the default browser.

    That said, here's how you would do it: You could use the CreateProcess method, but you're in managed-land, so you might as well use it. Use the Process class to launch the new process for you.

    Process^ browserProcess = gcnew Process();
    browserProcess->StartInfo->UseShellExecute = false;
    browserProcess->StartInfo->FileName = "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe";
    browserProcess->StartInfo->Arguments = "http://www.google.com";
    browserProcess->Start();