Microsoft KB Archive/307382

= How to programmatically start the default Internet browser by using Visual C++ =

Article ID: 307382

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q307382



For a Microsoft Visual C# .NET version of this article, see 305703.

For a Microsoft Visual Basic .NET version of this article, see 305705.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Diagnostics.Process
 * System.Windows.Forms

IN THIS TASK
SUMMARY
 * Requirements
 * Specify the URL, FTP, or File to Open
 * Use the Process Class Start Method to Start the Browser
 * Provide Exception Handling
 * Complete Code Sample
 * Troubleshooting

REFERENCES



SUMMARY
This article demonstrates how to start the default Internet browser by using managed extensions for Visual C++.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

back to the top

Specify the URL, FTP, or File to Open
You can specify a URL, a file, or an FTP address. All three of these assignments are valid: System::String * target= &quot;http://www.microsoft.com&quot;; System::String * target = &quot;ftp://ftp.microsoft.com&quot;; System::String * target = &quot;C:\\Program Files\\Microsoft Visual Studio\\INSTALL.HTM&quot;; back to the top

Use the Process Class Start Method to Start the Browser
The Process class contains a static Start method. Because this is a static method, you can call Start without having an instance of a Process class. System::Diagnostics::Process::Start(target); back to the top

Provide Exception Handling
Because you take advantage of the default UseShellExecute property when you call the Start method, you do not have to explicitly query the registry to determine which browser is the default. However, if you use this approach on a computer that does not have a browser installed, an exception occurs. This exception must be caught so that the appropriate action can be taken. This example explicitly traps for an error that is generated when the necessary registry key is not found and indicates that no browser is installed. In addition, a general exception handler is provided for other errors that may occur. The try...catch block is demonstrated in the complete code sample.

back to the top

Complete Code Sample
int main{ //Use no more than one assignment when you test this code. //System::String * target= &quot;http://www.microsoft.com&quot;; //System::String * target = &quot;ftp://ftp.microsoft.com&quot;; System::String * target = &quot;C:\\Program Files\\Microsoft Visual Studio\\INSTALL.HTM&quot;; try {     System::Diagnostics::Process::Start(target); }  catch (System::ComponentModel::Win32Exception * noBrowser) {     if (noBrowser->ErrorCode==-2147467259) System::Windows::Forms::MessageBox::Show(noBrowser->Message); }  catch (System::Exception * other) {     System::Windows::Forms::MessageBox::Show(other->Message); } return 0; }
 * 1) using 
 * 2) using 
 * 3) using 

back to the top

Troubleshooting
This code is highly dependent on the application-file type associations in the HKEY_CLASSES_ROOT hive of the registry. This can lead to unexpected results and exceptions if the registry is damaged. In addition, file types and extensions may be associated with applications other than the browser. For example, HTM or HTML files may be associated with Web development software instead of the browser.

back to the top

