Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

A weblog about Microsoft Access, SQL Azure, and SQL Server Tips and Code Samples
Browse by Tags · View All
Access Tips 45
Microsoft Access 45
VBA 41
database 33
Access 33
Code 32
Software development 31
Microsoft Access Software Development 26
Free Access Downloads 25
Software 25

Archive · View All
October 2010 4
September 2008 4
June 2009 4
January 2008 3
July 2009 3
June 2007 2
May 2009 2
March 2011 2
July 2010 2
August 2011 2

Patrick Wood's Blog

Microsoft Access VBA Uses WMI to Read the Registry

Jul 16 2009 6:16AM by Patrick Wood   

Microsoft Access VBA can read and write to the Windows Registry by using WMI. In this article we are going to show you how easy it is to read 5 different types of Registry Values with VBA and WMI.

The Registry provided a solution for the problem caused by the proliferation of separate ini settings files which often contained conflicting settings. The Registry became the central repository for data and settings reducing conflicts and providing better organization for the operating system, hardware, services, and applications settings. Most changes made concerning a computer and software are stored in the Registry.

The Registry is useful for the Access developer for storing connection strings, user preferences, a last used list, default startup settings and more. Since the Registry is vital to the operating system, hardware, services, and software it contains a very large and varied amount of data. Within this vast amount of data are treasures of information to be found. So let us begin by examining how we can read data from the Registry.

At first glance the Registry may seem very complicated, but actually the registry contains just two main types of elements: Keys and Values. The Keys are very similar to folders and subfolders having a similar hierarchy of Keys and SubKeys. The Registry Values are Name and Data pairs which are very much like the Properties and their Values we use in VBA code.

The top level Registry Keys are organized into Root Keys also known as Hives. The Root Keys on my XP are:

  • HKEY_CLASSES_ROOT
  • HKEY_CURRENT_USER
  • HKEY_LOCAL_MACHINE
  • HKEY_USERS
  • HKEY_CURRENT_CONFIG

The most common Registry Value Names and their Constants are:

  • String: REG_SZ = 1
  • Expanded String: REG_EXPAND_SZ = 2
  • Binary: REG_BINARY = 3
  • DWord: REG_DWORD = 4
  • Multi-String: REG_MULTI_SZ = 7

I will not take the time to explain them all here since ample information about Registry Value Types can easily be found on the internet.

Before I forget I need to give the usual, but important, warning that making changes to the Registry can cause damage to your software and operating system so if you are smart you will back up the Registry before making changes. To do that click on Start, then select Run, enter “Regedit” without the quotes and then click “OK”. After Regedit opens click on File and then Export to export your Registry to a backup file. This will probably take a few minutes, but it is better than hours of tears because you lost your loved ones photos!

The Registry stores information in several different types and using WMI we can read all the types listed here. We will start by declaring the Root Key Constants at the beginning of the Module.

Option Compare Database
Option Explicit

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005

Most of this first part of the procedures is familiar to you if you have been following our articles. However in these procedures we access a different WMI root. When we Set the objReg we use “\root\default:StdRegProv” instead of “\root\cimv2″.

The first example demonstrates how to read is the simple String Value.

Sub GetRegString()
    Dim objReg As Object
    Dim strComputer As String
    Dim strKeyPath As String
    Dim strValueName As String
    Dim strValue

    strComputer = "."

     Set objReg = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\" _
        & strComputer & "\root\default:StdRegProv")

    strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.EXE"
    strValueName = "Path"

    objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
    Debug.Print "Microsoft Access Location: " & strValue

    Set objReg = Nothing

End Sub

The next value to read is the often used DWord Value. In this procedure we use a Select Case construct to give meaning to the DWord value. In this case it is the Windows Automatic Updates setting.

