Concatenated strings returned with RegQueryValueEx
I want to read a string value from the registry and associate it with another specific string. I call RegQueryValueEx () like:
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 great, I get the string I want in sString and can even show it using MsgBox. But when I try to execute it with "some_string" I only get sString. Plz help me.
thank
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, the length is 256. When you load content using RegQueryValueEx, it terminates the null-string (C-style) but does not change its specified length, so in the VB world it is another 256 characters. Then when you add the second line, it is added after the first 256 characters, but the MsgBox shows the content up to the null character.
Since RegQueryValueEx puts the actual data length in sLength, you can add this line before MsgBox
sString = Left$(sString, sLength)
source to share
You need to get rid of the null character at the end. I suggest getting already written and tested > for VB6 . Here's another example from vbnet But if you just want to get rid of the zeros then this is the one I 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
source to share
use Mid $ and sLength to output string values from sString. So you are above weirdness due to extra characters (like the null terminator "0")
Remember that when you are dealing with a Win32 API, you must keep in mind that it assumes C conventions that are not the same as the VB Convention. Therefore, some cleaning needs to be done before shipping.
source to share