Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch web page from my application

Ok, this probably has a really simple answer, but I've never tried to do it before: How do you launch a web page from within an app? You know, "click here to go to our FAQ", and when they do it launches their default web browser and goes to your page. I'm working in C/C++ in Windows, but if there's a broader, more portable way to do it I'd like to know that, too.

like image 948
Matt Avatar asked Sep 30 '08 13:09

Matt


People also ask

How do I get an app to open instead of browser?

Every android app will have list of urls that it can open. So you have to go to that app settings and tell that it should open in browser for the urls and not in the app. To do that go to Settings -> Apps -> scroll down to the app that you don't want URLs to open in -> Tap on 'Open by Default' and select always Ask.


5 Answers

#include <windows.h>

void main()
{
   ShellExecute(NULL, "open", "http://yourwebpage.com",
            NULL, NULL, SW_SHOWNORMAL);
}
like image 113
Patrick Desjardins Avatar answered Oct 22 '22 23:10

Patrick Desjardins


I believe you want to use the ShellExecute() function which should respect the users choice of default browser.

like image 36
Brian Ensink Avatar answered Oct 23 '22 00:10

Brian Ensink


Please read the docs for ShellExecute closely. To really bulletproof your code, they recommend initializing COM. See the docs here, and look for the part that says "COM should be initialized as shown here". The short answer is to do this (if you haven't already init'd COM):

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)

like image 39
twk Avatar answered Oct 22 '22 23:10

twk


For the record (since you asked for a cross-platform option), the following works well in Linux:

#include <unistd.h>
#include <stdlib.h>

void launch(const std::string &url)
{
  std::string browser = getenv("BROWSER");
  if(browser == "") return;

  char *args[3];
  args[0] = (char*)browser.c_str();
  args[1] = (char*)url.c_str();
  args[2] = 0;

  pid_t pid = fork();
  if(!pid)
    execvp(browser.c_str(), args);
}

Use as:

launch("http://example.com");
like image 27
nico Avatar answered Oct 22 '22 23:10

nico


You can use ShellExecute function. Sample code:

ShellExecute( NULL, "open", "http://stackoverflow.com", "", ".", SW_SHOWDEFAULT );
like image 3
zakker Avatar answered Oct 23 '22 00:10

zakker