Retrieve Registry Info

This sample code demonstrates how to read various Values from the Registry. It will retrieve the ComputerName, Internet Explorer Version, Windows Version, ProductID and Serial Number.,Registered Name and Company Name.

You Need: Command Buttons 1 to 5

' In a Module1.bas
' Registry
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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
Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long

Global Const KEY_QUERY_VALUE = &H1
Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_USERS = &H80000003

Public Function GetRegStringValue$(Where, sKeyName$, sValueName$)
   Dim lRetVal As Long         'result of the API functions
   Dim hKey As Long         'handle of opened key

   lRetVal = RegOpenKeyEx(Where, sKeyName$, 0, KEY_QUERY_VALUE, hKey)

   Dim cch As Long
   Dim lrc As Long
   Dim lType As Long
   Dim sValue As String

   lrc = RegQueryValueExNULL(hKey, sValueName, 0&, lType, 0&, cch)
   GetRegStringValue$ = Space$(cch)
   lrc = RegQueryValueExString(hKey, sValueName, 0&, lType, GetRegStringValue$, cch)
   If lrc = 0 Then GetRegStringValue$ = Left$(GetRegStringValue$, cch - 1)

   RegCloseKey hKey
End Function


' In a Form
Private Sub Command1_Click()
   Where = HKEY_LOCAL_MACHINE
   sKeyName$ = "System\CurrentControlSet\Control\ComputerName\ComputerName"
   sValueName$ = "ComputerName"
   
   MsgBox GetRegStringValue(Where, sKeyName$, sValueName$)
End Sub

Private Sub Command2_Click()
   MsgBox _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "RegisteredOwner") + Chr$(10) + _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "RegisteredOrganization")
End Sub

Private Sub Command3_Click()
   MsgBox _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "ProductName") + Chr$(10) + _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "Version") + Chr$(10) + _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "VersionNumber") + Chr$(10) + Chr$(10) + _
   "   Product ID: " + GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "ProductID") + Chr$(10) + _
   "Product S/N: " + GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", "ProductKey") + Chr$(10)
End Sub


Private Sub Command4_Click()
   MsgBox _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Hardware\Description\System\CentralProcessor\0", "Identifier") + Chr$(10) + _
   GetRegStringValue$(HKEY_LOCAL_MACHINE, "Hardware\Description\System\CentralProcessor\0", "VendorIdentifier")

End Sub

Private Sub Command5_Click()
   MsgBox GetRegStringValue$(HKEY_LOCAL_MACHINE, "Software\Microsoft\Internet Explorer", "Version")
End Sub

You might also like...

Comments

Kym Manson Till the Roof comes off Till the Lights go out Till my Legs give out Can't shut my mouth I will not fall, my Wisdoms all.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.” - Jon Ribbens