CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2000
    Posts
    15

    Reading from registry

    I am trying to read data from registry using RegQueryValueEx API, the return value is ERROR_NOT_FOUND always! I don't know what's wrong. I managed to open the desired Key and retrieve its handle using RegOpenKeyEx API. How can I solve this?! I am coding in VB, any help?


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Reading from registry

    maybe you can look at somecode at http://vblib.virtualave.net for vbRegistry class.

    HTH


  3. #3
    Join Date
    Feb 2001
    Posts
    5

    Re: Reading from registry

    Do the following:

    Assuming you want to read a string value from registry .
    'Global constants

    Global Const HKEY_LOCAL_MACHINE = &H80000002
    Global Const KEY_ALL_ACCESS = &H3F

    Dim phkResult As Long 'result
    Dim szBuff() As Byte ' Byte array
    Dim nSizeBuff As Long 'Size
    Dim nType As Long 'Type
    Dim strRegValue As String 'Variable to hold registry value

    dim retval as long 'return val for func


    RegFuncRetVal = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_ALL_ACCESS, phkResult)

    retval = RegQueryValueExA(phkResult, "Regkey", 0, nType, vbNullString, nSizeBuff) 'Assuming Regkey is the value u want to read

    ReDim szBuff(nSizeBuff)

    RegFuncRetVal = RegQueryValueExAphkResult, "Regkey", 0, nType, szBuff(0), SizeBuff)
    If RegFuncRetVal <> ERROR_SUCCESS Then
    RegFuncRetVal = RegCloseKey(phkResult)
    End If

    strRegValue = ByteArrayToString(szBuff, nSizeBuff - 1)

    'Implementation for function ByteArrayToString

    Private Function ByteArrayToString(pByteArr() As Byte, pArrLen As Long) As String
    Dim szBuff As String
    Dim i As Long

    For i = 0 To pArrLen - 1
    szBuff = szBuff & Chr(pByteArr(i))
    Next i

    ByteArrayToString = szBuff
    End Function

    Try this and let me know.


  4. #4
    Join Date
    Dec 2000
    Posts
    15

    Re: Reading from registry

    I used similar code to this one before but it didn't work out. I managed to resolve the problem by opening the desired key (full path from Local machine till the key I need) then retrieved its default value, and it worked out just fine.
    The code is similar to this:

    '//** Variable Declaration
    Dim sValue as string
    Dim nValueSize as long
    Dim sKeyPath as string
    Dim nKeyHandel as long

    sValue = Space(255)
    nValueSize = Len(sValue)

    '// open the desired key and retrieve its data
    sKeyPath = “Software\MyProgram\Key”
    nResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyPath, 0, KEY_QUERY_VALUE, nKeyHandel)
    '// retrieve the default value
    nResult = RegQueryValueEx(nKeyHandel, "", 0&, REG_SZ, sValue, nValueSize)

    '// close the opened key
    RegCloseKey nKeyHandel



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