INFO: Launching the Default Web Browser with ShellExecute

ID: Q224816


The information in this article applies to:
  • Microsoft Internet Explorer (Programming) versions 1.0, 2.0, 2.01, 2.1, 3.0, 3.01, 3.02, 4.0, 4.01, 4.01 SP1, 4.01 SP2, 5.0
  • Microsoft Windows NT, versions 3.5, 3.51, 4.0
  • Microsoft Windows versions 95, 98
  • Microsoft Windows 2000


SUMMARY

This article gives general information on how the default Web browser is registered, how to launch the default Web browser with ShellExecute, and details on how ShellExecute causes Internet Explorer to navigate to a URL.


MORE INFORMATION

Determining the Default Web Browser

By default, Internet Explorer checks to see if it is the default browser each time it starts. If Internet Document (HTML) files are associated with a different browser when Internet Explorer starts, Internet Explorer recognizes that it is not the default browser and prompts you to make it the default browser.

When you select Make this the default browser in Internet Explorer and later versions of Netscape, the browser registers itself into all of the following keys to make it the "default" browser (Even though more entries are being written).

HKEY_CLASSES_ROOT\.htm
HKEY_CLASSES_ROOT\.html
HKEY_CLASSES_ROOT\http\shell\open\command
HKEY_CLASSES_ROOT\http\shell\open\ddeexec\Application
HKEY_CLASSES_ROOT\ftp\shell\open\command
HKEY_CLASSES_ROOT\ftp\shell\open\ddeexec\Application
HKEY_CLASSES_ROOT\gopher\shell\open\command
HKEY_CLASSES_ROOT\gopher\shell\open\ddeexec\Application 

Launching the Default Web Browser from Your Application

The easiest way to launch the default Web browser from your application is to simply call the ShellExecute API and pass it a URL. If the default Web browser is currently running, ShellExecute will tell the running instance to navigate to your URL. If it is not running, ShellExecute will start the application and navigate to your URL.

If you are developing your application in Visual C++, the following code demonstrates how to call ShellExecute in your application:

LONG r = ShellExecute(NULL, "open", "http://www.microsoft.com", NULL, NULL, SW_SHOWNORMAL); 
If you are using Visual Basic, you will need to insert the ShellExecute declaration in your project. This declaration can be found in the Win32api.txt that is located in a sub-folder of your Microsoft Visual Studio installation (C:\Program Files\Microsoft Visual Studio\Common\Tools\WinApi for a default Visual Studio 6.0 installation). Or you could copy it from API Text Viewer tool that comes with Visual Studio 6.0.

To do this in Visual Basic, follow these steps:
  1. Create a new project in Visual Basic.


  2. Select Standard EXE.


  3. Add a button to your form.


  4. Place the following code in the form:
    
    Private Declare Function ShellExecute _
                                Lib "shell32.dll" _
                                Alias "ShellExecuteA"( _
                                ByVal hwnd As Long, _
                                ByVal lpOperation As String, _
                                ByVal lpFile As String, _
                                ByVal lpParameters As String, _
                                ByVal lpDirectory As String, _
                                ByVal nShowCmd As Long) _
                                As Long
    
    Private Sub Command1_Click()
       Dim r As Long
       r = ShellExecute(0, "open", "http://www.microsoft.com", 0, 0, 1)
    End Sub 


  5. Run your project, and click on the button to navigate to your URL with the default Web browser.


The ShellExecute API is supported on Windows 95 and Windows NT 3.1 and later. ShellExecute can be used to activate the default Web browser in all Win32 versions of Microsoft Internet Explorer from version 1.0 and later. This technique is not supported on the Windows 3.x, UNIX, or Mac platforms.

How ShellExecute Works

The following is provided for informational purposes only. It is intended to provide general insight so that you might better understand how your application is interacting with the operating system. However, it is not intended that you base your designs on assumptions drawn from this information. Please be aware that this functionality may change in future versions of Microsoft products.

How ShellExecute Interprets the URL Passed

ShellExecute parses the string passed to it to extract either a protocol specifier or a file extension, which it then uses to determine what application to launch by looking in the registry. If you pass "http://www.microsoft.com" to ShellExecute, the "http://" sub-string is recognized as a protocol, which causes it to look at "HKCR\http\shell\open" for information on how to execute. If you pass "myfile.htm" to ShellExecute, the ".htm" sub-string is recognized as a file extension causing it to look at "HKCR\.htm," which leads to "HKCR\htmlfile\shell\open."

Generally, it's best to fully specify your URL in the string passed to ShellExecute, as in "http://www.microsoft.com" rather than "www.microsoft.com." That way ShellExecute knows exactly which protocol you want. However, some obvious patterns like "www.*" and and "ftp.*" are detected and mapped to the http protocol and the ftp protocol, respectively, by default.

How ShellExecute Determines Whether It Should Launch a New Instance

When ShellExecute is looking through the registry, it is ultimately looking for the "shell\open" subkey. If the "shell\open\ddeexec" key is defined, then a DDE message with the specified application "IExplore" and topic "WWW_OpenURL" is broadcast to all top-level windows on the desktop. The first application to respond to this message is the application that will navigate to the requested URL. If no applications respond to this DDE message, then ShellExecute uses the information contained in the "shell\open\command" subkey to launch the application. It then re-broadcasts the DDE message to navigate to the requested URL.


REFERENCES

For more information, please see the following articles in the Microsoft Knowledge Base:

Q164539 Determining Which Version of Internet Explorer You Are Using
Q174156 HOWTO: Programmatically Launch the Default Internet Browser
Q153774 Internet Explorer Not Configured as Default Browser

Additional query words:

Keywords : kbIE300 kbIE301 kbIE401 kbWebBrowser kbWinOS95 kbWinOS98 kbGrpInet
Version : WINDOWS:1.0,2.0,2.01,2.1,3.0,3.01,3.02,4.0,4.01,4.01 SP1,4.01 SP2,5.0,95,98; winnt:3.5,3.51,4.0; :
Platform : WINDOWS winnt
Issue type : kbinfo


Last Reviewed: May 25, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.