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

Query a Registry Key 1

Status
Not open for further replies.

JEngles

MIS
May 21, 2003
93
0
0
AU
Can someone please help me out with this. How would I do something like:

FILE_PATH = QueryRegistryKey(HKLM\SOFTWARE\Classes\AcroExch.Document\shell\Open\command\[Default])

Cheers,
Johan
 
One approach would be to use the RegQueryValueEx API call.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I beleive this is what Centurion was referring to;

This is some code that we use. We have put the first part into a module

Option Explicit
Public Const REG_SZ As Long = 1
Public Const REG_DWORD As Long = 4

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 ERROR_NONE = 0
Public Const ERROR_BADDB = 1
Public Const ERROR_BADKEY = 2
Public Const ERROR_CANTOPEN = 3
Public Const ERROR_CANTREAD = 4
Public Const ERROR_CANTWRITE = 5
Public Const ERROR_OUTOFMEMORY = 6
Public Const ERROR_ARENA_TRASHED = 7
Public Const ERROR_ACCESS_DENIED = 8
Public Const ERROR_INVALID_PARAMETERS = 87
Public Const ERROR_NO_MORE_ITEMS = 259

Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_ALL_ACCESS = &H3F

Public Const REG_OPTION_NON_VOLATILE = 0

Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias _
"RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _
As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _
As Long, phkResult As Long, lpdwDisposition As Long) As Long
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 RegQueryValueExString Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
As String, lpcbData As Long) As Long
Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
String, ByVal lpReserved As Long, lpType As Long, lpData As _
Long, lpcbData As Long) As Long
Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
As Long, lpcbData As Long) As Long
Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _
String, ByVal cbData As Long) As Long
Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _
ByVal cbData As Long) As Long
Public Function QueryValue(sKeyName As String, sValueName As String) As String
Dim lRetVal As Long 'result of the API functions
Dim hKey As Long 'handle of opened key
Dim vValue As Variant 'setting of queried value



'lRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyName, 0, KEY_QUERY_VALUE, hKey)
lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyName, 0, KEY_QUERY_VALUE, hKey)
lRetVal = QueryValueEx(hKey, sValueName, vValue)
'MsgBox vValue
QueryValue = vValue

RegCloseKey (hKey)

End Function
'SetValueEx and QueryValueEx Wrapper Functions:
Public Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long
Dim lValue As Long
Dim sValue As String
Select Case lType
Case REG_SZ
sValue = vValue & Chr$(0)
SetValueEx = RegSetValueExString(hKey, sValueName, 0&, lType, sValue, Len(sValue))
Case REG_DWORD
lValue = vValue
SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, lType, lValue, 4)
End Select
End Function
Public Sub SetKeyValue(sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long)
Dim lRetVal As Long 'result of the SetValueEx function
Dim hKey As Long 'handle of open key

'open the specified key

'lRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyName, 0, KEY_SET_VALUE, hKey)
lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyName, 0, KEY_SET_VALUE, hKey)
lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
RegCloseKey (hKey)

End Sub

Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
Dim cch As Long
Dim lrc As Long
Dim lType As Long
Dim lValue As Long
Dim sValue As String

On Error GoTo QueryValueExError

' Determine the size and type of data to be read
lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
If lrc <> ERROR_NONE Then Error 5

Select Case lType
' For strings
Case REG_SZ:
sValue = String(cch, 0)

lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
If lrc = ERROR_NONE Then
vValue = Left$(sValue, cch - 1)
Else
vValue = Empty
End If
' For DWORDS
Case REG_DWORD:
lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
If lrc = ERROR_NONE Then vValue = lValue
Case Else
'all other data types not supported
lrc = -1
End Select

QueryValueExExit:
QueryValueEx = lrc
Exit Function

QueryValueExError:
Resume QueryValueExExit
End Function
Public Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
Dim hNewKey As Long 'handle to the new key
Dim lRetVal As Long 'result of the RegCreateKeyEx function

lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal)
RegCloseKey (hNewKey)
End Sub

Then we use the various procedures like this;

CreateNewKey "Software\folder\folder", HKEY_LOCAL_MACHINE
SetKeyValue "Software\folder\folder", "keyvalue", keyvaluedata, REG_SZ
tmpVariable = QueryValue("Software\folder\folder", "keyvalue")
 
That's a nice set of functions, and would no doubt be useful in a full suite of Registry functions, but if all you need to do is read a value, then it might be a bit of overkill.

To simply read a String value from the Registry, all you need are the following constant and API declarations, then I've put inside of a CommandButton event, the code to open, query, and close the Registry
Code:
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

'==========================================

Private Sub cmdGetRegKey_Click()

   Dim RetVal     As Long
   Dim KeyHandle  As Long
   Dim KeyName    As String
   Dim ValType    As Long
   Dim ValSize    As Long
   Dim ValString  As String
   
   KeyName = "SOFTWARE\Classes\AcroExch.Document\shell\open\command"
   RegOpenKey HKEY_LOCAL_MACHINE, KeyName, KeyHandle
[COLOR=green]   ' Note the default value = "" not (Default)[/color]
   RetVal = RegQueryValueEx(KeyHandle, "", 0, ValType, ByVal 0, ValSize)
   If (RetVal = 0) Then
      If (ValType = REG_SZ) Then
         ValString = String(ValSize, Chr(0))
         RetVal = RegQueryValueEx(KeyHandle, "", 0, 0, ByVal ValString, ValSize)
         ValString = Left(ValString, InStr(ValString, Chr(0)) - 1)
         MsgBox "Key Value = " & ValString
      Else
         MsgBox "This key not a string value"
      End If
   Else
      MsgBox "Error reading key data - check key value"
   End If
   RegCloseKey KeyHandle

End Sub


Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
The easiest way I know to read a registry key default value is this:
Set sh = CreateObject("WScript.Shell")
key = "HKLM\SOFTWARE\Classes\AcroExch.Document\shell\Open\command\"
FILE_PATH = sh.RegRead(key)
Just a note: You'll have to deal with the final "%1"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Well it wasn't as easy as I had hoped, except perhaps had I been able to use PHV's suggestion ;-)

But thank you soo much for the help! Centurion your code was to put it mildy; exactly what I was looking for [medal]

Just curious though, what does this line mean. I know what it does...but, what's with all the variables?
RegQueryValueEx(KeyHandle, "", 0, ValType, ByVal 0, ValSize)

Thanks again,
Johan
 
The purpose of the first RegQueryValueEx(KeyHandle, "", 0, ValType, ByVal 0, ValSize) call is determine the type and size of the key's data.
KeyHandle is the handle to the Key (""SOFTWARE\Classes\AcroExch.Document\shell\open\command") that was provided to us when we opened the key with RegOpenKey call.
"" - this is the name of the specific value that we're after. In this case, the value we want is "(Default)", and the proper representation of default (at least with respect to the register) ""
0 - Reserved - always 0
ValType is being set by the function, and will represent the type of the data to be returned. In the actual code, we know the data should be type String, and all we're doing is verifying that. If (ValType = REG_SZ) Then
ByVal 0 - Sending over a null pointer here is what informs the function that we want the Type and Size of the data, and not the actual data itself.
ValSize is being set by the function, and is the size of the data for the open key.

As far as PHV's suggestion to use the Script Host Object Model, you should be aware that it certainly is an easier interface, not only to the registry, but to other systems things as well, but I've led to believe that it can be rendered completely useless should the System Administrator decide to disable all Scripting functions. That is why I no longer use it nor recommend its use.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I agree Centurion about the VBScrips, I block them out also.

Thankyou for all your help, I have managed to integrate it with what I was doing with complete success. Once again I have been showed how great this site is :)

Cheers,
Joha
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top