Results 1 to 10 of 10

Thread: Adding to a key in the registry

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2004
    Posts
    45

    Adding to a key in the registry

    how can I add a value?
    I googled it and the best I found was on the msdn (but that told me how to make a key...) Please and thanks.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Adding to a key in the registry

    You can use this:

    Code:
    SaveSetting app.name, section, Key, Setting
    use

    GetSetting ...

    to read it back.

  3. #3
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Adding to a key in the registry

    Here you go

    Code:
    'Put this under a command button
    WriteRegistry HKEY_CURRENT_USER, "SOFTWARE\MyApp\", "Start", ValString, "YourValue"
    And put the below into a module.

    Code:
    ' Possible registry data types
    Public Enum InTypes
       ValNull = 0
       ValString = 1
       ValXString = 2
       ValBinary = 3
       ValDWord = 4
       ValLink = 6
       ValMultiString = 7
       ValResList = 8
    End Enum
    ' Registry value type definitions
    Public Const REG_NONE As Long = 0
    Public Const REG_SZ As Long = 1
    Public Const REG_EXPAND_SZ As Long = 2
    Public Const REG_BINARY As Long = 3
    Public Const REG_DWORD As Long = 4
    Public Const REG_LINK As Long = 6
    Public Const REG_MULTI_SZ As Long = 7
    Public Const REG_RESOURCE_LIST As Long = 8
    ' Registry section definitions
    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_USERS = &H80000003
    Public Const HKEY_PERFORMANCE_DATA = &H80000004
    Public Const HKEY_CURRENT_CONFIG = &H80000005
    Public Const HKEY_DYN_DATA = &H80000006
    ' Codes returned by Reg API calls
    Private Const ERROR_NONE = 0
    Private Const ERROR_BADDB = 1
    Private Const ERROR_BADKEY = 2
    Private Const ERROR_CANTOPEN = 3
    Private Const ERROR_CANTREAD = 4
    Private Const ERROR_CANTWRITE = 5
    Private Const ERROR_OUTOFMEMORY = 6
    Private Const ERROR_INVALID_PARAMETER = 7
    Private Const ERROR_ACCESS_DENIED = 8
    Private Const ERROR_INVALID_PARAMETERS = 87
    Private Const ERROR_NO_MORE_ITEMS = 259
    
    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 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 RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData 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, ByVal lpData As String, lpcbData As Long) As Long
    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 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 RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
    Private Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName 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
    
    ' This routine allows you to get values from anywhere in the Registry, it currently
    ' only handles string and double word values.
    '
    ' Example
    ' Text1.Text = ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\", "DefaultUserName")
    '
    Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String) As String
    Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long, sValue As String, td As Double
    On Error Resume Next
    lResult = RegOpenKey(Group, Section, lKeyValue)
    sValue = Space$(2048)
    lValueLength = Len(sValue)
    lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue, lValueLength)
    If (lResult = 0) And (Err.Number = 0) Then
       If lDataTypeValue = REG_DWORD Then
          td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1)))
          sValue = Format$(td, "000")
       End If
       sValue = Left$(sValue, lValueLength - 1)
    Else
       sValue = ""
    End If
    lResult = RegCloseKey(lKeyValue)
    ReadRegistry = sValue
    
    
    End Function
    
    ' This routine allows you to write values into the entire Registry, it currently
    ' only handles string and double word values.
    '
    ' Example
    ' WriteRegistry HKEY_CURRENT_USER, "SOFTWARE\My Name\My App\", "NewSubKey", ValString, "NewValueHere"
    ' WriteRegistry HKEY_CURRENT_USER, "SOFTWARE\My Name\My App\", "NewSubKey", ValDWord, "31"
    '
    Public Sub WriteRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String, ByVal ValType As InTypes, ByVal Value As Variant)
    Dim lResult As Long
    Dim lKeyValue As Long
    Dim InLen As Long
    Dim lNewVal As Long
    Dim sNewVal As String
    On Error Resume Next
    lResult = RegCreateKey(Group, Section, lKeyValue)
    If ValType = ValDWord Then
       lNewVal = CLng(Value)
       InLen = 4
       lResult = RegSetValueExLong(lKeyValue, Key, 0&, ValType, lNewVal, InLen)
    Else
       sNewVal = Value
       InLen = Len(sNewVal)
       lResult = RegSetValueExString(lKeyValue, Key, 0&, 1&, sNewVal, InLen)
    End If
    lResult = RegFlushKey(lKeyValue)
    lResult = RegCloseKey(lKeyValue)
    End Sub
    And last but not least.....to read from the registry put the below under a command button.

    Code:
    ReadRegistry(HKEY_CURRENT_USER, "Software\MyApp\", "Start")

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Adding to a key in the registry

    Get/Save Settings only let you access the registry values associated with your app. If you need full access to the registry, you need something else.

  5. #5

    Thread Starter
    Banned
    Join Date
    Nov 2004
    Posts
    45

    Re: Adding to a key in the registry

    Hey thanks all, so how do I get full accsess?

  6. #6
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Adding to a key in the registry

    Use my code. It will allow you to read/write any value in the registry

  7. #7
    Lively Member Tw1sted L0gic's Avatar
    Join Date
    Jan 2005
    Posts
    88

    Re: Adding to a key in the registry

    There's a very nifty class module on Planetsourcecode.com which makes life very easy indeed.
    Naughty but Nice

  8. #8

    Thread Starter
    Banned
    Join Date
    Nov 2004
    Posts
    45

    Re: Adding to a key in the registry

    Thanks all.

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Resolved Re: Adding to a key in the registry

    Add the Checkmark and the word [RESOLVED] to the subject of the first post in your thread
    if your question has been answered satisfactorily.

  10. #10
    New Member
    Join Date
    Jan 2005
    Posts
    1

    Re: Adding to a key in the registry

    C:\My Documents\Visual Studio Projects\Valo-Ohjain\Form1.vb(152): Name 'SaveSetting' is not declared.


    I'm just starting with VB.NET and I'm not familiar with it at all. I get the above error when I try to use SaveSettings. So how do I declare it? The help for declare functions only seems to talk about self written functions.


    Edit: And now I realized the search came up with threads outside VB.NET area, though I used it there

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width