Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open the default web browser in Windows in C?

Tags:

c

windows

In C in Windows, how do I open a website using the default browser? In Mac OS X, I do system("open http://url");

like image 308
Daniel Avatar asked Jun 14 '10 12:06

Daniel


People also ask

How do I find my default browser in C#?

The value of HKEY_CURRENT_USER\Software\Clients\StartMenuInternet should give you the default browser name for this user. You can use the name found in HKEY_CURRENT_USER to identify the default browser in HKEY_LOCAL_MACHINE and then find the path that way.

How do I make C++ my default browser?

Just use ShellExecute[Ex] and specify a URL. It will handle opening the user's default browser.

What is the default Web browser of Windows operating system?

The adoption rate of Internet Explorer seems to be closely related to that of Microsoft Windows, as it is the default web browser that comes with Windows.


3 Answers

You have to use ShellExecute().

The C code to do that is as simple as:

ShellExecute(NULL, "open", "http://url", NULL, NULL, SW_SHOWNORMAL);

This was documented by Microsoft Knowledge Base article KB 224816, but unfortunately the article has been retired and there's no archived version of it.

like image 154
lornova Avatar answered Oct 13 '22 05:10

lornova


To open a URL in your default browser you could use shell commands and system() like this:

#include <stdlib.h>

int main(void)
{
  system("open https://example.com");
}

open is the default command to open stuff on MacOS, but what happens when you want to open a URL on Windows, Linux, or another operating system?

Well, you will need to change that open command.

On Linux

xdg-open <link>

On Windows

start <link>

On MacOS

open <link>

But there is good news, you don't need to handle that, I already created a module/package/library and you can install it using CLIB. It is cross-platform, already handle the operating systems stuff, and it is super easy to include it on your project.

Installation

$ clib install abranhe/opener.c

Usage

#include "opener.h"

int main(void)
{
    opener("https://example.com");
    return 0;
}

Since it is written using the shell commands, you are also able to open local directories.

// Open current directory
opener(".");
like image 45
abranhe Avatar answered Oct 13 '22 05:10

abranhe


In Windows, you can use start http://url on the command line to open an URL in the default browser. However, this seems to be specific to the command prompt and is not a real executable, so I don't think you can start it from your C/C++ program.

like image 42
Sjoerd Avatar answered Oct 13 '22 05:10

Sjoerd