Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Opening a URL in the browser from a C++ program.

Posted on 1998-06-20
4
466 Views
Last Modified: 2012-06-21
What's the most robust (and portable across browser types) technique for a VC++ program to
cause the end user's Win95 PC default browser to load a URL? I have used the following code
to do this, but have experienced that some end users report a problem that their browser
(either IE or Netscape) pop up an error message stating that the page cannot be loaded
even though the URL can be loaded "manually" by the user.

void show_url(const char* url)
{
char command[MAX_PATH+14];
GetWindowsDirectory(command, MAX_PATH);
strcat(command, "\\rundll32.exe");
if (spawnl(_P_DETACH, command, "rundll32.exe", "url.dll,FileProtocolHandler", url, (const char*) NULL) == -1)
{
   // print error message
}
}

I've also seen the following code fragment used in an example on Microsoft's web site:

ShellExecute (GetDesktopWindow(), "open", "http://www.microsoft.com/", NULL, NULL, SW_SHOW);

Any feedback or experience in a robust technique to perform this task would be appreciated.
0
Comment
Question by:rljj
  • 2
4 Comments
 

Author Comment

by:rljj
ID: 1166365
Edited text of question
0
 

Author Comment

by:rljj
ID: 1166366
Edited text of question
0
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 1166367
The ShellExecute method is the MS recommended way of doing this and I've used it a lot with good results.  The biggest problem with it that sometimes people unregister (knowingly or not) the htm/html/http file association.  This, of course, causes an error.  It's probably best to assume that there is a problem and check the registy for the proper file association before running ShellExecute.  I suspect that if you traced through ShellExecute, you'd end up finding code just like your first example.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1166368
There is an article about this in Windows Developer's Journal August 1997. You can download the source code at http://www.wdj.com. It is inside patersn.zip. It implements a function called GotoURL. The GotoURL function calls ShellExecute first. If it fails, it gets the information from the registry and run the default Web browser.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question