Use ShellExecute to launch the default Web browser
This article was previously published under Q224816
This article has been archived. It is offered "as is" and will no longer be updated.
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).
Determine the Default Web BrowserBy 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
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.
Start the Default Web Browser from Your Application
If you used Microsoft Visual C++ to develop your application, the following code describes how to call ShellExecute.
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.
LONG r = ShellExecute(NULL, "open", "http://www.microsoft.com", NULL, NULL, SW_SHOWNORMAL);
To insert the ShellExecute API into your project, follow these steps:
- Create a new project in Visual Basic.
- Select Standard EXE.
- Add a button to your form.
- 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 LongPrivate Sub Command1_Click() Dim r As Long r = ShellExecute(0, "open", "http://www.microsoft.com", 0, 0, 1)End Sub
- Run your project, and then click the button to go to your URL with the default Web browser.
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 PassedShellExecute 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 http://www.microsoft.com to ShellExecute, ShellExecute recognizes the http:// sub-string as a protocol, which causes ShellExecute to view
HKEY_CLASSES_ROOT\http\shell\openfor 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: http://www.microsoft.com instead of www.microsoft.com. 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 InstanceWhen ShellExecute looks through the registry, it looks for the
shell\opensubkey. If the
shell\open\ddeexeckey 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\commandsubkey 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 browserThe 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.
Article ID: 224816 - Last Review: 12/05/2015 13:49:05 - Revision: 2.1
Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, Microsoft Windows 2000 Professional Edition, Microsoft Internet Explorer 3.01, Microsoft Internet Explorer 3.02, Microsoft Internet Explorer 4.0 128-Bit Edition, Microsoft Internet Explorer 4.01 Service Pack 2, Microsoft Internet Explorer 4.01 Service Pack 1, Microsoft Internet Explorer 4.01 Service Pack 2, Microsoft Internet Explorer 5.0, Microsoft Internet Explorer 5.01, Microsoft Internet Explorer (Programming) 5.01 SP1, Microsoft Internet Explorer 5.5, Microsoft Internet Explorer (Programming) 5.5 SP1, Microsoft Internet Explorer (Programming) 5.5 SP2, Microsoft Internet Explorer (Programming) 6 (SP1), Microsoft Windows NT Server 3.5, Microsoft Windows NT Server 3.51, Microsoft Windows NT Server 4.0 Standard Edition, Microsoft Windows NT Workstation 3.5, Microsoft Windows NT Workstation 3.51, Microsoft Windows NT Workstation 4.0 Developer Edition
- kbnosurvey kbarchive kbinfo kbwebbrowser KB224816