Solved

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

Posted on 1998-06-20
4
462 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
Comment Utility
Edited text of question
0
 

Author Comment

by:rljj
Comment Utility
Edited text of question
0
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now