Community discussion forum

how can i change registry value from vb

Tags:
  • 4 months ago

    You can use SaveSetting
    SaveSetting App.Title, "Form Location", "Left", "250"
    and GetSetting to read it back.

    If it is a specific key you want to write you have to use Windows API
    Here's a module to get you started
    http://pscode.com/vb/scripts/ShowCode.asp?txtCodeId=10479&lngWId=1

    It's easier in VB.NET

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 4 months ago

     What if you want to create a key in the HKEY_LOCAL_MACHINE as opposed to HKEY_CURRENT_USER?

    Is it posible to store a value and retrieve within this section.

  • 4 months ago

    declare the api functions e.g. below :-

    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long

     create a simple function to call api's e.g. below :-

    Public Function GetSettingString(hKey As hndKey, _
                                     strPath As String, _
                                     strValue As String, _
                                     Optional Default As String) As String

        Dim hCurKey As Long
        Dim lResult As Long
        Dim lValueType As Long
        Dim strBuffer As String
        Dim lDataBufferSize As Long
        Dim intZeroPos As Integer
        Dim lRegResult As Long
       
        lRegResult = RegOpenKey(hKey, strPath, hCurKey)
        lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
       
        If lRegResult = ERROR_SUCCESS Then
            If lValueType = REG_SZ Then
                strBuffer = String(lDataBufferSize, " ")
            lResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
            intZeroPos = InStr(strBuffer, Chr$(0))
                If intZeroPos > 0 Then
                    GetSettingString = left$(strBuffer, intZeroPos - 1)
                Else
                    GetSettingString = strBuffer
                End If
            End If
        Else
            If Default = "" Then
                GetSettingString = ""
            ElseIf (Default = "Initial") Then
                GetSettingString = "NoValues"
            Else
                GetSettingString = Default
                SaveSettingString hKey, strPath, strValue, Default
            End If
        End If
           
        lRegResult = RegCloseKey(hCurKey)

    End Function

     finally call you new function

    GetSettingString(HKEY_LOCAL_MACHINE, "any subordinate path goes here", "string name")

  • 4 months ago

     Thanks for this wonderful code bro, but am abit of a leaner in vb, could you please help me with the statement for saving a value in this function and also retrieving a value form this key, please i will be grateful

Post a reply

Enter your message below

Sign in or Join us (it's free).