24

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

3 Answers 3

32

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.

2
7

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(".");
3
  • Suggestion: use a different identifier (open has a POSIX meaning), make the argument const and maybe return something to indicate success/failure: int abranhe_open(const char *url);
    – pmg
    Jan 23, 2019 at 19:29
  • @pmg thanks for your fast feedback, I'll work on it, int opening(...) or int opn(...) would be great
    – Abraham
    Jan 23, 2019 at 19:35
  • Thanks for this nugget, in my case the answer by lornova didn't work unless I specified the application's path/exe name
    – jsonV
    Dec 7, 2019 at 11:13
4

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.

5
  • system("cmd /c start http://stackoverflow.com/"); no need to #include <windows.h> when using system(), /c is for "Run command and then terminate"
    – netcat
    Oct 3, 2016 at 20:47
  • 1
    system("cmd /c start \"\" \"URL\") so it can handle special chars like &
    – netcat
    Oct 5, 2016 at 12:50
  • Hi netcat, I think you have a typo or two there. Surely: system( "cmd /c start \"URL\"" ) May 26, 2019 at 15:31
  • @SwissFrank, no, @netcat is correct. When passing a speech-mark enclosed argument to start, the first argument must be a speech-mark enclosed title for the command window. Feb 17, 2020 at 16:30
  • Windows 10 now supports opening a URL without having to pass it through cmd. i.e. system("start \"\" \"http://example.com/page?url=with&parameters#and_anchor\""); works for me. Feb 17, 2020 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.