Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB Windows VB.NET
iv found a code to set my browser i mad as the default browser
unfortunately this is a vb6 module code]
 
code:
    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 "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (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 "advapi32.dll" Alias "RegOpenKeyExA" (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 "advapi32.dll" Alias "RegSetValueExA" (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 "user32" Alias "SendMessageA" (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 = "Software\Clients\StartMenuInternet"
        Else
            sRoot = "Software\Clients\Mail"
        End If
 
        ' Create and null terminate needed strings
        sCommand = "shell\open\command"
        sKey = sRoot & "\" & sDisplayName
        sAll = sKey & "\" & sCommand
        sLoc = "@" & sClientResourceDLL & "," & iLocalization & Chr$(0)'<-- error
        sClientLocation_ '<-- error
 = """" & sClientCommandLine & """" & IIf(sCLParameters <> "", " ", "") & Trim(sCLParameters) & Chr$(0) '<-- error
        sDisplayName = sDisplayName & Chr$(0)'<-- error

        ' 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 '<-- error
            iStatus = RegOpenKeyEx(hGRegKey, sAll, 0, KEY_SET_VALUE, hKey)
            iStatus = RegSetValueExString(hKey, "", 0&, REG_SZ, sClientLocation,_ '<-- error 
Len(sClientLocation))'<-- error
            iStatus = RegCloseKey(hKey)
            iStatus = RegOpenKeyEx(hGRegKey, sKey, 0, KEY_SET_VALUE, hKey)
            iStatus = RegSetValueExString(hKey, "", 0&, REG_SZ, sDisplayName, Len(sDisplayName))
            ' Add the localization string
            iStatus = RegSetValueExString(hKey, "LocalizedString", 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, "", 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,_ '<-- error
 lRetVal)
                iStatus = RegSetValueExString(hNewKey,_ '<-- error
 "", 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, "SOFTWARE\Clients\mail")
        iRetVal = SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, "SOFTWARE\Clients\StartMenuInternet")
    End Sub
 
my qestion is: how do i convert it.
please help me!!
 
Bart de Lange
Posted 11-Feb-13 6:58am
Edited 20-Feb-13 6:51am
v2
Comments
Sergey Alexandrovich Kryukov at 1-Mar-13 11:03am
   
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Adam R Harris at 11-Feb-13 13:11pm
   
Good answer, was writing mine when you posted. Had i seen this i wouldn't have posted.
My 5.
ProgramFOX at 11-Feb-13 13:12pm
   
Thank you!
jibesh at 11-Feb-13 13:16pm
   
+5
ProgramFOX at 11-Feb-13 13:17pm
   
Thank you!
Bart de Lange at 11-Feb-13 13:20pm
   
sorry i didn't say i was using Visual Studio 2012 s i cant do the first 2
and its not a project or file its just the code i wrote
ProgramFOX at 11-Feb-13 13:25pm
   
Hi,
 
I think VB6 is too obsolete to upgrade from VB6 to VB.NET in Visual Studio 2012.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Slowly and painfully.
 
After looking at your code you really aren't doing anything that special so i would personally just rewrite it in VB.NET as opposed to trying to convert it. .NET has some really handy wrappers for registry access, which is what it looks like your code is doing, that you can read up more on here: Working with Windows Registry using VB.NET[^]
  Permalink  
Comments
Bart de Lange at 11-Feb-13 13:21pm
   
i managed to get it work with .html file but when i open a program and the click on a link it isn't working
Adam R Harris at 11-Feb-13 13:29pm
   
You are not looking for a web page, unless it's ASP.NET and then you are going to have all sorts of permission issues. What you are looking for is a console app or a function in a .exe (WinForms). If these concepts are a little over your head then i would suggest taking a look at some of the great articles written here to help you get a grasp of the concepts and fumble your way through creating your registry entries. Seeing as you are trying to register a browser you built im going to go right ahead and assume you are familiar with the concepts of Console vs WinForms.
Bart de Lange at 11-Feb-13 13:33pm
   
i meant when i have a .html (for example test.html) and i open it it opens in my webbrowser
Adam R Harris at 11-Feb-13 14:45pm
   
You not making much sense, did you convert you code to VB.NET? if not how did you manage to 'get it to work' and if you did what exactly is the problem.
Bart de Lange at 11-Feb-13 15:41pm
   
Ok,
With this code its posible that when i click a link in a program like Word,
it opens my webbrowser and navigates to the webpage in the link.
With another code i manager to ket this work:
I have a file which is test.html and when i double click it,
it opens my webbrowser and shows the file as a webpage,
I also want to set my browser as the full default browser so it does work with a link in a program to a site
Adam R Harris at 11-Feb-13 15:46pm
   
So your problem isn't the code you posted not working. The problem is your registry entries ... which is something completely different. Take a look at this thread, it should get you going in the right direction.
 
http://stackoverflow.com/questions/968449/how-do-i-change-default-browser-using-c-sharp-or-batch-file
Bart de Lange at 11-Feb-13 15:53pm
   
I'm going to test this,
 
This is somewhat the same as i can do with the code i entered
But i didn't know the right regkey that does the "thing"
So i thought the easiest way was to convert the code an just copy/paste it
But now i know the regkey its just easier
 
Thanks
Bart de Lange at 12-Feb-13 12:50pm
   
it still does'nt work ive tried this
My.Computer.Registry.ClassesRoot.CreateSubKey("BartHTML").SetValue("", Application.ExecutablePath & "%1", Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.CurrentUser.DeleteSubKey("Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice")
My.Computer.Registry.CurrentUser.DeleteSubKey("Software\Microsoft\Windows\Roaming\OpenWith\UrlAssociations\http\UserChoice")
My.Computer.Registry.CurrentUser.DeleteSubKey("Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice")
My.Computer.Registry.CurrentUser.DeleteSubKey("Software\Microsoft\Windows\Roaming\OpenWith\UrlAssociations\https\UserChoice")
My.Computer.Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice").SetValue("", Application.ExecutablePath, Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice").SetValue("", Application.ExecutablePath, Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\Roaming\OpenWith\UrlAssociations\http\UserChoice").SetValue("", Application.ExecutablePath & "%1", Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\Roaming\OpenWith\UrlAssociations\https\UserChoice").SetValue("", Application.ExecutablePath & "%1", Microsoft.Win32.RegistryValueKind.String)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100