Microsoft KB Archive/299853

= How to modify the default Web browser and e-mail client programmatically in Windows XP =

Article ID: 299853

Article Last Modified on 8/10/2007

-

APPLIES TO

 Microsoft Visual Basic 6.0 Enterprise Edition Microsoft Visual Basic 6.0 Professional Edition Microsoft Visual Basic 5.0 Enterprise Edition Microsoft Visual Basic 5.0 Professional Edition, when used with:  Microsoft Windows XP Professional

 Microsoft Windows XP Home Edition </li></ul>

-

<div class="notice_section">

This article was previously published under Q299853

<div class="summary_section">

SUMMARY
On a Windows XP-based computer, Windows XP sets the default Start menu Internet browser to Microsoft Internet Explorer and e-mail client to Microsoft Outlook Express. The Start menu Internet browser only controls the application that is started when you click the Internet or E-mail icon on the Start menu. This is separate from the default Web browser or e-mail client that is used to start arbitrary URLs from anywhere in the system. You can programmatically change the defaults for the system or individually for each user.

<div class="moreinformation_section">

MORE INFORMATION
By default, Windows XP uses a global setting in the HKeyLocalMachine (HKLM) Registry key to set an initial, default e-mail and Web browser client from the Start menu. Windows XP also implements new registry keys in HKeyCurrentUser (HKCU) to store individual internet and e-mail client information for each user as part of their profile if users select a different client than the default. The following code sample adds a Web browsing and e-mail client to the available options on the system and shows how to modify both the global and the current user: <ol> Create a new Microsoft Visual Basic &quot;Standard EXE&quot; project.</li> On the Project menu, click Add Module, click Module, and then click Open.</li>  Double-click the new module that you created, and then paste the following code into the code module. Const REG_SZ As Long = 1 Const HKEY_CURRENT_USER = &H80000001 Const HKEY_LOCAL_MACHINE = &H80000002 Const KEY_SET_VALUE = &H2 Const KEY_ALL_ACCESS = &H3F Const REG_OPTION_NON_VOLATILE = 0 Const HWND_BROADCAST = &HFFFF Const WM_SETTINGCHANGE = &H1A

