Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unexpected behavior of RegQueryValueEx API

Status
Not open for further replies.

rockyr

Technical User
Dec 28, 2000
12
US
I have a vb 6 app that uses the RegQueryValueEx function to get a value from a subkey.

The function is defined in a code module, and called from two locations, one on a form, the other in the same code module.

Note, that I call the exact same function from each location. When I call it from the form, on a command button click event, it returns the value properly. When I call it from the code module, it has some other information appended after the value.

For example, the value stored in the registry is named "File Path", with a value of "\\servername\sharename\" That is returned properly when called from the form, but "\\servername\sharename\ s h a r e n a m e " is returned when the same function is called from the code module.

The blanks in the appended info are chr(0)'s, so I can strip them off easily enough, but I would really like to know why this is happening.
 
It looks to me like the characters appended to the string are just residual garbage from a previous call. Can you show us the code? VBSlammer
redinvader3walking.gif
 
Here's the function that does the actual call:

Function GETVAL(valname As String) As String
Dim nBufferKey As Long, nBufferSubKey As Long
Dim nBufferDATA As String
nBufferDATA = Space(255)
RegOpenKey LOCALMACHINE, "SOFTWARE\eDirect", nBufferKey
RegOpenKey nBufferKey, "Settings", nBufferSubKey
RegQueryValueEx nBufferSubKey, valname, 0, REG_SZ, nBufferDATA, Len(nBufferDATA)
GETVAL = Trim(nBufferDATA)
End Function


As you can see, I've initialized the buffer to spaces, so there shouldn't be any residual data in there. Also, the same function, when called from code on a form doesn't have the garbage attached. It's strange.
 
You can't use Trim() to get rid of the null characters in a fixed length string. Try this:
Code:
GETVAL = Left(nBufferDATA, InStr(nBufferDATA, Chr(0)) - 1)
VBSlammer
redinvader3walking.gif
 
It's also a good practice to check the return values from your API calls so you can bail out of the function when necessary:

Code:
Function GETVAL(ByVal valname As String) As String
  Dim nBufferKey      As Long
  Dim nBufferSubKey   As Long
  Dim nBufferDATA     As String
  Dim varReturn       As Variant
  
  nBufferDATA = Space(255)
  
  varReturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\", _
        0, KEY_QUERY_VALUE, nBufferKey)
  
  If varReturn = ERROR_SUCCESS Then

    varReturn = RegOpenKeyEx(nBufferKey, "CurrentVersion", _
            0, KEY_QUERY_VALUE, nBufferSubKey)
    
    If varReturn = ERROR_SUCCESS Then
    
      varReturn = RegQueryValueEx(nBufferSubKey, valname, _
            0, REG_SZ, ByVal nBufferDATA, Len(nBufferDATA))
      
      If varReturn = ERROR_SUCCESS Then
      
        GETVAL = Left(nBufferDATA, InStr(nBufferDATA, Chr(0)) - 1)
      
      End If
    End If
  End If
  
End Function
VBSlammer
redinvader3walking.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top