4

I am writing a program in C++ using Visual studio, what I need to do is create an HTML file and write data in it, and then I wish to get it opened in the browser. Right now I can create file, write stuff but I cannot open it, can anyone help?

It maybe an easy question, but I am just a beginner.

vin
  • 869
  • 4
  • 17
  • 33

4 Answers4

5
#include <windows.h>

void main()
{  
   ShellExecute(NULL, "open", "http://dreamincode.net",
                NULL, NULL, SW_SHOWNORMAL);
}

http://www.dreamincode.net/code/snippet357.htm You would simply replace the above URL shown in the code with the absolute path of your html file. It could be done with variables of course.

Sturm
  • 4,105
  • 3
  • 24
  • 39
  • I got a few downvotes, anyone care to explain why they think it deserved it? – Sturm Jul 24 '12 at 16:11
  • @uɐɥʇɐᴎ +1 No down votes not deserved for your answer. Problem is you need not to write comment and that is why people do it casually. – Pranit Kothari Aug 02 '13 at 08:39
  • Correct me if I am wrong, but I think some browsers block opening local files for security reasons. So I am not sure this is guaranteed to work unless the user has specifically configured their browser to allow it. – bremen_matt May 01 '17 at 09:26
  • How can I make this open a Google Chrome tab instead of an Internet Explorer one. Do I need to change one of the `NULL`s ? Or does this implicitly open a new tab in the default browser, therefore I need to switch from Internet Explorer to Chrome as my default Browser ? – M.Ionut May 02 '20 at 10:16
2
    void CAboutDlg::OnButton1()
{
    CString strDir;
    char buffer[255];

    GetCurrentDirectory(255, buffer);

    strDir = buffer;

    strDir.TrimRight("\\");
    strDir += "\\";
    strDir += _T("helpindex.html");

    if( 32 >= (int)ShellExecute( NULL, "open", strDir, NULL, NULL, SW_SHOWNORMAL))
    {
        AfxMessageBox("::ShellExecuteFailed"
             " to open this link!");
    }  
}
Abude
  • 2,112
  • 7
  • 35
  • 58
0

try this ... it works fine for me...

#include <windows.h>
void main()
{ 
    LPCTSTR helpFile = "c\help\helpFile.html";
    ShellExecute(NULL, "open", helpFile, NULL, NULL, SW_SHOWNORMAL);
    system("PAUSE");
}
Najam Islam
  • 83
  • 2
  • 11
  • 2
    While this answer is probably correct and useful, it is preferred if you [include some explanation along with it](http://meta.stackexchange.com/q/114762/159034) to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Kevin Brown-Silva Oct 13 '15 at 15:21
0

If you are developing for UWP:

    Windows::System::Launcher::LaunchUriAsync(ref new Uri("https://www.google.com"));
Andrey
  • 5,932
  • 3
  • 17
  • 35