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!

Reading registry entries with RegQueryValueEx

Status
Not open for further replies.

Ditch2

Programmer
Aug 29, 2000
6
US
Obviously I'm missing something fundamental here. I get a successful result (0 on CallResult) along with the correct value type (KeyType = 1) and buffer size (KeySize = 35). I just never get the string value - which is what I want. It always comes back empty (KeyVal = ""). I've gone through several Microsoft examples and I can't figure out what I'm doing wrong. Help!

My test code is to supposed to bring back the path of the IE4 setup (non-critical for safety). I've tried a few other keys as well and can't get them to work either. The code goes like this

Module 1:

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long

Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_CURRENT_USER = &H80000001

Attached to a command button:

Private Sub Command1_Click()
Dim CallResult As Long
Dim hKey As Long
Dim KeyType As Long
Dim KeyVal As String
Dim KeySize As Long


CallResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\IE4\Setup", 0, &H1, hKey)

CallResult = RegQueryValueEx(hKey, "Path", 0&, KeyType, ByVal KeyVal, KeySize)

CallResult = RegCloseKey(hKey)
End Sub
 
you declare lpData as Any implicitely ByRef in the declare
statement, but you use ByVal KeyVal in the function call.
remove this inconsistency
 
Thanks, but it still won't work. I had actually tried it after I sent out the request with no success.

I'm absolutly at my wits end!
 
Here´s the code

Any Form:
CmbServer = getstring(HKEY_LOCAL_MACHINE, "Software\William\Connect\" + CmbNombre, "Server:")

'***************
'REGSITRY.BAS
'***************


Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_DWORD = 4 ' 32-bit umber

Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&


Public Function getstring(hKey As Long, strPath As String, strValue As String)
Dim keyhand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
Dim lValueType As Long

lResult = RegOpenKey(hKey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
getstring = Left$(strBuf, intZeroPos - 1)
Else
getstring = strBuf
End If
End If
End If

End Function
Function getdword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long

r = RegOpenKey(hKey, strPath, keyhand)

' Get length/data type
lDataBufSize = 4

lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)

If lResult = ERROR_SUCCESS Then
If lValueType = REG_DWORD Then
getdword = lBuf
End If
'Else
' Call errlog("GetDWORD-" & strPath, False)
End If

r = RegCloseKey(keyhand)

End Function

 
Well shoot. I came back to post that I had finally figured it out only to find Kazalivich had the correct answer and that hadn't been notified about yet. Anyway...

It seems as though you have to pass a buffer size into lpcdata, in my case keysize, that is greater than or equal to the length of the string you want the function to return . A better way is do what Kazalivich has done and let the function return the size first, store it in a variable, call the function again and pass the size back to second function call to return the string that is wanted.

It's all clearly documented, now that I know what it means!

Thanks all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top