Function SetValueEx(ByVal hKey As Long, sValueName As String, _
lType As Long, vValue As Variant) As Long
Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
'With this procedure a call of:
'
' CreateNewKey "TestKey", HKEY_CURRENT_USER
'
'creates a key called TestKey immediately under HKEY_CURRENT_USER.
'Calling CreateNewKey like this:
'
'
' CreateNewKey "TestKey\SubKey1\SubKey2", HKEY_LOCAL_MACHINE
'
'creates three-nested keys beginning with TestKey immediately under
'HKEY_LOCAL_MACHINE, SubKey1 subordinate to TestKey, and
'SubKey3 under SubKey2.
Sub SetKeyValue(sKeyName As String, sValueName As String, _
vValueSetting As Variant, lValueType As Long)
'Creating and setting a value of a specified key can be accomplished
'with the following short procedure. SetKeyValue takes the key that
'the value will be associated with, the name of the value, the setting
'of the value, and the type of the value (the SetValueEx function only
'supports REG_SZ and REG_DWORD, but this can be modified if necessary).
'Specifying a new value for an existing sValueName will modify the
'current setting of that value.
'A call of:
'
' SetKeyValue "TestKey\SubKey1", "StringValue", "Hello", REG_SZ
'
'creates a value of type REG_SZ called "SubKey1" with the setting
'of "Hello." This value will be associated with the key SubKey1
'of "TestKey." In this case, "TestKey" is a subkey of HKEY_CURRENT_USER,
'but this can be modified by changing the call to RegOpenKeyEx. This
'call will fail if "TestKey\SubKey1" does not exist. To avoid this
'problem, use a call to RegCreateKeyEx instead of a call to
'RegOpenKeyEx. RegCreateKeyEx will open a specified key if it already
'exists.
Sub QueryValue(sKeyName As String, sValueName As String)
'This procedure can be used to ascertain the setting of an existing
'value. QueryValue takes the name of the key and the name of a value
'associated with that key and returns the corresponding value. It uses
'a call to the QueryValueEx wrapper function that only supports REG_SZ
'and REG_DWORD types. With this procedure, a call of:
'
' QueryValue "TestKey\SubKey1", "StringValue"
'
'returns the current setting of the "StringValue" value, and assumes
'that "StringValue" exists in the "TestKey\SubKey1" key.
'If the Value that you query does not exist then QueryValue will return
'an error code of 2 - 'ERROR_BADKEY'.
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_ALL_ACCESS, hKey)
lRetVal = QueryValueEx(hKey, sValueName, vValue)
MsgBox vValue
RegCloseKey (hKey)
End Sub
Function GetKeyValue(KeyRoot As Long, KeyName As String, _
SubKeyRef As String, _
ByRef KeyVal As String) As Boolean
|