VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



My procedures to access the registry with simple WMI referencing. The project is made to enhance th

by H W Samuel (4 Submissions)
Category: Registry
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 6th February 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

My procedures to access the registry with simple WMI referencing. The project is made to enhance the VB registry functions SaveSetting,

API Declarations


'the root keys you want to set
Public Enum REGToolRootTypes
HK_CLASSES_ROOT = &H80000000
HK_CURRENT_USER = &H80000001
HK_LOCAL_MACHINE = &H80000002
HK_USERS = &H80000003
HK_PERFORMANCE_DATA = &H80000004
HK_CURRENT_CONFIG = &H80000005
HK_DYN_DATA = &H80000006
End Enum

Dim strFullPath 'Stores full registry key paths
Dim objReg
Dim objSubKey, arrSubKeys

'Reference the local computer registry.
'Can be modified to access registries remotely!
Const strComputerName = "."

Rate My procedures to access the registry with simple WMI referencing. The project is made to enhance th



'much more options and freedom
Public Sub WMISaveSetting _
(strRoot As REGToolRootTypes, stSection, _
stKey, stDefault, Optional stAppPath = "Software\Samuels")
Dim bAlreadyExists As Boolean

'This function creates a key thus:
'strRoot\stAppPath\stSection\stKey
'Then sets the value of the stKey as stDefault

On Error GoTo HANDLE_ERR
bAlreadyExists = False 'Validation (see below)
strFullPath = stAppPath & "\" & stSection 'See declaration

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputerName & "\root\default:StdRegProv")
    
'Enumerate existance of the keys that are being saved
objReg.EnumKey strRoot, stAppPath, arrSubKeys   

'In case no such keys have been created yet
If IsNull(arrSubKeys) Then GoTo AVOID_ERR: 

For Each objSubKey In arrSubKeys 'Enumerate parent and child keys
    If objSubKey = stSection Then
        bAlreadyExists = True
    End If
Next

AVOID_ERR:
If bAlreadyExists = False Then objReg.CreateKey strRoot, strFullPath

'Note here that only string values are saved. This is again to mimic
'VB registry functions behaviour
objReg.SetStringValue strRoot, strFullPath, stKey, stDefault 'Sets value of key
Exit Sub

HANDLE_ERR:
MsgBox Err.Description, vbCritical
End Sub

'Again an enhanced mimic of the GetSetting function in VB
Public Function WMIGetSetting _
(stRoot As REGToolRootTypes, stSection, _
stKey, Optional stDefault, Optional stAppPath = "Software\Samuels")
 
On Error GoTo HANDLE_ERR
strFullPath = stAppPath & "\" & stSection
 
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputerName & "\root\default:StdRegProv")
 
'Note here that only string values are retrieved. This is again to mimic
'VB registry functions behaviour
objReg.GetStringValue stRoot, strFullPath, stKey, stDefault

WMIGetSetting = stDefault 'Get string value of key
Exit Function

HANDLE_ERR:
MsgBox Err.Description, vbCritical
End Function

'Careful with calling this! The rest are safe enough, 
'but deleting is another thing
'Again DeleteSetting made broader
Public Sub WMIDeleteSetting _
(strRoot As REGToolRootTypes, Optional stSection, Optional strAppPath = "Software\Samuels")

On Error GoTo HANDLE_ERR
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputerName & "\root\default:StdRegProv")
 
strFullPath = strAppPath & "\" & stSection
 
objReg.DeleteKey strRoot, strFullPath 'Delete parent key and all values
Exit Sub

HANDLE_ERR:
MsgBox Err.Description, vbCritical
End Sub

Download this snippet    Add to My Saved Code

My procedures to access the registry with simple WMI referencing. The project is made to enhance th Comments

No comments have been posted about My procedures to access the registry with simple WMI referencing. The project is made to enhance th. Why not be the first to post a comment about My procedures to access the registry with simple WMI referencing. The project is made to enhance th.

Post your comment

Subject:
Message:
0/1000 characters