How to read a registry key in VBA

Posted on 2008-11-07
Last Modified: 2013-11-29
I would like to populate me.HDSerial with the data from: HKLM\Software\Microsoft\Windows Genuine Advantage\HDSLN

How can I read this registry data value ?
Question by:detroitdr
    LVL 75

    Accepted Solution

    LVL 38

    Assisted Solution

    see also this link:

    <o/t>Hi Mx!
    LVL 48

    Assisted Solution

    by:Gustav Brock
    Here's a function:

    Public Function GetHDSLN() As String

      Dim hKeyVar   As Long
      Dim PathVar   As String
      Dim ValueVar  As String
      Dim DataVar   As String
      PathVar = "Software\Microsoft\Windows Genuine Advantage"
      ValueVar = "HDSLN"
      Call ReadRegistry(hKeyVar, PathVar, ValueVar, DataVar)
      GetHDSLN = DataVar

    End Function

    On my machine it returns "5RA1GZF0".

    Insert the function and the generic code below into a new module.

    Option Compare Database
    Option Explicit
    Private Const HKEY_CURRENT_USER = &H80000001
    Private Const HKEY_LOCAL_MACHINE = &H80000002
    Private Const KEY_READ = &H20019
    Private Const REG_SZ = 1
    Private 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
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
      Alias "RegQueryValueExA" ( _
      ByVal hKey As Long, _
      ByVal lpValueName As String, _
      ByVal lpReserved As Long, _
      ByRef lpType As Long, _
      ByRef lpData As Any, _
      ByRef lpcbData As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" ( _
      ByVal hKey As Long) As Long
    Private Sub ReadRegistry( _
      hKey As Long, _
      subkey As String, _
      ValueVar As String, _
      DataVar As String)
      Dim datatype As Long  ' receives data type of read value
      Dim DataLen As Long  ' receives length of returned data
      Dim retval As Long  ' return value
      ' hKey: receives a handle to the newly created or opened registry key
      ' subkey: name of the subkey to open
      ' DataVar: receives data read from the registry
      ' datatype: receives data type of read value
      ' DataLen: receives length of returned data
      ' retval: return value
      ' Open the registry key.
      retval = RegOpenKeyEx(hKey, subkey, 0, KEY_READ, hKey)
      If retval = 0 Then
        ' Create buffer to receive the incoming data.
        DataLen = 255
        DataVar = Space(DataLen)
        ' Read the value from the registry key.
        retval = RegQueryValueEx(hKey, ValueVar, 0, datatype, ByVal DataVar, DataLen)
        ' Only attempt to display the data if it is in fact a string.
        If datatype = REG_SZ Then
          ' Remove zero termination and empty space from the buffer.
          DataVar = Left(DataVar, DataLen - 1)
        End If
      End If
      ' Close the registry key.
      retval = RegCloseKey(hKey)
    End Sub

    Open in new window

    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    </ot Hey pDog>

    LVL 48

    Expert Comment

    by:Gustav Brock
    Could you explain what this value is for?
    Other unknown info is available as well:


    Author Comment

    Cactus, this is the REAL serial number of your hd. The only problem using this is WGA must have run at least once to have this key.
    the UDG is unique number for your computer. again.. WGA must run once to have this.

    LVL 48

    Expert Comment

    by:Gustav Brock
    OK, thanks!


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
    In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now