How to read a registry key in VBA

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 ?
detroitdrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
puppydogbuddyCommented:
see also this link:
                     http://vba-corner.livejournal.com/3054.html


<o/t>Hi Mx!
0
Gustav BrockCIOCommented:
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
 
  hKeyVar = HKEY_LOCAL_MACHINE
  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.

/gustav
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

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
</ot Hey pDog>

mx
0
Gustav BrockCIOCommented:
Could you explain what this value is for?
Other unknown info is available as well:

  http://bobbo.me.uk/?p=35

/gustav
0
detroitdrAuthor Commented:
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.

0
Gustav BrockCIOCommented:
OK, thanks!

/gustav
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.