Launch web page from my application

前端 未结 6 786
误落风尘
误落风尘 2021-02-14 09:43

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\",

相关标签:
6条回答
  • 2021-02-14 10:32

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

    0 讨论(0)
  • 2021-02-14 10:35

    For some reason, ShellExecute do not work sometimes if application is about to terminate right after call it. We've added Sleep(5000) after ShellExecute and it helps.

    0 讨论(0)
  • 2021-02-14 10:42

    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");
    
    0 讨论(0)
  • 2021-02-14 10:42

    You can use ShellExecute function. Sample code:

    ShellExecute( NULL, "open", "http://stackoverflow.com", "", ".", SW_SHOWDEFAULT );
    
    0 讨论(0)
  • 2021-02-14 10:45
    #include <windows.h>
    
    void main()
    {
       ShellExecute(NULL, "open", "http://yourwebpage.com",
                NULL, NULL, SW_SHOWNORMAL);
    }
    
    0 讨论(0)
  • 2021-02-14 10:49

    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)

    0 讨论(0)
提交回复
热议问题