views:

819

answers:

6

Hi,

I want to read a string value from the registry and concatenate it with another certain string. I'm calling RegQueryValueEx() , like this:

Dim lResult As Long
Dim sLength As Long
Dim sString As String

sString = Space$(256)
sLength = 256

lResult = RegQueryValueEx(hKey, "MyKey", 0, REG_SZ, ByVal sString, sLength)
MsgBox sString & "blah-blah-blah"

RegQueryValueEx() works fine, I'm getting the needed string in sString and even can display it with MsgBox. But when I try to concat it with "some_string" I get only sString showed. Plz, help me.

Thanks

+1  A: 

Precedence issue, maybe? How about trying:

MsgBox(sString & "blah-blah-blah")

Or

Dim sDisplay as String
sDisplay = sString & "blah-blah"

MsgBox sDisplay
Andrew Medico
A: 

Perhaps the string contains a 0-character so that it ends prematurely?

Lasse V. Karlsen
A: 

You need to get rid of the null character at the end. I suggest getting an already written and tested registry module for VB6. Here is another example from vbnet But if you just want to get rid of nulls here is one I've used.

Public Function StringFromBuffer(ByRef strBuffer As String) As String
' Extracts String From a Buffer (buffer is terminated with null)
' 06/30/2000 - WSR

Dim lngPos As Long

    ' attempt to find null character in buffer
    lngPos = InStr(1, strBuffer, vbNullChar)

    ' if found
    If lngPos > 0 Then

        ' return everything before it
        StringFromBuffer = Left$(strBuffer, lngPos - 1)

    ' if not found
    Else

        ' return whole string
        StringFromBuffer = strBuffer

    End If ' lngPos > 0

End Function ' StringFromBuffer
Will Rickards
A: 

use Mid$ and sLength to pull the string values out of sString. This way you above strangeness due to extra characters (like the null terminator '0')

Remember when you deal with the Win32 API you have to keep in mind that it assumes C conventions which are not the same as VB Convention. So you have to do some cleanup before sending it along.

RS Conley
+6  A: 

There is probably a null-character in the string, because VB strings store the length of the string in memory just before the contents of the string. In your case that length is 256. When you load the content using RegQueryValueEx, it null-terminates the string (C-style), but does not change its indicated length, so in the VB world it's still 256 characters long. Then when you append the second string, it gets appended after the first 256 characters, but MsgBox only shows the contents up to the null-character.

Because RegQueryValueEx puts the length of the actual data in sLength, you can add this line before the MsgBox

sString = Left$(sString, sLength)
Manne
You need sLength - 1 as sLength is the length including the null character.
Will Rickards
A: 

It worked for me when I did:

sString = Left$(sString, sLength-1)

the problem indeed was the null character at the end of the string.

Thanks, guys!