Click to See Complete Forum and Search --> : Reading from registry


Daed
April 25th, 2001, 08:59 AM
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?

cksiow
April 25th, 2001, 08:05 PM
maybe you can look at somecode at http://vblib.virtualave.net for vbRegistry class.

HTH

sspawse
April 26th, 2001, 05:26 AM
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.

Daed
April 26th, 2001, 06:01 AM
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