Solved

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

Posted on 1998-06-20
4
470 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
[X]
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
  • 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

Industry Leaders: 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

691 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