Solved

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

Posted on 1998-06-20
4
468 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

749 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