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.

4 Answers 4

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.

4
  • I got a few downvotes, anyone care to explain why they think it deserved it?
    – Sturm
    Jul 24, 2012 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. Aug 2, 2013 at 8: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. May 1, 2017 at 9: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 NULLs ? 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 2, 2020 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!");
    }  
}
0
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");
}
1
  • 2
    While this answer is probably correct and useful, it is preferred if you include some explanation along with it 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. Oct 13, 2015 at 15:21
0

If you are developing for UWP:

    Windows::System::Launcher::LaunchUriAsync(ref new Uri("https://www.google.com"));

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.