Private Declare Function RegCloseKey Lib &quot;advapi32.dll&quot; _ (ByVal hKey As Long) As Long Private Declare Function RegCreateKeyEx Lib &quot;advapi32.dll&quot; _ Alias &quot;RegCreateKeyExA&quot; (ByVal hKey As Long, _ ByVal lpSubKey As String, ByVal Reserved As Long, _  ByVal lpClass As String, ByVal dwOptions As Long, _  ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, _  phkResult As Long, lpdwDisposition As Long) As Long Private Declare Function RegOpenKeyEx Lib &quot;advapi32.dll&quot; _ Alias &quot;RegOpenKeyExA&quot; (ByVal hKey As Long, _ ByVal lpSubKey As String, ByVal ulOptions As Long, _  ByVal samDesired As Long, phkResult As Long) As Long Private Declare Function RegSetValueExString Lib &quot;advapi32.dll&quot; _ Alias &quot;RegSetValueExA&quot; (ByVal hKey As Long, _ ByVal lpValueName As String, ByVal Reserved As Long, _  ByVal dwType As Long, ByVal lpValue As String, _  ByVal cbData As Long) As Long Private Declare Function SendMessage Lib &quot;user32&quot; _ Alias &quot;SendMessageA&quot; (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, lparam As String) As Long Public Function SetClient(iClient As Integer, sDisplayName As String, _ sClientCommandLine As String, sClientResourceDLL As String, _  iLocalization As Integer, bGlobalClient As Boolean, _  Optional sCLParameters As String, Optional bMakeDefault As Boolean) As Integer ' iClient - 1 for internet browser, 2 for e-mail client ' sDisplayName - the name to be displayed on the menu for the client ' sClientCommandLine - the path and filename of the e-mail client ' ' The next two parameters are included for localization of the client. ' For backwards compatibility with applications that do not support localized ' strings, the name of the application in the installed language should be set ' as the Default value for the key. ' sClientResourceDLL - provides a path to an EXE or DLL containing the '  localized strings for the client. ' iLocalization - a string resource ID within the DLL whose value is '   to be displayed to the user allowing the same registration to  '   be used for multiple languages. For each language, provide a '   different Resource DLL, and the dynamic loading of the string '  from the DLL results in the correct strings being displayed, depending '  on the language. ' ' bGlobalClient - sets the value for either all users (True) or the '  current user (False) ' sCLParameters - additional parameters on the command line to be passed to the '  browser or e-mail client. ' bMakeDefault - (Optional) set the browser or e-mail application as the default Dim iStatus As Integer Dim hHandle As Long Dim hGRegKey As String Dim hLRegKey As String Dim sCommand As String Dim sKey As String Dim sAll As String Dim sRoot As String Dim hKey As Long Dim sLoc As String hGRegKey = HKEY_LOCAL_MACHINE hLRegKey = HKEY_CURRENT_USER If iClient = 1 Then sRoot = &quot;Software\Clients\StartMenuInternet&quot; Else sRoot = &quot;Software\Clients\Mail&quot; End If ' Create and null terminate needed strings sCommand = &quot;shell\open\command&quot; sKey = sRoot & &quot;\&quot; & sDisplayName sAll = sKey & &quot;\&quot; & sCommand sLoc = &quot;@&quot; & sClientResourceDLL & &quot;,&quot; & iLocalization & Chr$(0) sClientLocation = &quot;&quot;&quot;&quot; & sClientCommandLine & &quot;&quot;&quot;&quot; & _ IIf(sCLParameters <> &quot;&quot;, &quot; &quot;, &quot;&quot;) & Trim(sCLParameters) & Chr$(0) sDisplayName = sDisplayName & Chr$(0) ' Create a registry key for the new client iStatus = RegCreateKeyEx(hGRegKey, sKey, 0&, vbNullString, _   REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal) iStatus = RegCreateKeyEx(hGRegKey, sAll, 0&, vbNullString, _   REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal) If iStatus = ERROR_NONE Then iStatus = RegOpenKeyEx(hGRegKey, sAll, 0, KEY_SET_VALUE, hKey) iStatus = RegSetValueExString(hKey, &quot;&quot;, 0&, REG_SZ, sClientLocation, _     Len(sClientLocation)) iStatus = RegCloseKey(hKey) iStatus = RegOpenKeyEx(hGRegKey, sKey, 0, KEY_SET_VALUE, hKey) iStatus = RegSetValueExString(hKey, &quot;&quot;, 0&, REG_SZ, sDisplayName, _     Len(sDisplayName)) ' Add the localization string iStatus = RegSetValueExString(hKey, &quot;LocalizedString&quot;, 0&, REG_SZ, _       sLoc, Len(sLoc)) iStatus = RegCloseKey(hKey) Else SetClient = iStatus Exit Function End If '  Sets browser as local or global default if specified If bMakeDefault Then If bGlobalClient Then iStatus = RegOpenKeyEx(hGRegKey, sRoot, 0, KEY_SET_VALUE, hKey) iStatus = RegSetValueExString(hKey, &quot;&quot;, 0&, REG_SZ, sDisplayName, _       Len(sDisplayName)) iStatus = RegCloseKey(hKey) Else iStatus = RegCreateKeyEx(hLRegKey, sRoot, 0&, vbNullString, _       REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal) iStatus = RegSetValueExString(hNewKey, &quot;&quot;, 0&, REG_SZ, _       sDisplayName, Len(sDisplayName)) iStatus = RegCloseKey(hNewKey) End If   UpdateMenus End If End Function Private Sub UpdateMenus ' Refresh the menu choices with the updated client Dim iRetVal As Integer iRetVal = SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, _   &quot;SOFTWARE\Clients\mail&quot;) iRetVal = SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, _   &quot;SOFTWARE\Clients\StartMenuInternet&quot;) End Sub Here are examples of calling this function in the module. Dim x As Integer ' Sets up a browser for the current user and makes it default x = SetClient(1, &quot;My Browser&quot;, &quot;C:\Program Files\MyBrowser\mybrowser.exe&quot;, &quot;C:\Program Files\MyBrowser\localization.dll&quot;, -123, True, &quot;-homepage&quot;, False) ' Sets up an e-mail client for the current user and makes it default x = SetClient(2, &quot;My E-mail&quot;, &quot;C:\Program Files\MyEmail\myemail.exe&quot;, &quot;C:\Program Files\MyEmail\localization.dll&quot;, -456, True, &quot;-inbox&quot;, False) </li>  Call the function in the module as follows. Dim x As Integer ' Sets up a browser for the current user and makes it default x = SetClient(1, &quot;My Browser&quot;, &quot;C:\Program Files\MyBrowser\mybrowser.exe&quot;, &quot;C:\Program Files\MyBrowser\localization.dll&quot;, -123, True, &quot;-homepage&quot;, False) ' Sets up an e-mail client for the current user and makes it default x = SetClient(2, &quot;My E-mail&quot;, &quot;C:\Program Files\MyEmail\myemail.exe&quot;, &quot;C:\Program Files\MyEmail\localization.dll&quot;, -456, True, &quot;-inbox&quot;, False) </li> On the File menu, click Make .exe, type a name for the project, and then click OK.

Note  is a placeholder for the name of the project.</li> Double-click .exe to run the program.</li></ol>

<div class="references_section">