Sub GetRegDWord()
    Dim objReg As Object
    Dim strComputer As String
    Dim strKeyPath As String
    Dim strValueName As String
    Dim strDWordValue As String
    Dim dwValue

    strComputer = "."

     Set objReg = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\" _
        & strComputer & "\root\default:StdRegProv")

    strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
    strValueName = "AUOptions"

    objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, dwValue

    ' If there is no DWord Value we want "Null" to be returned
    strDWordValue = Nz(dwValue, "Null")

    Select Case strDWordValue
        Case "2"
            strDWordValue = strDWordValue & " - Notify for download and notify for install."
        Case "3"
            strDWordValue = strDWordValue & " - Auto download and notify for install."
        Case "4"
            strDWordValue = strDWordValue & " - Auto download and schedule the install."
        Case "Null"
            strDWordValue = "Automatic Updates status is Unknown - No value was returned."
        Case Else
            strDWordValue = strDWordValue & " - Automatic Updates are not enabled."
    End Select

    Debug.Print strDWordValue

    Set objReg = Nothing

End Sub

The next value to read is the Multi-String Value. In this procedure we will get a list of Event Log Sources. In a later article we will show how to filter and save specific Event Logs Text which contain important data.

' Example Call SaveRegSystemEventLog("C:\SystemEventLogSources.txt")
Sub SaveRegSystemEventLog(strTextFilePath As String)
    Dim objReg As Object
    Dim fso As Object
    Dim fsoFile As Object
    Dim strComputer As String
    Dim strValueName As String
    Dim strKeyPath As String
    Dim arrValues
    Dim arrValue

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFile = fso.CreateTextFile(strTextFilePath)

    strComputer = "."

     Set objReg = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\" _
        & strComputer & "\root\default:StdRegProv")

    strKeyPath = "SYSTEM\CurrentControlSet\Services\Eventlog\System"
    strValueName = "Sources"

    objReg.GetMultiStringValue HKEY_LOCAL_MACHINE, strKeyPath, _
        strValueName, arrValues

    For Each arrValue In arrValues
        fsoFile.WriteLine arrValue
'        Debug.Print arrValue
    Next

    fsoFile.Close

    Set fsoFile = Nothing
    Set fso = Nothing
    Set objReg = Nothing

End Sub

The next value to read is the Expanded String Value.

Sub ReadExpandedRegString()

    Dim objReg As Object
    Dim strComputer As String
    Dim strValueName As String
    Dim strKeyPath As String
    Dim strValue

    strComputer = "."

    Set objReg = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\" _
        & strComputer & "\root\default:StdRegProv")

    strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon"
    strValueName = "UIHost"

    objReg.GetExpandedStringValue HKEY_LOCAL_MACHINE, strKeyPath, _
        strValueName, strValue

    Debug.Print "The Windows logon UI host is: " & strValue

    Set objReg = Nothing

End Sub

Lastly, we see the code to read a binary registry value. In this case the code reads the binary AccessName Value.

Sub ReadBinaryRegValue(strTextFilePath As String)

    Dim objReg As Object
    Dim fso As Object
    Dim fsoFile As Object
    Dim strComputer As String
    Dim strKeyPath As String
    Dim strValueName As String
    Dim i As Long
    Dim varValue

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFile = fso.CreateTextFile(strTextFilePath)

    strComputer = "."

    Set objReg = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\" _
        & strComputer & "\root\default:StdRegProv")

    strKeyPath = "Software\Microsoft\Office\11.0\Access"
    strValueName = "AccessName"

    objReg.GetBinaryValue HKEY_CURRENT_USER, strKeyPath, _
        strValueName, varValue

    For i = LBound(varValue) To UBound(varValue)
        fsoFile.WriteLine varValue(i)
    Next

    fsoFile.Close

    Set fsoFile = Nothing
    Set fso = Nothing
    Set objReg = Nothing

End Sub

We will learn more about how VBA can manage the Registry using WMI in the articles to come.


Republished from Access Easy Tips [18 clicks].  Read the original version here [32134 clicks].

Patrick Wood
101 · 2% · 521
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]