Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
Load and store *.pnm image file 1 75
how to convert c++ code to Android App 3 80
Would like to move button in a function 3 71
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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 clear a vector as well as how to detect empty vectors in C++.

914 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

17 Experts available now in Live!

Get 1:1 Help Now