Use ShellExecute to launch the default Web browser


This article provides general information about how to register the default Web browser and then how to start the default Web browser with the ShellExecute application programming interface (API). It also provides details on how ShellExecute causes Microsoft Internet Explorer to go to a Uniform Resource Locator (URL).

More Information

Determine the Default Web Browser

By default, Microsoft Internet Explorer checks to see if it is the default Web browser each time that 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 then prompts you to make it the default browser.

When you select
Make this the default browser in Internet Explorer and in later versions of Netscape, the browser registers itself into all of the following keys to make itself the default browser (among other entries that are 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

Start the Default Web Browser from Your Application

Call the ShellExecute API and pass it a URL. That is the easiest way for you to start the default Web browser from your application. If the default Web browser currently runs,
ShellExecute tells the instance that runs to go to your Uniform Resource Locator (URL). If it is not running,
ShellExecute starts the application and then browses to your URL.

If you used Microsoft Visual C++ to develop your application, the following code describes how to call ShellExecute.
LONG r = ShellExecute(NULL, "open", "", NULL, NULL, SW_SHOWNORMAL);
If you used Microsoft Visual Basic, you must insert the
ShellExecute declaration in your project. You can find this declaration in the Win32api.txt file that is located in a sub-folder of your Microsoft Visual Studio installation. Optionally, you may copy it from the API Text Viewer tool that comes with Visual Studio 6.0.

To insert the
ShellExecute API into your project, follow these steps:
  1. Create a new project in Visual Basic.
  2. Select Standard EXE.
  3. Add a button to your form.
  4. Put 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", "", 0, 0, 1)
    End Sub
  5. Run your project, and then click the button to go to your URL with the default Web browser.
The ShellExecute API is supported on Microsoft Windows 95 and Microsoft Windows NT 3.1 and later. You can use
ShellExecute 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 Microsoft Windows 3.x, UNIX, or on MacIntosh platforms.

How ShellExecute Works

The following background is for informational purposes only. It is provided so that you can better understand how your application interacts with the operating system. Do not base your designs on assumptions that you draw from this information. Be aware that this functionality may change in future versions of Microsoft products.

How ShellExecute Interprets the URL Passed

ShellExecute parses the string that is passed to it so that ShellExecute can extract either a protocol specifier or an extension. Next, ShellExecute looks in the registry and then uses either the protocol specifier or the extension to determine which application to start. If you pass to ShellExecute,
ShellExecute recognizes the http:// sub-string as a protocol, which causes ShellExecute to view
HKEY_CLASSES_ROOT\http\shell\open for information about how to run. If you pass myfile.htm to
ShellExecute, ShellExecute recognizes the ".htm" sub-string as an extension. This causes ShellExecute to view HKEY_CLASSES_ROOT\.htm, which leads to

Typically, it is best to fully specify your URL in the string that is passed to ShellExecute, for example: instead of When you fully specify the URL, you make sure that ShellExecute knows exactly which protocol you want. By default, however, ShellExecute detects some patterns that include www.* and ftp.*, and then maps those patterns to the Hypertext Transfer Protocol (HTTP) protocol and the File Transfer Protocol (FTP), respectively.

How ShellExecute Determines Whether to Start a New Instance

When ShellExecute looks through the registry, it looks for the shell\open subkey. If the
shell\open\ddeexec key is defined, then a Dynamic Data Exchange (DDE) message with the specified application IExplore and the 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 goes to the requested URL. If no application responds to this DDE message, then ShellExecute uses the information that is contained in the shell\open\command subkey to start the application. It then re-broadcasts the DDE message to go to the requested URL.


For more information about default Web browsers, click the following article number to view the article in the Microsoft Knowledge Base:

153774 Internet Explorer not configured as default browser

The third-party products that this article discusses are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, about the performance or reliability of these products.

ID članka: 224816 - posljednja izmjena: 31. srp 2007. - verzija: 1

Povratne informacije