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

Thread: Registry

  1. #1
    Join Date
    Aug 2000
    Posts
    29

    Registry

    i would like to know how to access a certain key in the registry i.e. BinDirPath key in HKEY_LOCAL_MACHINE\Software\Micorsoft\Office\8.0.
    thanks hope you could help me.


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Registry

    refer http://vblib.virtualave.net, there is a class vbRegistry in the activeX DLL which wrap the API for ease of use.


    HTH

    cksiow
    http://vblib.virtualave.net - share our codes

  3. #3
    Join Date
    Apr 2001
    Location
    Pondicherry,India 605009
    Posts
    21

    Re: Registry

    Hi!
    You can use RegQueryValueEx

    API.
    By using this dll function along with RegCloseKey ,RegOpenKey

    you can achive this.

    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




    If you want any further information, do reply.
    -Poobal

    Do what you Love to do and
    Commit Yourself to doing it in an
    Excellent Fashion

  4. #4
    Join Date
    Aug 2000
    Posts
    29

    Re: Registry

    can you please give me a sample code for using these registry function?
    thanks.


  5. #5
    Join Date
    Apr 2001
    Location
    Pondicherry,India 605009
    Posts
    21

    Re: Registry

    Hi!
    Here is the sample code.

    'This program needs 3 buttons
    Const REG_SZ = 1 ' Unicode nul terminated string
    Const REG_BINARY = 3 ' Free form binary
    Const HKEY_CURRENT_USER = &H80000001
    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 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
    Function RegQueryStringValue(byval hKey as Long, byval strValueName as string) as string
    Dim lResult as Long, lValueType as Long, strBuf as string, lDataBufSize as Long
    'retrieve nformation about the key
    lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, byval 0, lDataBufSize)
    If lResult = 0 then
    If lValueType = REG_SZ then
    'Create a buffer
    strBuf = string(lDataBufSize, Chr$(0))
    'retrieve the key's content
    lResult = RegQueryValueEx(hKey, strValueName, 0, 0, byval strBuf, lDataBufSize)
    If lResult = 0 then
    'Remove the unnecessary chr$(0)'s
    RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
    End If
    ElseIf lValueType = REG_BINARY then
    Dim strData as Integer
    'retrieve the key's value
    lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
    If lResult = 0 then
    RegQueryStringValue = strData
    End If
    End If
    End If
    End Function
    Function GetString(hKey as Long, strPath as string, strValue as string)
    Dim Ret
    'Open the key
    RegOpenKey hKey, strPath, Ret
    'get the key's content
    GetString = RegQueryStringValue(Ret, strValue)
    'Close the key
    RegCloseKey Ret
    End Function
    Sub SaveString(hKey as Long, strPath as string, strValue as string, strData as string)
    Dim Ret
    'Create a new key
    RegCreateKey hKey, strPath, Ret
    'Save a string to the key
    RegSetValueEx Ret, strValue, 0, REG_SZ, byval strData, len(strData)
    'close the key
    RegCloseKey Ret
    End Sub
    Sub SaveStringLong(hKey as Long, strPath as string, strValue as string, strData as string)
    Dim Ret
    'Create a new key
    RegCreateKey hKey, strPath, Ret
    'set the key's value
    RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
    'close the key
    RegCloseKey Ret
    End Sub
    Sub DelSetting(hKey as Long, strPath as string, strValue as string)
    Dim Ret
    'Create a new key
    RegCreateKey hKey, strPath, Ret
    'Delete the key's value
    RegDeleteValue Ret, strValue
    'close the key
    RegCloseKey Ret
    End Sub
    private Sub Command1_Click()
    Dim strString as string
    'Ask for a value
    strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
    If strString = "" Or Val(strString) > 255 Or Val(strString) < 0 then
    MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
    Exit Sub
    End If
    'Save the value to the registry
    SaveStringLong HKEY_CURRENT_USER, "test", "BinaryValue", CByte(strString)
    End Sub
    private Sub Command2_Click()
    'get a string from the registry
    Ret = GetString(HKEY_CURRENT_USER, "test", "BinaryValue")
    If Ret = "" then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
    MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
    End Sub
    private Sub Command3_Click()
    'Delete the setting from the registry
    DelSetting HKEY_CURRENT_USER, "test", "BinaryValue"
    MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
    End Sub
    private Sub Form_Load()
    Command1.Caption = "set Value"
    Command2.Caption = "get Value"
    Command3.Caption = "Delete Value"
    End Sub




    All the best.
    -Poobal


    Do what you Love to do and
    Commit Yourself to doing it in an
    Excellent Fashion

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

Featured