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

前端 未结 3 1319
借酒劲吻你
借酒劲吻你 2020-12-16 12:56

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条回答
  •  臣服心动
    2020-12-16 13:46

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

    #include 
    
    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 
    

    On Windows

    start 
    

    On MacOS

    open 
    

    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(".");
    

提交回复
热议问题