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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegQueryValueEx 1

Status
Not open for further replies.

figler

Programmer
Dec 26, 2001
155
US
A pretty typical story, I think:
I downloaded someone else's code, hoping that with it I wouldn't have to deal with Windows API to query/write to Registry. Then I get some funny behavior and, not knowing even where to start, I come to Tek-Tips for some expert advice.
Well, the code is attached. The weird thing is that the key (a String) looks normal when I view it using regedit. But when I use RegQueryValueEx, I get a weird string returned to me. The value is the path of an Access database:
C:\Documents And Settings\AUser\My Documents\Database.MDB
What I get back is:
C:\Documents And Settings\AUser\My Documents\Database.MDB o c u m e n t s \ d a t a b a s e . m d b

Does anyone have *ANY* idea why this happens?

Thanks! -Brad
Code:
Function GetStringValue(SubKey As String, Entry As String)

Call ParseKey(SubKey, MainKeyHandle)

If MainKeyHandle Then
   rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_READ, HKey) 'open the key
   If rtn = ERROR_SUCCESS Then 'if the key could be opened then
      sBuffer = Space(255)     'make a buffer
      lBufferSize = Len(sBuffer)
      rtn = RegQueryValueEx(HKey, Entry, 0, REG_SZ, sBuffer, lBufferSize)
'...
End Function
ps I only included a chunk of code here, because sBuffer is messd up as of the RegQueryValueEx function (the last line)...
 
I think you need to add the line
sbuffer = left$(sbuffer,lbuffersize)

Many API functions return a null terminated string rather than a VB string. Since the functions also returns the length of the string the above line of code will truncate the buffer at the appropriate place.

Glyn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top