dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Working with Registry

  1. #1
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180

    Working with Registry

    Hi Guys

    Please provide with some sample code to Create/Modify/Delete Keys/Subkeys and assign values to them.
    I searched the site and studied the sample
    registryjob.zip posted by Cimperiali - this works well but too large for me to figure out what's where. Shall be thankful if anybody can provide me with something simpler.

    Thanks

    Suhaib

  2. #2
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    'Author: Ananth
    'Main Purpose :
    'To create/modify windows registry key settings

    Option Explicit
    Public Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
    Public Declare Function RegCreateKeyEx Lib "advapi32" 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, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
    Public Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
    Public Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
    Public Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
    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
    ' Reg Data Types...
    Const REG_SZ = 1 ' Unicode nul terminated string
    Const REG_EXPAND_SZ = 2 ' Unicode nul terminated string
    Const REG_DWORD = 4 ' 32-bit number

    ' Reg Create Type Values...
    Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted

    ' Reg Key Security Options...
    Const READ_CONTROL = &H20000
    Const KEY_QUERY_VALUE = &H1
    Const KEY_SET_VALUE = &H2
    Const KEY_CREATE_SUB_KEY = &H4
    Const KEY_ENUMERATE_SUB_KEYS = &H8
    Const KEY_NOTIFY = &H10
    Const KEY_CREATE_LINK = &H20
    Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
    Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
    Const KEY_EXECUTE = KEY_READ
    Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL


    Public Const HKEY_LOCAL_MACHINE = &H80000002 ' Reg Key ROOT Types...

    Const ERROR_NONE = 0 ' Return Value...
    Const ERROR_BADKEY = 2
    Const ERROR_ACCESS_DENIED = 8
    Const ERROR_SUCCESS = 0

    Private Type SECURITY_ATTRIBUTES '- Registry Security Attributes TYPE...
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Boolean
    End Type

    Public Function UpdateKey(KeyRoot As Long, KeyName As String, SubKeyName As String, SubKeyValue As String, Optional dwDowrd As Integer) As Boolean
    Dim rc As Long ' Return Code
    Dim hKey As Long ' Handle To A Registry Key
    Dim hDepth As Long '
    Dim lpAttr As SECURITY_ATTRIBUTES ' Registry Security Type

    lpAttr.nLength = 50 ' Set Security Attributes To Defaults...
    lpAttr.lpSecurityDescriptor = 0 ' ...
    lpAttr.bInheritHandle = True ' ...

    '- Create/Open Registry Key...
    rc = RegCreateKeyEx(KeyRoot, KeyName, _
    0, REG_SZ, _
    REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
    hKey, hDepth) ' Create/Open //KeyRoot//KeyName

    If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError ' Handle Errors...


    '- Create/Modify Key Value...
    If (SubKeyValue = "") Then SubKeyValue = " " ' A Space Is Needed For RegSetValueEx() To Work...

    ' Create/Modify Key Value
    If dwDowrd = 0 Then
    rc = RegSetValueEx(hKey, SubKeyName, _
    0, REG_SZ, _
    SubKeyValue, LenB(StrConv(SubKeyValue, vbFromUnicode)))
    Else
    ' MsgBox (SubKeyValue)
    rc = RegSetValueExLong(hKey, SubKeyName, 0&, REG_DWORD, CLng(SubKeyValue), REG_DWORD)
    ' rc = RegSetValueEx(hKey, SubKeyName, _
    ' 0, REG_DWORD, _
    ' SubKeyValue, LenB(StrConv(SubKeyValue, vbFormCode)))
    End If

    If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError ' Handle Error

    '- Close Registry Key...
    rc = RegCloseKey(hKey) ' Close Key

    UpdateKey = True ' Return Success
    Exit Function ' Exit
    CreateKeyError:
    UpdateKey = False ' Set Error Return Code
    rc = RegCloseKey(hKey) ' Attempt To Close Key
    End Function
    Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String) As String
    Dim i As Long ' Loop Counter
    Dim rc As Long ' Return Code
    Dim hKey As Long ' Handle To An Open Registry Key
    Dim hDepth As Long '
    Dim sKeyVal As String
    Dim lKeyValType As Long ' Data Type Of A Registry Key
    Dim tmpVal As String ' Tempory Storage For A Registry Key Value
    Dim KeyValSize As Long ' Size Of Registry Key Variable

    ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
    gv_Reg_type = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' Open Registry Key
    rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_READ, hKey) ' Open Registry Key

    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' Handle Error...

    tmpVal = String$(1024, 0) ' Allocate Variable Space
    KeyValSize = 1024 ' Mark Variable Size


    ' Retrieve Registry Key Value...
    rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
    lKeyValType, tmpVal, KeyValSize) ' Get/Create Key Value

    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' Handle Errors

    tmpVal = Left$(tmpVal, InStr(tmpVal, Chr(0)) - 1)


    ' Determine Key Value Type For Conversion...
    Select Case lKeyValType ' Search Data Types...
    Case REG_SZ, REG_EXPAND_SZ ' String Registry Key Data Type
    sKeyVal = tmpVal ' Copy String Value
    Case REG_DWORD ' Double Word Registry Key Data Type
    For i = Len(tmpVal) To 1 Step -1 ' Convert Each Bit
    sKeyVal = sKeyVal + Hex(Asc(Mid(tmpVal, i, 1))) ' Build Value Char. By Char.
    Next
    sKeyVal = Format$("&h" + sKeyVal) ' Convert Double Word To String
    End Select

    GetKeyValue = sKeyVal ' Return Value
    rc = RegCloseKey(hKey) ' Close Registry Key
    Exit Function ' Exit

    GetKeyError: ' Cleanup After An Error Has Occured...
    GetKeyValue = vbNullString ' Set Return Val To Empty String
    rc = RegCloseKey(hKey) ' Close Registry Key
    End Function

  3. #3
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    http://www.vb-helper.com/howtoint.htm

    Go to this site.
    Search by registry. there are many examples

  4. #4
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    Hi Dinesh,
    Thanx for the code.
    Is the usage given below right ??

    MsgBox GetKeyValue(&H80000002, "software/google/navclient", "brand")

    B'Coz it gave me a blank instead of a value.

    Thanks

  5. #5
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    Yes it is correct

    GetKeyValue(HKEY_LOCAL_MACHINE, "Software\My project\Common", "ProjectPath")

    make sure that Your registry exists in HKEY_LOCAL_MACHINE

    sometimes it may exists in HKEY_CURRENT_USER

  6. #6
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    OH! Dear
    it is not /
    it should be \
    Small mistake But Big Error

  7. #7
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    But... dinesh
    I checked in HKLM, this registry entry does exist
    What is "ProjectPath" here btw ?

    Thanx

    Suhaib

  8. #8
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110
    it is not /
    it should be \

    ProjectPath is my RegistryKey Like brand of Yours

  9. #9
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    Wooow !!
    Got it buddy, thanx
    Now tell me, does UpdateKey() update a registry entry if existing, else create it ?? I am yet to try this.

    Thanx

    Suhaib

  10. #10
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    UpdateKey() - This one worked too Thanx a ton !!!

  11. #11
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180
    Continuing the journey ....
    Guys, how do I get the list the Subkeys and their values under a given Key ?

    Thanks

    Suhaib

  12. #12
    Join Date
    May 2002
    Location
    Colombo,Sri Lanka
    Posts
    1,110

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)